Pages: [1]   Go Down
Author Topic: Re: Need help with blink loop code  (Read 373 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 652
Posts: 50868
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define LED 13
#define TENTH 100
#define SECOND 1000
#define MINUTE 60*SECOND
#define HOUR 60*MINUTE
void loop()
{
   // Execute this loop 10 times every two hours
   for(int i=0; i<10; i++)
   {
      // This loop takes 1 second
      for(int j=0; j<5; i++)
      {
         digitalWrite(LED, HIGH);
         delay(TENTH);
         digitalWrite(LED, LOW);
         delay(TENTH);
      }
   }

   // Twiddle thumbs the rest of the time
   delay(HOUR*2 - SECOND*10);
}
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34725
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

When ever you find your self writing the same lines of code over and over it is time to try and investigate loops. In this case try and see how a for loop will cut down on the lines of code you write.
Computer languages are constructed so that the computer does the repetitive stuff not you.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 652
Posts: 50868
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Add up how long you are delaying, too. The total will be more than 2 hours. Your LED will start blink 10 seconds later, every iteration.

Not really critical in this case, but it's worth keeping in mind.

Also, if you want to change the length of time between iterations, or how long the LED is on and off while blinking, or how long the LED blinks, the magic numbers you have will make that harder.

The constants I defined can be easily changed.
Logged

Pages: [1]   Go Up
Jump to: