Hi All
Been tinkering for a while, used arrays before but come stuck with the following test program. Basically press one of 3 buttons, and want to be told that button (or message) is active.
Regardless of which button is pressed, the output is always 1.
![]()
Would really appreciate someone advising where I've gone wrong as I've stared at this for hours and cant progress!
Thanks
int delayTime = 4000;
int messageActive[3]; // Set array to 1 to show message
const int buttonAPin = 2;
const int buttonBPin = 3;
const int buttonCPin = 4;
bool buttonAState = false;
bool buttonBState = false;
bool buttonCState = false;
bool displayMessage = 0; // If any messages need displaying.
void setup() {
Serial.begin(9600);
// Clear Message Buffer
for (int Indexer = 0; Indexer < 3; Indexer++) {
messageActive[Indexer] = 0;
}
pinMode(buttonAPin, INPUT);
pinMode(buttonBPin, INPUT);
pinMode(buttonCPin, INPUT);
}
void loop() {
displayMessage = 0;
buttonAState = digitalRead(buttonAPin);
buttonBState = digitalRead(buttonBPin);
buttonCState = digitalRead(buttonCPin);
if (buttonAState == true) {
messageActive[0] = true;
} else {
messageActive[0] = false;
}
if (buttonBState == true) {
messageActive[1] = true;
} else {
messageActive[1] = false;
}
if (buttonCState == true) {
messageActive[2] = true;
} else {
messageActive[2] = false;
}
for (int i = 0; i < 3; i++) {
if (messageActive[i] == true) {
Serial.print("There is a Message at : ");
Serial.println(messageActive[i]);
displayMessage = true;
}
}
if (displayMessage == true) {
for (int Indexer = 0; Indexer < 3; Indexer++) {
if (messageActive[Indexer] == 1) {
Serial.print("The following are active : ");
Serial.println(messageActive[Indexer]);
}
}
} // End if
else {
Serial.println("Nothing to display");
}
delay(delayTime);
Serial.println();
}