Go Down

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


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);
        digitalWrite(LED, LOW);

  // Twiddle thumbs the rest of the time
  delay(HOUR*2 - SECOND*10);
The art of getting good answers lies in asking good questions.


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.


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.
The art of getting good answers lies in asking good questions.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131