The program below is working OK. Originally all my main code was in loop(). I revised it following recommendations I've seen here to confine as much as possible to functions. But I'm wondering if I should also have placed most variables in the functions? Or even in loop()? Or are they OK left in the globals section?
I'd welcome any other advice on improving my code. 'Arduino level' please, no advanced C/C++!
That includes a pointer to any program or library for achieving the above. I failed to find any that got close, after much prior searching. But the millis() tutorials by UKHeliBob and Robin2 helped a lot, many thanks.
// Tuesday 31 May 2022; simplified 'action' for testing
// After pressing button A, if button B is pressed within set time
// then take some action.
// Intended for music box, hoping to increase possible button actions.
const unsigned long period = 1500; // max period allowed
const byte buttonAPin = A1; // First button
const byte buttonBPin = A2; // Second button
const byte blueLEDPin = 11; // Test: Lights briefly on correct logic.
bool preventingA = false; // Flag, true after A pressed, false after action.
bool preventingB = false; // Flag, true after B pressed, false after action.
// In both cases, false prevents timestamp captures, true allows them.
unsigned long currentMillis;
unsigned long AMillis;
unsigned long BMillis;
/////////////////////////////////////////
void setup()
{
Serial.begin(115200);
Serial.println("TwoButnFnForMB-2-TryFunctions");
pinMode(buttonAPin, INPUT_PULLUP);
pinMode(buttonBPin, INPUT_PULLUP);
pinMode(blueLEDPin, OUTPUT);
digitalWrite(blueLEDPin, LOW); // Set LED off; probably redundant
}
/////////////////////////////////////////
void loop()
{
getTimeA();
getTimeB();
action();
}
/////////////////////////////////////////
void getTimeA()
{
// Get time button A pressed
currentMillis = millis(); // Capture latest time
// It's then the same for all variables
// Read ButtonA to get its timestamp; actually several will
// be read, while button is low, and the last will get used.
if (digitalRead(buttonAPin) == LOW && (preventingA == false) )
// if (digitalRead(buttonAPin) == LOW && (preventingA == LOW) )
{
AMillis = currentMillis; // Capture time of press.
preventingA = true; // To stop further capturing.
}
}
/////////////////////////////////////////
void getTimeB()
{
// Get time button B pressed; function getTimeB()
if (digitalRead(buttonBPin) == LOW && (preventingB == false) )
{
BMillis = currentMillis;
preventingB = true; // To stop further B capturing
// Serial.print("******* BMillis = ");
// Serial.println(BMillis);
Serial.print("B-A = which is within the time allowed");
Serial.println(BMillis - AMillis);
}
}
/////////////////////////////////////////
void action()
{
// Take action if B is pressed within time allowed
if (preventingA == true && preventingB == true
&& (BMillis - AMillis <= period))
{
digitalWrite(blueLEDPin, HIGH);
delay(2000); // Not critical enough to use millis()
digitalWrite(blueLEDPin, LOW);
preventingA = LOW; // Reset these for next usage
preventingB = LOW;
}
}