I think I understand, are you saying I need to the additional code with the "&&" format to all of the buttons? like this?
do {
void oneOnAtATime();
{
int index;
int delayTime = 100; // milliseconds to pause between LEDs
// make this smaller for faster switching
// step through the LEDs, from 0 to 7
for(index = 0; index <= 4; index++)
{
digitalWrite(ledPins[index], HIGH); // turn LED on
delay(delayTime); // pause to slow down
digitalWrite(ledPins[index], LOW); // turn LED off
}
}
while((button1State == LOW) && (button2State == LOW) &&
(button3State == LOW) && (button4State == LOW));
}
void pingPong(); // A-BUTTON
{
int index;
int delayTime = 100; // milliseconds to pause between LEDs
// step through the LEDs, from 0 to 7
if ((button1State == HIGH) && (button2State == LOW) &&
(button3State == LOW) && (button4State == LOW)) {
for (index = 0; index <= 4; index++)
{
digitalWrite(ledPins[index], HIGH); // turn LED on
delay(delayTime); // pause to slow down
digitalWrite(ledPins[index], LOW); // turn LED off
}
// step through the LEDs, from 7 to 0
for (index = 4; index >= 0; index--)
{
digitalWrite(ledPins[index], HIGH); // turn LED on
delay(delayTime); // pause to slow down
digitalWrite(ledPins[index], LOW); // turn LED off
}
}
else {
// turn LED off:
digitalWrite(ledPins[index], LOW);
}
}
More or less. You need to adapt the code that I wrote earlier. The code that I wrote is:
void pingPong()
{
int index;
int delayTime = 100; // milliseconds to pause between LEDs
boolean turnOff = false;
// step through the LEDs, from 0 to 7
if (button2State == HIGH) {
for (index = 0; index <= 4; index++)
{
digitalWrite(ledPins[index], HIGH); // turn LED on
delay(delayTime); // pause to slow down
digitalWrite(ledPins[index], LOW); // turn LED off
if (button2State == LOW) {
turnOff = true;
break; // exit from this for loop
}
}
}
// step through the LEDs, from 7 to 0
if ( !turnOff && button2State == HIGH) {
for (index = 4; index >= 0; index--)
{
digitalWrite(ledPins[index], HIGH); // turn LED on
delay(delayTime); // pause to slow down
digitalWrite(ledPins[index], LOW); // turn LED off
if (button2State == LOW) {
turnOff = true;
break; // exit from this for loop
}
}
}
if (turnOff) {
// turn ALL LED's off:
for (index = 0; index <= 4; index++)
{
digitalWrite(ledPins[index], LOW);
}
}
}
So, for the while you should do something like:
boolean exitSequence = false;
do {
int index;
int delayTime = 100; // milliseconds to pause between LEDs
// make this smaller for faster switching
// step through the LEDs, from 0 to 7
for (index = 0; index <= 4; index++)
{
digitalWrite(ledPins[index], HIGH); // turn LED on
delay(delayTime); // pause to slow down
digitalWrite(ledPins[index], LOW); // turn LED off
if ((button1State == HIGH) && (button2State == HIGH) &&
(button3State == HIGH) && (button4State == HIGH)) {
exitSequence = true;
break;
}
}
} while (!exitSequence);
You could keep the while condition like you had, but this is different and do exactly the same (and is wrote in a shorter way). What you really need to do is write the break inside the for loop, so the sequence stops at the moment that you press the button and not only at the end of the for loop.
I recall one more time the fact that you need to have somewhere something like:
int button1State, button2State, button3State, button4State;
// read the state of the pushbutton value:
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
button3State = digitalRead(button3Pin);
button4State = digitalRead(button4Pin);
Is not the same thing! An I'm pointing that exactly to avoid an error like that.
The key state is only read when you do:
button1State = digitalRead(button1Pin);
If you read the state of the key, in one place and then in other place you test that state, the state that you are testing is the first that you read. Even if the key state changes in between when you test is you don't know that it changes. (I don't know if I explain my self)
So, you need to change that in order to accomplish your goal: "as sooner as you press the key, the program starts to do other thing"