with a project for a falling blocks game on my neotrellis with my arduino nano esp32 the lights are visibly flickering. here is my code.
////////////////////////////////////////////////////////////
//////////////////// Dodging blocks game ////////////////////
////////////////////////////////////////////////////////////
////////////////////libraries////////////////////
#include "Adafruit_NeoTrellis.h"
#include <BlockNot.h>
////////////////////variables////////////////////
int speedCount = 0; //points and speed variable
byte hundreds; //variables used to display numbers on the screen
byte tens;
byte ones;
constexpr uint32_t RED = 6553600; //color values
constexpr uint32_t GREEN = 25600;
constexpr uint32_t BLUE = 100;
constexpr uint32_t YELLOW = 6579200;
constexpr uint32_t CYAN = 25700;
constexpr uint32_t PURPLE = 6553700;
constexpr uint32_t WHITE = 6579300;
constexpr uint32_t characterColor = PURPLE; //change the color to another to customise the aesthetics
constexpr uint32_t blockColor = CYAN;
constexpr uint32_t numberColor = WHITE;
byte blockPosition[4] = { 16, 16, 16, 16 }; // array varible to handle block positions
bool zero[16] = { 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1 }; // array variables for numbers
bool one[16] = { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0 };
bool two[16] = { 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1 };
bool three[16] = { 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1 };
bool four[16] = { 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0 };
bool five[16] = { 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0 };
bool six[16] = { 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 };
bool seven[16] = { 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0 };
bool eight[16] = { 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
bool nine[16] = { 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1 };
////////////////////initializing code////////////////////
BlockNot timer(1000); // timer set to one second per block falling, this gets faster as the game progresses
Adafruit_NeoTrellis trellis;
////////////////////define a callback for key presses////////////////////
TrellisCallback blink(keyEvent evt) {
// Check is the pad pressed?
if (evt.bit.EDGE == SEESAW_KEYPAD_EDGE_RISING) { // if button was just pressed
Serial.println("BUTTON PRESS");
//////////////////// character movement ////////////////////
enum { left = -1,
neither,
right } dir = [](int NUM) {
switch (NUM) {
case 12: return left;
case 15: return right;
default: return neither;
}
}(evt.bit.NUM);
if (dir) {
int current = 0;
for (int p = 12; p <= 15; p++) {
if (trellis.pixels.getPixelColor(p) == characterColor) {
current = p;
break;
}
}
if (current) {
int next = current + dir;
if (next < 12) {
next = 15;
} else if (next > 15) {
next = 12;
}
if (trellis.pixels.getPixelColor(next) != blockColor) {
trellis.pixels.setPixelColor(current, 0);
trellis.pixels.setPixelColor(next, characterColor);
trellis.pixels.show();
Serial.println("MOVE");
} else {
Serial.println("CANT MOVE ON TO YELLOW");
}
} else {
Serial.println("MOVE ERROR");
}
}
} else if (evt.bit.EDGE == SEESAW_KEYPAD_EDGE_FALLING) {
// or is the pad released?
return 0;
}
}
////////////////////setup////////////////////
void setup() {
Serial.begin(115200);
if (!trellis.begin()) {
Serial.println("Could not start trellis, check wiring?");
while (1) delay(1);
} else {
Serial.println("NeoPixel Trellis started");
}
//activate all keys and set callbacks
for (int i = 0; i < NEO_TRELLIS_NUM_KEYS; i++) {
trellis.activateKey(i, SEESAW_KEYPAD_EDGE_RISING);
trellis.activateKey(i, SEESAW_KEYPAD_EDGE_FALLING);
trellis.registerCallback(i, blink);
}
//do a little animation to show we're on
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
trellis.pixels.setPixelColor(i, Wheel(map(i, 0, trellis.pixels.numPixels(), 0, 255)));
trellis.pixels.show();
delay(50);
}
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
trellis.pixels.setPixelColor(i, 0x000000);
trellis.pixels.show();
delay(50);
}
trellis.pixels.setPixelColor(12, characterColor); //display the character
trellis.pixels.show();
timer.start(WITH_RESET); // start the timer
}
////////////////////loop////////////////////
void loop() {
// put your main code here, to run repeatedly:
if (speedCount >= 1000) { // if you win
for (int i = 0; i < NEO_TRELLIS_NUM_KEYS; i++) { //turn off buttons
trellis.activateKey(i, SEESAW_KEYPAD_EDGE_RISING, false);
trellis.activateKey(i, SEESAW_KEYPAD_EDGE_FALLING, false);
}
for (uint16_t i = 0; i < 150; i++) { // make a fun animation
trellis.pixels.setPixelColor(random(0, 16), Wheel(random(0, 256)));
trellis.pixels.show();
delay(10);
}
blockPosition[0] = 16; // reset the block variable
blockPosition[1] = 16;
blockPosition[2] = 16;
blockPosition[3] = 16;
timer.setDuration(1000);
hundreds = (speedCount / 100) % 10; // calculate the numbers for the display. if the score is exactly 1000 it will show 000 on the screen. if it is higher than 1000 it will show how much higher than 1000 it is.
tens = (speedCount / 10) % 10;
ones = speedCount % 10;
updateNumbers(); // show the numbers
speedCount = 0; // reset score
for (int i = 0; i < NEO_TRELLIS_NUM_KEYS; i++) { //turn buttons back on
trellis.activateKey(i, SEESAW_KEYPAD_EDGE_RISING);
trellis.activateKey(i, SEESAW_KEYPAD_EDGE_FALLING);
trellis.registerCallback(i, blink);
}
trellis.pixels.setPixelColor(12, characterColor);
trellis.pixels.show();
timer.start(WITH_RESET);
}
trellis.read(); // update the buttons
if (timer.triggered()) { //if the timer to make the blocks fall triggers
speedCount = speedCount + 10; // increment the score. this number can be customised to change how fast the game changes from fast to slow.
timer.setDuration(1000 - speedCount); // make the timer faster
Serial.println(speedCount);
//////////////////// block movement code ////////////////////
if (blockPosition[3] != 16 && trellis.pixels.getPixelColor(blockPosition[3]) != characterColor) { // delete the pixel on the bottom liner
trellis.pixels.setPixelColor(blockPosition[3], 0);
}
if (blockPosition[2] != 16) { //move the pixel from the second to bottom line to the bottom line
blockPosition[3] = blockPosition[2] + 4;
if (trellis.pixels.getPixelColor(blockPosition[3]) == characterColor) { // if a block falls into the character you lose
for (int i = 0; i < NEO_TRELLIS_NUM_KEYS; i++) { //turn off buttons
trellis.activateKey(i, SEESAW_KEYPAD_EDGE_RISING, false);
trellis.activateKey(i, SEESAW_KEYPAD_EDGE_FALLING, false);
}
Serial.println("lose");
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
trellis.pixels.setPixelColor(i, Wheel(map(i, 0, trellis.pixels.numPixels(), 60, 100)));
trellis.pixels.show();
delay(50);
}
blockPosition[0] = 16; // reset block variables
blockPosition[1] = 16;
blockPosition[2] = 16;
blockPosition[3] = 16;
timer.setDuration(1000); // reset the timer
hundreds = (speedCount / 100) % 10; // calculate numbers to be displayed
tens = (speedCount / 10) % 10;
ones = speedCount % 10;
updateNumbers(); //update the display
speedCount = 0; // reset the score
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) { // losing animation
trellis.pixels.setPixelColor(i, Wheel(map(i, 0, trellis.pixels.numPixels(), 60, 100)));
trellis.pixels.show();
delay(50);
}
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
trellis.pixels.setPixelColor(i, 0x000000);
trellis.pixels.show();
delay(50);
}
for (int i = 0; i < NEO_TRELLIS_NUM_KEYS; i++) { //turn buttons on again
trellis.activateKey(i, SEESAW_KEYPAD_EDGE_RISING);
trellis.activateKey(i, SEESAW_KEYPAD_EDGE_FALLING);
trellis.registerCallback(i, blink);
}
trellis.pixels.setPixelColor(12, characterColor);
trellis.pixels.show();
timer.start(WITH_RESET);
} else {
trellis.pixels.setPixelColor(blockPosition[3], blockColor);
trellis.pixels.setPixelColor(blockPosition[2], 0);
blockPosition[2] = 16;
}
}
if (blockPosition[1] != 16) { //move the pixel from the second to tope line to the second to bottom line
blockPosition[2] = blockPosition[1] + 4;
trellis.pixels.setPixelColor(blockPosition[2], blockColor);
trellis.pixels.setPixelColor(blockPosition[1], 0);
blockPosition[1] = 16;
}
if (blockPosition[0] != 16) { //move the pixel from the top line to the second to top line
blockPosition[1] = blockPosition[0] + 4;
trellis.pixels.setPixelColor(blockPosition[1], blockColor);
trellis.pixels.setPixelColor(blockPosition[0], 0);
blockPosition[0] = 16;
}
blockPosition[0] = random(0, 4); // make a new pixel on the top line
trellis.pixels.setPixelColor(blockPosition[0], blockColor);
trellis.pixels.show();
}
}
////////////////////color calculator////////////////////
uint32_t Wheel(byte WheelPos) {
if (WheelPos < 85) {
return trellis.pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if (WheelPos < 170) {
WheelPos -= 85;
return trellis.pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return trellis.pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
return 0;
}
////////////////////update screen function////////////////////
void updateNumbers() {
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
trellis.pixels.setPixelColor(i, 0x000000);
trellis.pixels.show();
delay(30);
}
Serial.println("HUNDREDS");
switch (hundreds) { // display the hundreds digit
case 0:
Serial.println(0);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (zero[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 1:
Serial.println(1);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (one[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 2:
Serial.println(2);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (two[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 3:
Serial.println(3);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (three[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 4:
Serial.println(4);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (four[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 5:
Serial.println(5);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (five[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 6:
Serial.println(6);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (six[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 7:
Serial.println(7);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (seven[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 8:
Serial.println(8);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (eight[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 9:
Serial.println(9);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (nine[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
default:
Serial.println("HUNDREDS ERROR");
break;
}
delay(2000);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
trellis.pixels.setPixelColor(i, 0x000000);
trellis.pixels.show();
delay(30);
}
Serial.println("TENS");
switch (tens) { // display the tens digit
case 0:
Serial.println(0);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (zero[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 1:
Serial.println(1);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (one[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 2:
Serial.println(2);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (two[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 3:
Serial.println(3);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (three[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 4:
Serial.println(4);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (four[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 5:
Serial.println(5);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (five[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 6:
Serial.println(6);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (six[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 7:
Serial.println(7);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (seven[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 8:
Serial.println(8);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (eight[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 9:
Serial.println(9);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (nine[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
default:
Serial.println("TENS ERROR");
break;
}
delay(2000);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
trellis.pixels.setPixelColor(i, 0x000000);
trellis.pixels.show();
delay(30);
}
Serial.println("ONES");
switch (ones) { // display the ones digit
case 0:
Serial.println(0);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (zero[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 1:
Serial.println(1);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (one[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 2:
Serial.println(2);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (two[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 3:
Serial.println(3);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (three[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 4:
Serial.println(4);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (four[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 5:
Serial.println(5);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (five[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 6:
Serial.println(6);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (six[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 7:
Serial.println(7);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (seven[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 8:
Serial.println(8);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (eight[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
case 9:
Serial.println(9);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
if (nine[i] == 1) {
trellis.pixels.setPixelColor(i, numberColor);
}
}
trellis.pixels.show();
break;
default:
Serial.println("ONES ERROR");
break;
}
delay(2000);
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
trellis.pixels.setPixelColor(i, 0x000000);
trellis.pixels.show();
delay(30);
}
}
does the arduino not supply enough power?