# define redLED 33
# define yellowLED 25
# define greenLED 26
# define walkingButtonExtender 32
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
void setup()
{
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(walkingButtonExtender, INPUT_PULLUP);
Serial.begin(9600); // Set the Serial Monitor baud rate
}
void loop()
{
//state1();
//state2();
state3();
state4();
}
// Red and yellow leds only (2 seonds)
void state1()
{
digitalWrite(redLED, HIGH);
digitalWrite(yellowLED, HIGH);
digitalWrite(greenLED, LOW);
delay(2000);
}
// Green led only (5 seconds)
void state2()
{
digitalWrite(redLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, HIGH);
delay(5000);
}
// Yellow led only (2 seconds)
void state3()
{
digitalWrite(redLED, LOW);
digitalWrite(yellowLED, HIGH);
digitalWrite(greenLED, LOW);
delay(2000);
}
// Red led only (5 seconds)
void state4()
{
digitalWrite(redLED, HIGH);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, LOW);
int maxDuration = 5;
int extendedDuration = 0;
// Setting Time variables
const unsigned long period = 1000; // in mili seconds
previousMillis = millis();
// Looping every second
for(int i = 0; i < maxDuration + extendedDuration; i++)
{
Serial.print("Walking: ");
Serial.print(maxDuration + extendedDuration - i);
Serial.println(" secnods left.");
currentMillis = millis();
previousMillis = currentMillis;
// for some reason, when putting the "currentMillis - prevoiusMillis > period"
// as condition for the while loop, it will not work. Could be a bug!
while(true)
{
if(currentMillis - previousMillis > period)
{
break;
}
if(digitalRead(walkingButtonExtender) == LOW)
{
extendedDuration++;
delay(100); // Slow down the processor to detect the button click once
}
currentMillis = millis();
}
}
Serial.println("Walking: 0 seconds left.");
// Old Code:
// for(int i = 0; i < maxDuration + extendedDuration; i++)
// {
// Serial.print("Walking: ");
// Serial.print(maxDuration + extendedDuration - i);
// Serial.println(" secnods left.");
// //delay(1000);
// timeNow = millis()
// while()
// if(digitalRead(walkingButtonExtender) == LOW)
// {
// extendedDuration++;
// }
// }
// Serial.println("Walking: 0 seconds left.");
}