Pages: 1 [2]   Go Down
Author Topic: How to blink 3 leds at different time intervals  (Read 615 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

one goes on for 500 milliseconds and then goes off for 500 milliseconds
another goes on after 30 seconds and stays on for the next 30 seconds
another goes on after 60 minutes and stays on for the next 60 minutes
any way its fine heres my code:
Code:
uint32_t
  time_now  = 0,
  time_past = 0;
 
uint8_t
  half_seconds    = 0,
  seconds         = 0,
  minutes         = 0,
  LED_seconds_pin = 11,
  LED_minutes_pin = 12,
  LED_hours_pin   = 13;

bool
  LED_seconds_state = false,
  LED_minutes_state = false,
  LED_hours_state   = false;

void setup()
{
  pinMode( LED_seconds_pin, OUTPUT );
  pinMode( LED_minutes_pin, OUTPUT );
  pinMode( LED_hours_pin,   OUTPUT );

  time_past = millis();
}

void loop()
{
  time_now = millis();

  //every 500 milliseconds
  if ( time_now - time_past >= 500 )
  {
    time_past = time_now;

    //toggle LED_seconds
    LED_seconds_state = !LED_seconds_state;
    digitalWrite( LED_seconds_pin, LED_seconds_state );

    //every seconds
    if ( ++ half_seconds >= 2 )
    {
      half_seconds = 0;
     
      //every 30 seconds
      if ( ++ seconds >= 60 )
     
      {
        seconds = 0;

        //toggle LED_minutes
        LED_minutes_state = !LED_minutes_state;
        digitalWrite( LED_minutes_pin, LED_minutes_state );

        //every 30 minutes
        if ( ++ minutes >= 30 )
        {
          minutes = 0;

          //toggle LED_hours
          LED_hours_pin = !LED_hours_pin;
          digitalWrite( LED_hours_pin, LED_hours_state );
        }
      }
    }
  }
}
thanks!
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It works so I am going to remove this page k?
Logged

France
Offline Offline
God Member
*****
Karma: 29
Posts: 898
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It can help other people so don't delete it smiley
Logged

Pages: 1 [2]   Go Up
Jump to: