I'm using an esp32-wroom board. I'm using platformIO in VS Code to program this. The project I'm attempting is this...
I have push button switch wired up the the controller using a pullup resistor wired to +3.3V and the other end going to ground. My ultimate goal is to have multiple switches that play a given animation when the button is pressed and released. I want to animation to continue until I press the button again at which point I want the display to clear. With my current code (posted below) when I press t he button the animation plays 1 frame then stops. Another thing I'd like to have is the ability to set how fast the animation plays via code. I have been around in circles with this code 100 different ways from Sunday and I just can't figure out what I'm doing wrong. Would someone be kind enough to help me out here and maybe give me some clues where I'm going wrong? Also if I'm doing something totally dumb in my code I'd love some direction to get it right.
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <Arduino.h>
#include "animations.h"
// Define the number of devices we have in the chain and the hardware interface
#define MAX_DEVICES 4
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define CLK_PIN 18 // or SCK
#define DATA_PIN 23 // or MOSI
#define CS_PIN 5 // or SS
// Create a new instance of the MD_MAX72XX library
MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
unsigned long previousMillis = 0;
const long interval = 75;
const int button1Pin = 2;
const int button2Pin = 4;
int currentFrame = 0;
void playAnimation1(); // Forward declaration
void buttons(); // Forward declaration
void setup() {
mx.begin();
mx.control(MD_MAX72XX::INTENSITY, 3); // Set intensity level (0-15)
mx.clear();
pinMode(button1Pin, INPUT_PULLUP);
}
void loop() {
buttons();
}
void playAnimation1() {
// Ensure the number of columns does not exceed the array bounds
int numColumns = sizeof(animation_03[0]) / sizeof(animation_03[0][0]);
if (numColumns > 32) {
numColumns = 32; // Limit to 32 columns if the array has more columns
}
for (int i = 0; i < numColumns; i++) {
mx.setColumn(i, animation_03[currentFrame][i]);
}
currentFrame++;
if (currentFrame >= sizeof(animation_03) / sizeof(animation_03[0])) {
currentFrame = 0;
}
}
void buttons() {
#define button1Pressed LOW // Button wired with pullup resistor
uint32_t currentMillis = millis();
static uint32_t lastMillis;
const uint32_t debounceTime = 20;
bool currentButton1State = digitalRead(button1Pin);
static bool lastButton1State;
bool animation1Playing = false;
if (lastButton1State != currentButton1State) {
if (currentMillis - lastMillis >= debounceTime) {
lastButton1State = currentButton1State;
if (currentButton1State == button1Pressed) {
animation1Playing = true;
playAnimation1();
}
}
} else {
animation1Playing = false;
lastMillis = currentMillis;
}
}