Three buttons and three different LED configurations - code issues

Dear all
I have used the boards to solve all my problems so far, but I am stumped by my latest. I suspect I have taken my coding beyond my ability. What I am trying to do is hook up a three button board (built in resistors) and an 8 LED board (with resistors again) to 11 digital pins on my Arduino.
What I am trying to do is activate three different LED flashing patterns to the three buttons:
Button 1 is all LEDs on.
Button 2 is LEDs coming on in turn from low to high, then fading off in turn the opposite direction.
Button 3 is the exact opposite of Button 2

What I get is button 2 and 3 working as I expected, but when button 1 is pressed I get the second half of the code for button 2. I have toyed with a different way of coding the button states, but I managed to get all the buttons to not work at all, so this is the ‘best’ code I have found so far.
Incidentally, when I hooked up button 1 just by itself the 8 LEDs all came on and stayed on while the button was pressed, using just that portion of the code. It was when I added the button 2 and 3 code that it stopped working.

Here is the code:

//Turns on and off multiple LEDs ,when pressing button attach to pin10, pin11 and pin12

/**********************************/
const int keyPin1 = 10; //the number of the key pin
const int keyPin2 = 11;
const int keyPin3 = 12;
const int ledPin2 = 2;//the number of the led pins
const int ledPin3 = 3;
const int ledPin4 = 4;
const int ledPin5 = 5;
const int ledPin6 = 6;
const int ledPin7 = 7;
const int ledPin8 = 8;
const int ledPin9 = 9;
const int lowestPin = 2;//the lowest LED pin attached to
const int highestPin = 9;//the highest LED pin attach to 
// variables will change:
int keyPinState1 = 0;         // variable for reading the pushbuttons statuses
int keyPinState2 = 0;
int keyPinState3 = 0;
/**************************************/
void setup()
{ 
  //set pins 2 through 9 as output  
  for(int thisPin = lowestPin;thisPin <= highestPin;thisPin++)
  {
  pinMode(thisPin,OUTPUT); //initialize thisPin as an output
  }
  {
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(ledPin8, OUTPUT);
  pinMode(ledPin9, OUTPUT);
  pinMode(keyPin1, INPUT);//initialize the key pin as input 
  pinMode(keyPin2, INPUT);
  pinMode(keyPin3, INPUT);
  }
}
/**********************************/
void loop()
{
   if(digitalRead(keyPin1) ==HIGH && digitalRead(keyPin2) ==LOW && digitalRead(keyPin3) ==LOW)// if we're pushing button 1 
  {
   digitalWrite(ledPin2, HIGH);//turn on the leds
   digitalWrite(ledPin3,HIGH);
   digitalWrite(ledPin4,HIGH);
   digitalWrite(ledPin5,HIGH);
   digitalWrite(ledPin6,HIGH);
   digitalWrite(ledPin7,HIGH);
   digitalWrite(ledPin8,HIGH);
   digitalWrite(ledPin9,HIGH);
  }
else
{
    digitalWrite(ledPin2,LOW);//turn off the leds
    digitalWrite(ledPin3,LOW);
    digitalWrite(ledPin4,LOW);
    digitalWrite(ledPin5,LOW);
    digitalWrite(ledPin6,LOW);
    digitalWrite(ledPin7,LOW);
    digitalWrite(ledPin8,LOW);
    digitalWrite(ledPin9,LOW);
}

  //iterate over the pins
  //turn the led on from lowest to the highest
  if(digitalRead(keyPin2) ==HIGH ) // if we're pushing button 2
  
  for(int thisPin = lowestPin;thisPin <= highestPin;thisPin++)
  {
    digitalWrite(thisPin,HIGH);//turn this led on
    delay(50);//wait for 50 microseconds
    //fade from the highest to the lowest
  }
  for(int thisPin = highestPin;thisPin>=lowestPin;thisPin--)
  {
    digitalWrite(thisPin,LOW);//turn this led off
    delay(50);//wait for 50 microseconds
  }
       //iterate over the pins
  //turn the led on from highest to the lowest

  if(digitalRead(keyPin3) ==HIGH )// if we're pushing button 3
  
  for(int thisPin = highestPin;thisPin>=lowestPin;thisPin--)                                                       
  {
    digitalWrite(thisPin,HIGH);//turn this led on
    delay(50);//wait for 50 microseconds
  }
  for(int thisPin = lowestPin;thisPin <= highestPin;thisPin++)
  {
    digitalWrite(thisPin,LOW);//turn this led off
    delay(50);//wait for 50 microseconds
  }
} 
/************************************/

Forgive the coding longhand - I am just three weeks into owning an arduino!
Many thanks for any help you can give.
Sam

Best way to troubleshoot this kind of thing is to use Serial.print to a) print out the values of pins so you can see if they are as expected and b) print out at an “I am here” sort of message in the if various legs of ifs and elses, so you can see where the logic is taking you based on the pin values.

    delay(50);//wait for 50 microseconds

That's 50 milliseconds.

You're just missing some brackets {}. Format with control-T and the problem will be obvious from the indentation.

Dear all
Thanks for your suggestions. The 50 microsecond thing was just in the comments - have amended to millisecond, but of course it makes no difference. I have hunted high and low for bracket issues, and I really can't see any. I have tried adding some more in, but then I get compiling errors (as I don't really know what I am doing!). I have a feeling though I need to partition some bits of code off from what happens when button 1 is pressed.
I have changed the orientation of the button module, and the problem was still on the new button one, so definitely coding issue. I changed the LED module (same result). I have tried with the serial monitor, and as far as I can tell, it is doing what the code is telling it to do, I just don't know how to tell it to do something different!
As button 1 does switch all the leds on, but then switches them off in the style of the second half of button 2's code, I deleted that little bit of code to see what would happen. Button 3 did what it should, button 2 just repeated the first half of its code, and button 1 did the first half of button 2's code but twice as quickly as button 2! I am sure this all means something, but if it is a couple of brackets, can someone point me in the right direction please?
Many thanks
Sam

Actually, I have changed what I want the LEDs to do now. I want them to flash on and off with button 1 pressed now, and this code works!

//Turns on and off multiple LEDs ,when pressing button attach to pin10, pin11 and pin12

/**********************************/
const int keyPin1 = 10; //the number of the key pin
const int keyPin2 = 11;
const int keyPin3 = 12;
const int ledPin2 = 2;//the number of the led pins
const int ledPin3 = 3;
const int ledPin4 = 4;
const int ledPin5 = 5;
const int ledPin6 = 6;
const int ledPin7 = 7;
const int ledPin8 = 8;
const int ledPin9 = 9;
const int lowestPin = 2;//the lowest LED pin attached to
const int highestPin = 9;//the highest LED pin attach to
// variables will change:
int keyPin1State = 0;         // variable for reading the LED statuses
int keyPin2State = 0;
int keyPin3State = 0;


/**************************************/
void setup()
{
  //set pins 2 through 9 as output
  for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++)
  {
    pinMode(thisPin, OUTPUT); //initialize thisPin as an output
  }
    pinMode(ledPin2, OUTPUT); //initialise ledPins as outputs
    pinMode(ledPin3, OUTPUT);
    pinMode(ledPin4, OUTPUT);
    pinMode(ledPin5, OUTPUT);
    pinMode(ledPin6, OUTPUT);
    pinMode(ledPin7, OUTPUT);
    pinMode(ledPin8, OUTPUT);
    pinMode(ledPin9, OUTPUT);
    pinMode(keyPin1, INPUT);//initialize the key pin as input
    pinMode(keyPin2, INPUT);
    pinMode(keyPin3, INPUT);
}
/**********************************/
void loop()
{
  if (digitalRead(keyPin1) == HIGH)  // if we're pushing button 1, flash the leds
  {
    digitalWrite(ledPin2, HIGH);//turn on the leds
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin5, HIGH);
    digitalWrite(ledPin6, HIGH);
    digitalWrite(ledPin7, HIGH);
    digitalWrite(ledPin8, HIGH);
    digitalWrite(ledPin9, HIGH);
    delay(250);
    digitalWrite(ledPin2, LOW);//turn off the leds
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, LOW);
    digitalWrite(ledPin6, LOW);
    digitalWrite(ledPin7, LOW);
    digitalWrite(ledPin8, LOW);
    digitalWrite(ledPin9, LOW);
    delay(50);
  }
  else
  {
    digitalWrite(ledPin2, LOW); //turn off the leds
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, LOW);
    digitalWrite(ledPin6, LOW);
    digitalWrite(ledPin7, LOW);
    digitalWrite(ledPin8, LOW);
    digitalWrite(ledPin9, LOW);
  }
  //iterate over the pins
  //turn the led on from lowest to the highest
  if (digitalRead(keyPin2) == HIGH ) // if we're pushing button 2

    for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++)
    {
      digitalWrite(thisPin, HIGH); //turn this led on
      delay(50);//wait for 50 milliseconds        
    }
    //fade from the highest to the lowest
  for (int thisPin = highestPin; thisPin >= lowestPin; thisPin--)
  {
    digitalWrite(thisPin, LOW); //turn this led off
    delay(50);//wait for 50 milliseconds
  }
  //iterate over the pins
  //turn the led on from highest to the lowest
  if (digitalRead(keyPin3) == HIGH ) // if we're pushing button 3

    for (int thisPin = highestPin; thisPin >= lowestPin; thisPin--)
    {
      digitalWrite(thisPin, HIGH); //turn this led on
      delay(50);//wait for 50 milliseconds
    }
  for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++)
  {
    digitalWrite(thisPin, LOW); //turn this led off
    delay(50);//wait for 50 milliseconds
  }
}
/************************************/

I may never know why I couldn’t get them to come on and stay on, but at least I can go to bed knowing I did program it to do what I wanted it to do this time! :slight_smile:
Thanks for your time

Learn about arrays. To do this thing, you would put the pin numbers in an array and then loop over that array to turn the LEDs off and on.

After you learn about arrays, learn about shift registers - they are a chip that you can get for five bucks, that allow you to use one pin to control a number of digital outs.

PaulMurrayCbr:
Learn about arrays. To do this thing, you would put the pin numbers in an array and then loop over that array to turn the LEDs off and on.

After you learn about arrays, learn about shift registers - they are a chip that you can get for five bucks, that allow you to use one pin to control a number of digital outs.

Thanks Paul. I think I bit off more than I could chew! I am going back to Blink and Arduino for Dummies! :slight_smile: