Pixel Motion
Difficulty: ● ● ○ ○ ○
-
Make Sure You have:
-
Did you know ?
Parts Required
No products found in parts_required metafield
How It Works ?
GENESIS
Connection Guide
Snap your modules as shown above, copy the code into the Arduino IDE and Click upload
#include <Adafruit_NeoPixel.h>
#define DPAD_PIN 2
#define MATRIX_PIN 16
#define MATRIX_SIZE 25
int debounce = 150;
int x = 2, y = 2;
uint32_t currentColor = 0xFF0000;
Adafruit_NeoPixel matrix(MATRIX_SIZE, MATRIX_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
matrix.begin();
matrix.clear();
matrix.show();
randomSeed(analogRead(0));
}
void loop() {
int button = readAnalogButton();
switch(button) {
case 1: // UP
y = max(0, y - 1);
delay(debounce);
break;
case 2: // DOWN
y = min(4, y + 1);
delay(debounce);
break;
case 3: // LEFT
x = max(0, x - 1);
delay(debounce);
break;
case 4: // RIGHT
x = min(4, x + 1);
delay(debounce);
break;
case 5: // CENTER
currentColor = matrix.Color(random(0,256), random(0,256), random(0,256));
delay(debounce);
break;
case 0:
break;
}
displayPixel(x, y, currentColor);
}
int readAnalogButton() {
int val = analogRead(DPAD_PIN);
if (val < 100) return 4; // RIGHT (~0)
else if (val < 900) return 2; // DOWN (~747)
else if (val < 1900) return 1; // UP (~1525)
else if (val < 2600) return 5; // CENTER (~2295)
else if (val < 3500) return 3; // LEFT (~3127)
else return 0;
}
void displayPixel(int x, int y, uint32_t color) {
matrix.clear();
int index = x * 5 + (4 - y); // 90° clockwise rotation mapping
matrix.setPixelColor(index, color);
matrix.show();
}