Turning on two buttons consecutively

Hi There,

I am very new to arduino programming specifically and electronics in general, but I have been doing my best to read up on it with books and tutorials on the internet.

I am working on a project wherein I would like to turn on a series on solenoid valves via relays with indicator LEDs to show which valve is currently open. I have been able to achieve this pretty quickly after slightly altering code I found in Jeremy Blum's 'Exploring Arduino'. When I try to add a second button and corresponding LED however, it works, but only after the first has shut off. In theory, I would like to be able to run down a line of momentary buttons to turn on the corresponding LEDs and trigger their respective relays.

Again, I'm still learning, so apologies if this is an extremely silly problem, but I'm at a bit of a dead end... or rather don't know how to ask google the right question.

Thank you. Code is copy & pasted below.

This is an early sketch for a button based irrigation system
*/

const int LED8=8; //The LED is connected to pin 8
const int BUTTON2=2; //The Button is connected to pin 2
const int LED9=9; //The LED is connected to pin 9
const int BUTTON3=3; //The Button is connected to pin 3

void setup()
{
pinMode (LED8, OUTPUT); //Set the LED pin as an output
pinMode (BUTTON2, INPUT); //Set button as input (not required)
pinMode (LED9, OUTPUT); //Set the LED pin as an output
pinMode (BUTTON2, INPUT); //Set button as input (not required)
}

void loop()
{
if (digitalRead(BUTTON2) == LOW)
{
digitalWrite(LED8, LOW);
}
else
{
digitalWrite(LED8, HIGH);
delay(6000);
}

if (digitalRead(BUTTON3) == LOW)
{
digitalWrite(LED9, LOW);
}
else
{
digitalWrite(LED9, HIGH);
delay(6000);
}
}

The attached code been recently posted on this forum, sorry I do not recall by whom to give credit.

Is is one of the best examples on how to program multiple buttons and code should be adaptable to operate / control other I/O devices - relays included.

Have fun.

#include <Servo.h>           //servo library included
Servo myservo;              //my servo is myservo
const byte servoPin = 9;                 //The servo is plugged into this Arduino pin
const int servoPositionClose = 0;        //The 'closed' position in degrees on the servo
const int servoPositionOpen = 90;        //The open position in degrees

const byte numButtons = 4;               //The number of buttons connected (arrays below must have the same number of elements)
const byte buttonPins[] = {8, 7, 4, 2};  //The Arduino pin numbers the buttons are plugged into
const unsigned long buttonDelays[] = {1000, 2000, 3000, 4000}; //Milliseconds - the delay for each button
bool buttonState[numButtons];            //we're going to use this to remember which button(s) was previously pushed
unsigned long openTimeStart=0;           //record the time that we most recently moved to the open position
unsigned long currentDelay=0;             

void setup()                //do these things once
{
  Serial.begin(9600);           //data transmission rate to pc
  myservo.attach(servoPin);       
  myservo.write(servoPositionClose);        //servo start at 0 position

  for(int i=0; i<numButtons; i++) {
    pinMode(buttonPins[i],INPUT_PULLUP);      //pin X forced to HIGH when there is no external input
  } 

 
  Serial.println("Servo tester started");
  Serial.print("Compiled on ");
  Serial.print(__DATE__);
  Serial.print(" ");
  Serial.print(__TIME__);
  Serial.println("\n");
}


void loop()                     //loop forever
{
  bool newButtonState[numButtons];
  //read the current state of all the buttons
  for(int i=0; i<numButtons; i++) {
    newButtonState[i] = digitalRead(buttonPins[i]);     
  } 

  //work through the buttons to see what changed - which button has most-recently been pressed?
  for(int i=0; i<numButtons; i++) {
   
    if(newButtonState[i] == LOW && buttonState[i] != LOW) {
      //this button has been pushed and it wasn't before
      Serial.print("open ");
      Serial.println(i);
      currentDelay = buttonDelays[i];
      openTimeStart = millis();
    }
   
    //record the status of every button, for next time through loop
    buttonState[i] = newButtonState[i];
  }

  if(openTimeStart > 0 && millis() - openTimeStart < currentDelay) {
    myservo.write(servoPositionOpen);
  } else {
    myservo.write(servoPositionClose);
  }   

}

Delta_G:
You've fallen into the trap of using delay to handle your timing. That's fine if you're only doing one thing but this line

delay(6000);

Literally means don't do anything for 6 seconds. No button checking no nothing.

See the Blink Without Delay example and any of the hundreds of tutorials about how it works for some inspiration on a better plan.

Please, let the OP decide to use or not to use delay.

It is not good practice to change code just for sake of some ill conceived idea that code has to let loop() running even if it does nothing useful to accomplish the main project task.

232:
It is not good practice to change code just for sake of some ill conceived idea that code has to let loop() running even if it does nothing useful to accomplish the main project task.

You mean something useful like checking for the desired push buttons?

232:
Please, let the OP decide to use or not to use delay.

It's not a matter of taste, but a matter of functionality.

P.S. Could you troll somewhere else?

Delta_G:
It's not some ill conceived notion. That's what is blocking his code from reading the other button. How would you propose to fix it?

Easy: just add it to the yield function. Or even easier: just edit the delay function to read the buttons while blocking everything. Maybe interrupts could help?
Why would you use the overly complicated process of measuring time?
/s

Please, 232, you seem to have some problems with the way this forum works, and you don't want to believe the solutions of the experienced members whom you so despise.

If you want to do everything the hard way, feel free, but don't impose that onto other people.

@OP, as suggested, look into blink without delay.
Try to get it working with a single button+LED. You can then either copy the code multiple times for the other buttons, or you can encapsulate them into a class, using an Object-Oriented approach. The latter is obviously a cleaner solution, but it might be a little harder to grasp at first.
Good luck!

Pieter

Am I the only one who thinks that the delay in the original post is of absolutely no use?

if (button is pressed)
{
  turn_led_off();
}
else
{
  turn_led_on();
  wait_6_seconds_for_no_reason();
}

Just wondering..

EDIT: I though I saw INPUT_PULLUP used on buttons, my post may seem a bit inverted..