In my code I am trying to check the elapsed time after button press. Idea is, if the button is pressed once, it will show 'Level 1' and if the button is pressed again with in 5 seconds, it will move to Level 2, else it will reset the button count to 0.
I took help from the examples and some other posts. I am getting button counts right but somehow press counter is not resetting to 0 after 5 seconds. Wondering what am I doing wrong here.
#include <FastLED.h>
#define NUM_LEDS 60
#define DATA_PIN 12
CRGB leds[NUM_LEDS];
const uint8_t buttonPin[] = {2, 3}; // Inputs for buttons. On many Arduinos pins 0 and 1 are used for serial making pin 2 the first free pin.
const uint8_t buttonCount = 2; // Number of buttons.
uint8_t pressCount = 0;
bool buttonPressedFlag[buttonCount]; // Flags to indicate that a button has been detected as pressed and debounced
typedef void (* functionPtrs) (void);
functionPtrs functions[2] = {&buttonZero, &buttonOne}; // Pointers to the above 2 functions, this is how a different function is selected depending on which button is pressed
void setup() {
FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
FastLED.clear();
FastLED.show();
Serial.begin(9600); // Start the serial monitor to see the results.
char fileName[] = {__FILE__};
Serial.println(fileName); // Prints the name of the file the location this sketch is in.
for (uint8_t i = 0; i < buttonCount; ++i) {
pinMode(buttonPin[i], INPUT_PULLUP); // Make the button pins inputs with the internal pull up resistor enabled.
}
}
void loop() {
Buttons(); // Calls the function to read the button.
doStuff(); // Does something in response to the input
// Your other code goes here, make sure it is non blocking (no delays, no loops that don't exit quickly)
}
/* This is the function that reads the state of the button, debounces it then sets a flag for the separate doStuff()function to do something */
void Buttons() {
#define buttonPressed LOW // When the button is pressed the input will be low, this is to remove the confusion this migth cause.
uint32_t currentMillis = millis(); // Millis times uses to debounce the button
static uint32_t lastMillis[buttonCount]; // Start of the debounce timeout for each button
const uint32_t bounceTimeout = 20; // Debounce time in milliseconds
bool currentButtonState[buttonCount]; // Holds the current state of each button
static bool lastButtonState[buttonCount]; // Holds the previous debounced state of the button
uint8_t i;
for (i = 0; i < buttonCount; ++i) {
currentButtonState[i] = digitalRead(buttonPin[i]); // Reads the current state of each button and saves the result
if (lastButtonState[i] != currentButtonState[i]) { // Checks to see if each button has been pressed or released, at this point each button has not been debounced
if (currentMillis - lastMillis[i] >= bounceTimeout) { // Checks to see if the state of each button has been stable for at least bounceTimeout duration
lastButtonState[i] = currentButtonState[i]; // At this point the button has been debounced, so save the last state
if (currentButtonState[i] == buttonPressed) { // The button might have been pressed or released, this make sure only presses are acted on, not releases
buttonPressedFlag[i] = true; // Button press has been detected and debounced, set a flag to indicate to the next function that some action can be taken
}
}
} else {
lastMillis[i] = currentMillis; // Saves the current value of millis in last millis so the debounce timer for each button starts from current millis
}
}
}
/* Selects one of the 2 functions buttonZero() etc depending on which button was pressed */
void doStuff() {
uint8_t i;
for (i = 0; i < buttonCount; ++i) {
if (buttonPressedFlag[i]) {
buttonPressedFlag[i] = false; // Clear the flag to ensure the action only happens once
pressCount++;
functions[i](); // Calls one of the 2 functions depending on which button was pressed
}
}
}
// 2 functions, which are called depending on which button is pressed
void buttonZero() {
uint32_t currentTime;
uint32_t startTime = millis();
uint32_t elapsedTime;
const uint32_t timeOut = 5000;
if (pressCount == 1){
currentTime = millis();
Serial.println ("Level 1");
elapsedTime = currentTime - startTime;
if (elapsedTime >= timeOut) {
Serial.println ("Resetting");
pressCount = 0;
}
}
else if (pressCount == 2){
Serial.println ("Level 2");
}
else if (pressCount > 2){
Serial.println ("Resetting");
pressCount = 0;
}
}
void buttonOne() {
}