Close

4th step : Try to make it on the 3x8x32 LED matrix

A project log for 2D LED Ping ball

We want to create a kind of joystick, using an accelerometer, that follows the movement of our hands and that display it on a LED matrix

sarahsarah 01/09/2024 at 09:370 Comments

We first tried to keep using the code we have and that works with the small LED matrix by only changing the width and a few other details of the code but that didn't work out. We spent a very long time on this because we couldn't understand why our code wouldn't work anymore. We changed the wiring multiple times thinking that was maybe our problem but it apparently wasn't. Down here is the failed code we tried for our LED matrix and the results on the LED matrix.

#include <LedControl.h>
#include <Wire.h>
 
#define DEVICE (0x53)   // ADXL345 device address
#define TO_READ (6)      // num of bytes we are going to read (two bytes for each axis)
 
byte buff[TO_READ];      // 6 bytes buffer for saving data read from the device
 
int MATRIX_WIDTH = 8;
int MATRIX_HEIGHT = 32;  // Change to 32 for an 8x32 matrix
 
LedControl lc = LedControl(12, 11, 10, 1); // DIN, CLK, CS, NRDEV
unsigned long delaytime = 50;
int x_key = A1;
int y_key = A0;
int x_pos;
int y_pos;
 
class Grain
{
public:
  int x = 0;
  int y = 0;
  int mass = 1;
};
Grain *g;
 
void setup()
{
  g = new Grain();
 
  ClearDisplay();
 
  Wire.begin();
  Serial.begin(9600);
 
  writeTo(DEVICE, 0x2D, 0);
  writeTo(DEVICE, 0x2D, 16);
  writeTo(DEVICE, 0x2D, 8);
}
 
void loop()
{
  int regAddress = 0x32;
  int x, y;
 
  readFrom(DEVICE, regAddress, TO_READ, buff);
 
  x = (((int)buff[1]) << 8) | buff[0];
  y = (((int)buff[3]) << 8) | buff[2];
 
  x = map(x, -300, 300, 0, MATRIX_WIDTH);
  y = map(y, -300, 300, 0, MATRIX_HEIGHT);  // Adjust mapping for 8x32 matrix
 
  Serial.print("X: ");
  Serial.print(x);
  Serial.print("   Y: ");
  Serial.print(y);
  Serial.print("\n");
 
  ClearDisplay();
  g->x = x;
  g->y = y;
  lc.setLed(0, g->x, g->y, true);
 
  delay(10);
}
 
void ClearDisplay()
{
  int devices = lc.getDeviceCount();
 
  for (int address = 0; address < devices; address++)
  {
    lc.shutdown(address, false);
    lc.setIntensity(address, 1);
    lc.clearDisplay(address);
  }
}
 
void writeTo(int device, byte address, byte val)
{
  Wire.beginTransmission(device);
  Wire.write(address);
  Wire.write(val);
  Wire.endTransmission();
}
 
void readFrom(int device, byte address, int num, byte buff[])
{
  Wire.beginTransmission(device);
  Wire.write(address);
  Wire.endTransmission();
 
  Wire.beginTransmission(device);
  Wire.requestFrom(device, num);
 
  int i = 0;
  while (Wire.available())
  {
    buff[i] = Wire.read();
    i++;
  }
  Wire.endTransmission();
}

Discussions