Hello,
check out the proposal for your school assignement.
/* BLOCK COMMENT
ATTENTION: This Sketch contains elements of C++.
https://www.learncpp.com/cpp-tutorial/
https://forum.arduino.cc/t/detecting-third-button-press/919620
*/
#define ProjectName "Detecting Third button press"
// hardware and timer settings
constexpr byte ButtonOnePin {A0}; // portPin o---|button|---GND
constexpr byte ButtonTwoPin {A1}; // portPin o---|button|---GND
constexpr byte ButtonThreePin {A2}; // portPin o---|button|---GND
constexpr byte Led2Pow0Pin {2}; // portPin o---|220|---|LED|---GND
constexpr byte Led2Pow1Pin {3}; // portPin o---|220|---|LED|---GND
constexpr byte Led2Pow2Pin {4}; // portPin o---|220|---|LED|---GND
constexpr byte Led2Pow3Pin {5}; // portPin o---|220|---|LED|---GND
unsigned long BlinkDuration {1000}; // times in msec
// CONSTANT DEFINITION
// forward declaration using function prototype
void blinkBlink();
void buttonOnePressed();
void buttonOneReleased();
void buttonTwoPressed();
void buttonTwoReleased();
void buttonThreePressed();
void buttonThreeReleased();
// VARIABLE DECLARATION AND DEFINITION
unsigned long currentTime;
struct TIMER { // has the following members
unsigned long duration; // memory for interval time
bool repeat_; // control for blinking
bool control_; // control for start/stop
unsigned long stamp; // memory for actual time
};
struct BUTTON {
byte pin;
bool statusQuo;
void (*task[2])();
unsigned long stamp;
unsigned long duration;
} buttons[] {
{ButtonOnePin, false, {buttonOneReleased, buttonOnePressed}, 0, 20},
{ButtonTwoPin, false, {buttonTwoReleased, buttonTwoPressed}, 0, 20},
{ButtonThreePin, false, {buttonThreeReleased, buttonThreePressed}, 0, 20},
};
struct BOX {
byte leds[4];
int counter;
TIMER timer;
} box {{Led2Pow0Pin, Led2Pow1Pin, Led2Pow2Pin, Led2Pow3Pin}, 0, BlinkDuration, true, false, 0};
// ------------------ USER FUNCTIONS ---------------
bool checkTimer(TIMER & time_) { // generic time handler using TIME struct
if (currentTime - time_.stamp >= time_.duration && time_.control_) {
if (time_.repeat_) time_.stamp = currentTime;
else time_.control_ = false;
return true;
} else return false;
}
void startTimer(TIMER &timer) {
timer.control_ = true;
timer.stamp = currentTime;
}
void stoppTimer(TIMER &timer) {
timer.control_ = false;
}
void checkButton() {
for (auto &button_ : buttons) {
if (currentTime - button_.stamp >= button_.duration) {
button_.stamp = currentTime;
bool stateNew = !digitalRead(button_.pin);
if (button_.statusQuo != stateNew) {
button_.statusQuo = stateNew;
button_.task[stateNew]();
}
}
}
}
void blinkBlink() {
box.counter++;
}
void upDate() {
int element = 1;
for (auto led : box.leds) digitalWrite (led, (box.counter & element ? 1 : 0)), element *= 2;
}
void buttonOnePressed() {
box.counter++;
}
void buttonOneReleased() {
}
void buttonTwoPressed() {
startTimer(box.timer);
}
void buttonTwoReleased() {
}
void buttonThreePressed() {
stoppTimer(box.timer);
box.counter=0;
}
void buttonThreeReleased() {
}
void setup() {
Serial.begin(9600);
Serial.println(F("."));
Serial.print(F("File : ")), Serial.println(__FILE__);
Serial.print(F("Date : ")), Serial.println(__DATE__);
Serial.print(F("Project: ")), Serial.println(ProjectName);
pinMode (LED_BUILTIN, OUTPUT); // used as heartbeat indicator
// https://www.learncpp.com/cpp-tutorial/for-each-loops/
for (auto Button : buttons) pinMode(Button.pin, INPUT_PULLUP);
for (auto Output : box.leds) pinMode(Output, OUTPUT);
// check outputs
for (auto Output : box.leds) digitalWrite(Output, HIGH), delay(100);
for (auto Output : box.leds) digitalWrite(Output, LOW), delay(100);
}
void loop () {
currentTime = millis();
digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
if (checkTimer(box.timer)) blinkBlink();
checkButton();
upDate();
}
Have a nice day and enjoy coding in C++.