hi guys, I´m a newbie and this code helped me a lot , I modified it a bit since what I want to do is a bit different and simpler I think . The code is working but is erratic , after sometime the yellow led doesn´t go high and some other misconduct.
what i want to do do i that while input is low green led is high , when input goes high , green led goes low yellow led starts blinking and after sometime yellow led goes low ,red led blinks .
Meanwhile the other green leds should be blinking in order .
I´ll paste the code here hoping I can get some help , I know I used some delays and maybe they are messing with the time variables , i was planning on blinking the leds whitout delay after I solved the basics.
Thanks for any help.
the circuit :
Code :
int buttonPin= 5;
int currentButtonState;
int previousButtonState;
unsigned long buttonPressTime;
unsigned long currentTime;
unsigned long waitPeriod = 3000;
boolean timing = true;
int red=13;
int yellow=12;
int green=11;
int gr1=8;
int gr2=9;
int gr3=10;
unsigned long
time_now = 0,
time_past = 0;
uint8_t
half_seconds = 0,
seconds = 0,
minutes = 0,
LED_seconds_pin = 8,
LED_minutes_pin = 9,
LED_hours_pin = 10;
bool
LED_seconds_state = false,
LED_minutes_state = false,
LED_hours_state = false;
void setup()
{
Serial.begin(115200);
pinMode(buttonPin, INPUT);
pinMode(red,OUTPUT);
pinMode(yellow,OUTPUT);
pinMode(green,OUTPUT);
pinMode(gr1,OUTPUT);
pinMode(gr2,OUTPUT);
pinMode(gr3,OUTPUT);
pinMode( LED_seconds_pin, OUTPUT );
pinMode( LED_minutes_pin, OUTPUT );
pinMode( LED_hours_pin, OUTPUT );
time_past = millis();
}
void loop()
{
currentTime = millis();
previousButtonState = currentButtonState;
currentButtonState = digitalRead(buttonPin);
//wave();
if ( timing)
{
if (currentButtonState != previousButtonState && currentButtonState == LOW)//input gone LOW
{
Serial.println("\nTiming started Gled ON");
buttonPressTime = currentTime;
timing = false;
gledstrip();
}
}
else if (currentButtonState != previousButtonState && currentButtonState == HIGH)//input gone HIGH
{
//timing = false;
timing = true;
yledstrip();
}
if ((currentTime - buttonPressTime >= waitPeriod) && timing && currentButtonState == HIGH)
{
timing = true;
rledstrip();
}
}
void gledstrip()
{
Serial.println("Input went LOW");
digitalWrite(red,LOW);
digitalWrite(yellow,LOW);
digitalWrite(green,HIGH);
}
void yledstrip()
{
Serial.println("Input went HIGH within 3 seconds Yled ON");
digitalWrite(green,LOW);
delay(200);
digitalWrite(yellow,HIGH);
}
void rledstrip()
{
Serial.println("Input still HIGH after 3 seconds Rled ON");
digitalWrite(yellow,LOW);
delay(500);
digitalWrite(red,HIGH);
delay(500);
digitalWrite(red,LOW);
//delay(500);
}