/*
Arduino Forum
Topics: Button question
Category: Using Arduino
Sub-Category: Programming Questions
Link: https://forum.arduino.cc/t/button-question/1159424/12
*/
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 4 // Define the number of displays connected
#define CLK_PIN 13 // CLK or SCK
#define DATA_PIN 11 // DATA or MOSI
#define CS_PIN 10 // CS or SS
#define buttonPin 2 // the pin that the pushbutton is attached to
#define debouncePeroid 10
// Hardware SPI connection
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// Arbitrary output pins
// MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
// individual messages in strings
const char msg_1[] = "1111";
const char msg_2[] = "2222";
const char msg_3[] = "3333";
const char msg_4[] = "4444";
const char msg_5[] = "5555";
const char msg_6[] = "6666";
// an array of pointers to the strings
char *messages[] = {msg_1, msg_2, msg_3, msg_4, msg_5, msg_6};
uint8_t numberOfMessages = sizeof(messages) / sizeof(messages[0]);
uint8_t msgIndex;
bool enable;
void setup(void) {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
P.begin();
enable = false;
msgIndex = numberOfMessages - 1;
}
void loop(void) {
if (enable & P.displayAnimate()) {
P.displayText(messages[msgIndex], PA_CENTER, 40, 10, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
enable = false;
} else if (isPressed()) {
enable = true;
msgIndex = (msgIndex + 1) % numberOfMessages;
}
}
bool isPressed() {
static bool state = HIGH;
static bool input = HIGH;
static unsigned long startTime;
bool prevInput = input;
input = digitalRead(buttonPin);
if (input != prevInput) {
startTime = millis();
}
if (input != state) {
if (millis() - startTime >= debouncePeroid) {
state = input;
return state == LOW; // When pressed, state = LOW, if pinmode = INPUT_PULLUP, and state = HIGH, if pinmode = INPUT.
}
}
return LOW;
}