Two push buttons, two led's blinking at different rates

I have been working with Arduino for three days and been bashing my head with this. I have two push buttons and two led's. Here is what I am trying to do:

I would like both led's to blink slow when I don't push down any push buttons.
I would like to have led1 blink fast, led2 blink slow when I push down pushbutton1.
I would like to have led1 blink slow, led2 blink fast when I push down pushbutton2.
I would like them both to blink fast when I push down both pushbuttons.

My circuit is built correctly but I am having trouble with the code. I would like to use delay () if it is possible. Here is what i have:

//set pin numbers
const int ledPin1 = 2;
const int buttonPin1 = 4;
const int ledPin2 = 12;
const int buttonPin2 = 13;

int buttonState = 0;    //reading pushbutton

void setup () {

  Serial.begin(9600);
  pinMode(ledPin1, OUTPUT);    //sets pin as output
  pinMode(buttonPin1, INPUT);   //sets button as input
  pinMode(ledPin2, OUTPUT);    //sets pin as output
  pinMode(buttonPin2, INPUT);   //sets button as input
}

void loop() {
  if (digitalRead(buttonPin1) == HIGH && digitalRead(buttonPin2) == HIGH) {
    {
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, HIGH);
      delay(100);
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      delay(100);
    }
  }
      else if (digitalRead(buttonPin1) == HIGH && digitalRead(buttonPin2) == LOW) {
    {
      digitalWrite(ledPin1, HIGH);
      delay(100);
      digitalWrite(ledPin1, LOW);
      delay(100);
      digitalWrite(ledPin2, HIGH);
      delay(1000);
      digitalWrite(ledPin2, LOW);
      delay(1000);
    }
      }
            else if (digitalRead(buttonPin1) == LOW && digitalRead(buttonPin2) == HIGH) {
    {
      digitalWrite(ledPin1, HIGH);
      delay(1000);
      digitalWrite(ledPin1, LOW);
      delay(1000);
      digitalWrite(ledPin2, HIGH);
      delay(100);
      digitalWrite(ledPin2, LOW);
      delay(100);
  }
      }
    else {
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, HIGH);
      delay(1000);
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      delay(1000);
    }
  }

dougeffle:
I would like to use delay () if it is possible.

I'd go for blink without delay bwod, it I were you.

My first thought is to code the bwod stuff for each led in its own function, which will be called from loop().

In a third function, also called from loop() but ahead of the calls to the bwod functions, read the buttons. In that function, simply set each led's interval to slow or fast depending the state of its button.

Then when the bwod functions are called, the interval used for each led will be based on the buttons.

Give that a bash?

Do you know of any examples out there that uses this type of code? I'm having trouble finding anything about coding the bwod stuff into it's own function for each led or calling the functions to set the interval to slow or fast.

Start by reading Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

You can try this. If it works (compiles, not tested) it should be a good guide as you're going through the BWOD tutorial.

//set pin numbers
const int ledPin1 = 2;
const int buttonPin1 = 4;
const int ledPin2 = 12;
const int buttonPin2 = 13;

//these hold the "last" state of the buttons
//used for checking for a change in the state of the buttons
byte
    lastSw1,
    lastSw2;
    
//these track the state of the LEDs -- on or off
bool
    bstateLED1,
    bstateLED2;

//used for timing the LEDs and their delay times
unsigned long
    timeNow,
    timeLED1,
    timeLED1Delay,
    timeLED2,
    timeLED2Delay;

void setup () 
{
    //Serial.begin(9600);
    pinMode(ledPin1, OUTPUT);    //sets pin as output
    digitalWrite( ledPin1, LOW );
    bstateLED1 = false;
    
    pinMode(ledPin2, OUTPUT);    //sets pin as output
    digitalWrite( ledPin2, LOW );
    bstateLED2 = false;
    //
    pinMode( buttonPin1, INPUT_PULLUP );   //sets button as input
    lastSw1 = digitalRead( buttonPin1 );
    pinMode( buttonPin2, INPUT_PULLUP );   //sets button as input
    lastSw2 = digitalRead( buttonPin2 );

    timeNow = millis();
    timeLED1 = timeNow;
    timeLED2 = timeNow;
    timeLED1Delay = 1000;
    timeLED2Delay = 1000;
    
}//setup

