Multiple LEDs/Blinking code question

Hello,

I'm extremely new to arduino and I'm trying to get my feet wet by doing some simple LED exercises. I have a breadboard setup to where I have 3 LEDS and two switches--- I want to program the arduino to power up all 3 LEDs once I press down a switch but have only one of them blink off/on every second.

The problem that I'm having is when I press my other switch button to turn off the LEDs, my 'blinking LED' doesn't turn off. I'm going to safely assume it has something to do with my void loop and it not telling the arduino to stop that specific blinking loop when I press the 'off' switch. I just dont know how to implement that in my code. I've pasted my code here:

int ledPin = 5;
int ledPin1 = 4;
int ledPin2 = 13;
int buttonApin = 9;
int buttonBpin = 8;

byte leds = 0;

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
}

void loop()
{
if (digitalRead(buttonApin) == LOW)
{
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
}
{
//blink LED
digitalWrite(ledPin2, HIGH); // Pin 13 = 5 V, LED emits light
delay(500); // ..for 0.5 seconds
digitalWrite(ledPin2, LOW); // Pin 13 = 0 V, LED no light
delay(500); // ..for 0.5 seconds
}
if (digitalRead(buttonBpin) == LOW)
{
digitalWrite(ledPin, LOW);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
}

As you can probably tell, I'm totally green--- any help would be appreciated!

You have to remember if the LED should be blinking or not. Blink the LED only if it should be blinking.

int ledPin = 5;
int ledPin1 = 4;
int ledPin2 = 13;
int buttonApin = 9;
int buttonBpin = 8;






boolean blinking = false;


byte leds = 0;


void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);
  pinMode(buttonBpin, INPUT_PULLUP);
}


void loop()
{
  if (digitalRead(buttonApin) == LOW)
  {
    blinking = true;
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
  }


  if (digitalRead(buttonBpin) == LOW)
  {
    blinking = false;
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
  }


  if (blinking)
  {
    // blink LED
    digitalWrite(ledPin2, HIGH);                    // Pin 13 = 5 V, LED emits light
    delay(500);                                // ..for 0.5 seconds
    digitalWrite(ledPin2, LOW);                     // Pin 13 = 0 V, LED no light
    delay(500);                                // ..for 0.5 seconds
  }
}

remove digitalWrite(ledPin2,LOW) from your last if statement

change the blinking led code to something like (psuedo code)

if (digitalRead(pin2)) //if on
digitalWrite(pin2)=low; //turn off
else
digitalWrite(pin2)=high; //if off, turn on

put a delay at the end of your code so it slows down the looping a bit so you can see the blinking otherwise the loop exutes so quickly it looks like the led2 is dimmed.

Your "other" button DOES turn off the blinking LED. But the blink is unconditional and so it keeps blinking.

Ahh, I understand what I was missing now.

Thanks for the input guys, I appreciate it.

The demo Several Things at a Time blinks 3 LEDs and other stuff. It may be of interest.

...R