Hi everyone,
I have been trying to set up a 16 X 16 led matrix (WS2812b) to my ESP32- WROOM. The goal is to simulate LED dots falling along with gravity and to do this I used an ADXL335 accelerometer to measure the tilt angles. I'll upload my code below:
#include "FastLED.h"
#define LED_PIN 16 // where panel Din is connected.
#define LED_COUNT 20 // Number of leds you want to have active at all times
#define MATRIX_SIZE 16
CRGB leds[LED_COUNT];
const int x_out = A0;
const int y_out = A3;
const int z_out = A6;
const int sampleSize = 10;
int ReadAxis(int axisPin)
{
long reading = 0;
analogRead(axisPin);
delay(1);
for (int i = 0; i < sampleSize; i++)
{
reading += analogRead(axisPin);
}
return reading/sampleSize;
}
const int xo = ReadAxis(x_out)/10;
const int yo = ReadAxis(y_out)/10;
const int zo = ReadAxis(z_out)/10;
struct Dot {
float x;
float y;
float velocityX;
float velocityY;
};
Dot dots[LED_COUNT];
void setup() {
delay(50);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, LED_COUNT);
FastLED.setBrightness(50); //Number 0-255. Prefer 25 -50 ig to decrease current consumption
// Initialize dots
for (int i = 0; i < LED_COUNT; i++) {
dots[i].x = random(0,MATRIX_SIZE); // somewhere in the matrix
dots[i].y = random(0,MATRIX_SIZE); // just defining all points here. They don't light up
dots[i].velocityX = 0;
dots[i].velocityY = 0;
}
}
bool checkCollision(int dotIndex) {
for (int i = 0; i < LED_COUNT; i++) {
if (i != dotIndex) {
float distance = sqrt(pow(dots[i].x - dots[dotIndex].x, 2) + pow(dots[i].y - dots[dotIndex].y, 2));
if (distance < 1) { // Adjust collision threshold as needed
return true; // Collision detected
}
}
}
return false; // No collision
}
void loop() {
float gravity = 9.8; // Earth's gravity, adjust if needed
float dotSpeed = 0.05; // Adjust dot movement speed
int xAccel = analogRead(x_out) / 10; //Not using the AXDL335 library or anything
int yAccel = analogRead(y_out) / 10;
int zAccel = analogRead(z_out) / 10;
xAccel = xAccel - xo;
yAccel = yAccel - yo;
zAccel = zAccel - zo;
// Calculating Tilt angle
float tiltX = map(xAccel, -50, 50, -90, 90); // prefer - 50 to 50 because that is how much change in reading occurs when tilted -90 or +90 degrees
float tiltY = map(yAccel, -50, 50, -90, 90);
// Update dot velocities based on tilt angles
for (int i = 0; i < LED_COUNT; i++) {
dots[i].velocityX += tiltX * dotSpeed;
dots[i].velocityY += tiltY * dotSpeed;
dots[i].x += dots[i].velocityX;
dots[i].y += dots[i].velocityY;
// Check for collisions with edges
if (dots[i].x < 0 || dots[i].x >= MATRIX_SIZE) {
dots[i].velocityX *= 0;
dots[i].x = constrain(dots[i].x, 0, MATRIX_SIZE - 1);
}
if (dots[i].y < 0 || dots[i].y >= MATRIX_SIZE) {
dots[i].velocityY *= 0;
dots[i].y = constrain(dots[i].y, 0, MATRIX_SIZE - 1);
}
// Check for collisions with other dots
if (checkCollision(i)) {
dots[i].velocityX *= 0;
dots[i].velocityY *= 0;
}
}
// Set LED colors by position
for (int i = 0; i < LED_COUNT; i++) {
int ledX = round(dots[i].x);
int ledY = round(dots[i].y);
int ledIndex = ledX + (MATRIX_SIZE - 1 - ledY) * MATRIX_SIZE; // Convert coordinates to LED index
// Set LED color to red
leds[ledIndex] = CRGB::Red; // the change will be here to set custom colors. for now, set to red
}
// Show the updated LED pattern
FastLED.show();
delay(100); // control delay speeds to control animation speed
}
There are no errors during compilation. I have double-checked the connection and used a 5V 2A power source, which is why I want to keep the number of LEDs lit low and also its brightness low.
The accelerometer Vcc is connected to the 3.3V pin on the ESP32 and all components are grounded.
However, the LEDs don't light up and I cannot seem to figure out why. Any help is very much appreciated. If you need any more information, please let me know. If I am in the wrong forum, please redirect me.
Thank you for your time