The program flow should go like so:
1) if buttonVal < 28, immediately turn on led1 for two seconds while checking that buttonVal is still under 28. *If you use delay you cannot check to see if it was continuously held
2) if it was continuously held, turn a flag variable on.
3) that variable turns on the next peice of code which is the second led, for two seconds.
If you want to add a 3rd and 4th led, repeat steps 2 and 3.
There are many ways to code this. Mine is probably the ugliest, but hopefully easiest to understand.
Just the loop part..
void loop()
{
int buttonVal = analogRead(buttonPin);
long hold = 0;
long saveHold = millis();
boolean flag1 = false;
boolean flag2 = false;
boolean flag3 = false;
//for first LED
if(buttonVal < 28)
{
flag1 = true;
while(hold < holdTime)
{
hold = millis() - saveHold;
buttonVal = analogRead(buttonPin);
digitalWrite(ledPin1, HIGH);
if(buttonVal >= 28)
{
flag1 = false;
}
}
}
digitalWrite(ledPin1, LOW);
//for second LED
if(flag1)
{
hold = 0;
saveHold = millis();
flag2 = true;
while(hold < holdTime)
{
hold = millis() - saveHold;
digitalWrite(ledPin2, HIGH);
if(buttonVal >= 28)
{
flag2 = false;
}
}
}
digitalWrite(ledPin2, LOW);
//for third LED
if(flag2)
{
hold = 0;
saveHold = millis();
flag3 = true;
while(hold < holdTime)
{
hold = millis() - saveHold;
digitalWrite(ledPin3, HIGH);
if(buttonVal >= 28)
{
flag3 = false;
}
}
}
digitalWrite(ledPin3, LOW);
//for fourth LED
if(flag3)
{
hold = 0;
saveHold = millis();
while(hold < holdTime)
{
hold = millis() - saveHold;
digitalWrite(ledPin4, HIGH);
}
}
digitalWrite(ledPin4, LOW);
}