Loop within a loop

Hi,

I was wondering if someone could give me some help with making a loop within a loop.

I have multiple if statements for button press, and on one of the if statements, i would like LED to stay on until another if statement is fulfilled.

*/

const int button1Pin = 2; // pushbutton 1 pin
const int button2Pin = 3; // pushbutton 2 pin
const int led1Pin = 4; // LED pin1
const int led2Pin = 5; // LED pin2
const int led3Pin = 6; // LED pin3

void setup()
{

pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);

pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
}

void loop()
{
int button1State, button2State; // variables to hold the pushbutton states

button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);

if (((button1State == HIGH) && (button2State == HIGH)))
{
digitalWrite(led1Pin, HIGH); // turn the LED on
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
delay (3000);
}

if (((button1State == LOW) && (button2State == LOW)))
{
digitalWrite(led1Pin, LOW); // turn the LED on
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, LOW);
delay (3000);
}

if (((button1State == HIGH) && (button2State == LOW)))
{
digitalWrite(led1Pin, LOW); // turn the LED on
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, LOW);
delay (3000);
}

if (((button1State == LOW) && (button2State == HIGH)))
{
digitalWrite(led1Pin, LOW); // turn the LED on
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, HIGH);
delay (3000);

/// loop - digitalWrite(led3Pin, HIGH); - until - if (((button1State == HIGH) && (button2State == LOW)))

}

else
{
digitalWrite(led1Pin, LOW); // turn the LED off
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
}

}

keytracker.5.ino (1.79 KB)

keytracker.5.ino (1.79 KB)

   /// loop - digitalWrite(led3Pin, HIGH); - until - if (((button1State == HIGH) && (button2State == LOW)))
while ( not ((button1State == HIGH) && (button2State == LOW))) )
{
digitalWrite(led3Pin, HIGH);
}

I'm not designing here - it's not pretty but I'm giving you as close to what you are literally asking for.

If you want a loop inside a loop, it looks like:

void loop()
{
loop();
}

:)

Also learn to use code tags! Please read the post: How to use this forum - please read.

If you want a loop inside a loop, it looks like: Code: [Select] void loop() { loop(); }

NOOOOO!

aarg: I'm not designing here - it's not pretty but I'm giving you as close to what you are literally asking for.

If you want a loop inside a loop, it looks like:

void loop()
{
loop();
}

:)

Probably not a good idea...