void DoLEDs( void )
{
    //grab the current millis count
    timeNow = millis();

    //LED 1
    //if the elapsed millis (now minus then) is less than the delay value, just leave
    if( (timeNow - timeLED1) < timeLED1Delay )
        return;
    //reset the timer for the next blink
    timeLED1 = timeNow;
    //if toggle the LED
    //bstateLED1 holds the state of the LED; true = on, false = off
    if( bstateLED1 )
    {
        //on now; turn it off
        bstateLED1 = false;
        digitalWrite( ledPin1, LOW );
        
    }//if
    else
    {
        //off now; turn it on
        bstateLED1 = true;
        digitalWrite( ledPin1, HIGH );
        
    }//else

    //repeat for LED2
    //LED 2
    if( (timeNow - timeLED2) < timeLED2Delay )
        return;
    timeLED2 = timeNow;
    if( bstateLED2 )
    {
        bstateLED2 = false;
        digitalWrite( ledPin2, LOW );
        
    }//if
    else
    {
        bstateLED2 = true;
        digitalWrite( ledPin2, HIGH );
        
    }//else

}//DoLEDs

void ReadSwitches( void )
{
    byte
        currSw;

    //read button 1
    currSw = digitalRead( buttonPin1 );
    //if it's not the same as last time it has changes state
    // -- either just pressed or just released
    if( currSw != lastSw1 )
    {
        //record this as the new "last"
        lastSw1 = currSw;
        //if it's low now, it was just pressed...
        if( currSw == LOW )
            //so program the "fast" blink -- 100mS toggle time
            timeLED1Delay = 100;
        else
            //otherwise, program the "slow" blink -- 1000mS toggle time
            timeLED1Delay = 1000;
             
    }//if

    //repeat for button 2
    currSw = digitalRead( buttonPin2 );
    if( currSw != lastSw2 )
    {
        lastSw2 = currSw;
        if( currSw == LOW )
            timeLED2Delay = 100;
        else
            timeLED2Delay = 1000;
             
    }//if
    
}//ReadSwitches

void loop() 
{
    //read switches and do the LEDs in separate functions
    ReadSwitches();
    DoLEDs();
    
}//loop

As expected, Blackfin provided a complete solution as is his or her wont, so you can stop reading now if you like. Or, read on:

dougeffle:
I'm having trouble finding anything about coding the bwod stuff into it's own function for each led

Pretty much take the stuff that's in bwod's loop() and put it in its own function, then call that function from loop.

So here is the virgin bwod for reference, some comments removed to save space:

//Blink without Delay
// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
   unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Then here, I copied most of loop() into a new function myBWOD(), leaving the line that puts millis() into currentMillis behind in loop(). Also changed it so that currentMillis is declared at the top above setup(). See lines marked XXXXX

//Blink without Delay in a function
// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)
unsigned long currentMillis = millis(); // new XXXXXXXXXXXXXXXXXXX

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  currentMillis = millis(); //left in loop but doesn't do the declare here any more XXXXXXXX
  myBWOD();  // calls the new function XXXXXXXX
}

void myBWOD() { //contains most of the old loop() XXXXXXXXXXXXXXXXXXXXxx
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

dougeffle:
I'm having trouble finding anything about .... calling the functions to set the interval to slow or fast.

Then here, I just added another function to read the button. Button is from pin to ground. Pressed or unpressed gives different blink rate. See lines marked YYYYYYYY

//Blink without Delay in a function and button to change rate
// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
long interval = 1000;           // interval at which to blink (milliseconds) YYYYYY not const any more, need to be changed
unsigned long currentMillis = millis(); // new XXXXXXXXXXXXXXXXXXX
byte button = 2; // YYYYYYYYYYYYYYY

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  pinMode(button, INPUT_PULLUP); //YYYYYYYYYYYYYYYY
}

void loop()
{
  currentMillis = millis(); //left in loop but doesn't do the declare here any more XXXXXXXX
  checkButton(); //YYYYYYYYYYYYYYYYY
  myBWOD();  // calls the new function XXXXXXXX
}

void checkButton()  //YYYYYYYYYYYYYYY
{
  if (digitalRead(button) == HIGH) interval = 1000; // not presssed
  else interval = 100; //  presssed
}

void myBWOD() { //contains most of the old loop() XXXXXXXXXXXXXXXXXXXXxx
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

That's for one led of course, so you need to double up and have names like button1 and button2 etc.

I'm on my way out right now, Saturday beckons, back in a few hours....