Go Down

Topic: Re: Need help with blink loop code (Read 385 times) previous topic - next topic

PaulS

Code: [Select]
#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);
}

Grumpy_Mike

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.

PaulS

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.

Go Up