I am trying to write a simple program that detects which of my 2 buttons was pressed first. Although I think the code is written correctly, the output doesn't seem to be anything what I would expect.
I am using a simple array of integers to record the "placements" of which button was pressed first and second, as you can see below.
void setup() {
Serial.begin(9600);
pinMode(5, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
}
int location = 0;
int placement[2] = {99, 99};
bool pressed = false;
void loop() {
if (digitalRead(5) == HIGH)
{
Serial.println("RESET BUTTON IS PRESSED");
placement[0] = 99;
placement[1] = 99;
pressed = false;
location = 0;
}
if (digitalRead(7) == HIGH)
{
if (placement[location] == 99)
{
placement[location] = 7;
location++;
pressed = true;
}
}
if (digitalRead(8) == HIGH)
{
if (placement[location] == 99)
{
placement[location] = 8;
location++;
pressed = true;
}
}
if (pressed == true)
{
String text = String(location) + ". place: button n. " + String(placement[location]);
Serial.println(text);
pressed = false;
}
}
What I expect from the program after pushing my 2 buttons nearly at the same time is a simple output in the console:
place = button n. 8
place = button n. 7
If only one button is pressed, then I only expect one line printed in the console. However, even if I only press one button, the console prints 2 lines as seen in the attached picture.
Does anyone know, what could be causing this? This is my first time programming in this "cyclic" way, so if this is some kind of a rookie mistake, I am truly sorry. Thanks to anyone who answers and who takes their time to read this, I really appreciate it.