Code does strange things

I’m trying to make a function that reads an analog pin and set a slot in the array to low or high based on the reading. So if the reading is between 400 and 600 slot number 5 would be set to high and the rest would be set to low. if it was between 225 and 300 slot number 2 would be set to high and the rest low etc.

When i print out the pieces of the array individually i get the reading i was looking for. When i print out all the slots together the last slot jumps between 4 and 5. And if i throw a delay in there the program stops printing completely for some reason. :o

void setup() {
  Serial.begin(9600);
}

byte buttonValue[5];

void readKeypad() {
  int keypadValue = analogRead(A2);
 
  if (keypadValue > 50 && keypadValue < 140) {
    buttonValue[0] = HIGH;
  }
  else{
    buttonValue[0] = LOW;
  }
  if (keypadValue > 140 && keypadValue < 180) {
    buttonValue[1] = HIGH;
  }
  else{
    buttonValue[1] = LOW;
  }
  if (keypadValue > 180 && keypadValue < 225) {
    buttonValue[2] = HIGH;
  }
  else{
    buttonValue[2] = LOW;
  }
  if (keypadValue > 225 && keypadValue < 300) {
    buttonValue[3] = HIGH;
  }
  else{
    buttonValue[3] = LOW; 
  }
  if (keypadValue > 300 && keypadValue < 400) {
    buttonValue[4] = HIGH;
  }
  else{
    buttonValue[4] = LOW;
  }
  if (keypadValue > 400 && keypadValue < 600) {
    buttonValue[5] = HIGH;
  }
  else{
    buttonValue[5] = LOW;
  }
}

void loop() {
  readKeypad();
  Serial.print(buttonValue[0]);
  Serial.print(buttonValue[1]);
  Serial.print(buttonValue[2]);
  Serial.print(buttonValue[3]);
  Serial.print(buttonValue[4]);
  Serial.print(buttonValue[5]);
  Serial.println();
}

byte buttonValue[5];``buttonValue[5] = HIGH;
Oops

Thank you sir. you have been of great help :slight_smile: :slight_smile: :slight_smile: