// extend this to text, tetris, snake, pong, LDR spectrum
// https://www.instructables.com/Arduino-based-Bi-color-LED-Matrix-Flappy-Bird-Game/
// https://forum.arduino.cc/t/how-to-make-a-flappy-bird-game-on-an-8x8-led-matrix-with-max7219-driver/1401903
#include <LedControl.h>
#define data 12
#define clock 11
#define chipSelect 10
#define matrixCount 1
LedControl lc = LedControl(data, clock, chipSelect, matrixCount);
int buttonPin[] = {2, 3, 4, 5};
int buttonSize = sizeof(buttonPin) / sizeof(buttonPin[0]);
int buttonState[4], buttonStateOld[4];
unsigned long timer, timeout = 500;
unsigned long buttonTimer, buttonTimeout = 100;
void setup() {
Serial.begin(115200);
for (byte i = 0; i < buttonSize; i++)
pinMode(buttonPin[i], INPUT_PULLUP);
lc.clearDisplay(0);
}
void loop() {
readButtons();
validateButtonPress();
}
void fly() {
}
void crash() {
}
void movepipe() {
}
void win() {
}
void readButtons() {
for (int i = 0; i < buttonSize; i++) {
buttonState[i] = digitalRead(buttonPin[i]);
}
}
void validateButtonPress() {
for (int i = 0; i < buttonSize; i++) { // step through button pins
if (buttonState[i] != buttonStateOld[i]) { // if button changed (not equal)...
if ((buttonStateOld[i] == 1) && (buttonState[i] == 0)) { // if button changes released to pressed...
if (millis() - buttonTimer > buttonTimeout) { // ... and time since last press has timed out...
buttonTimer = millis(); // store new timer for next button press.
printButtons(); // do something
}
}
buttonStateOld[i] = buttonState[i];
}
}
}
void printButtons() {
for (int i = 0; i < buttonSize; i++) {
Serial.print(buttonState[i]);
}
Serial.println();
}