Problem with Arrays

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.

image

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();
}

Welcome to the forum

How are the inputs wired ?
Have you got any pulldown resistors keeping them at a known state at all times or are they floating at an unknown voltage, could be HIGH, could be LOW, when the button is not pressed ?

Um... no.
I think you want just this:

    if (messageActive[i] == true) {
      Serial.print("There is a Message at : ");
      Serial.println(i);
      displayMessage = true;
    }

What do you expect to print else than 1

and the value of it

If you want to print which button is pressed

to

Blimey! what quick responses. Thanks all

Daft mistake, thanks AresXT. All sorted :slight_smile:

Programming problems aside, my question still stands

Consider using INPUT_PULLUP in the pinMode()s to turn on the built in pullup resistors and change the program logic and circuit to match and detect LOW as a button press

As an aside, whilst it works, it is more obvious what is going on if you test input states as HIGH or LOW rather than true or false

If you want something very obvious to test for then #define the pressed/not pressed values as PRESSED and NOT_PRESSED and test for them instead

Thanks Bob.

Other projects I've used INPUT_PULLUP's as you've mentioned, on this occasion I've used resistors on the breadboard, so they are all pulled down.

Other comments noted - appreciated.