Go Down

Topic: Light and time (Read 403 times) previous topic - next topic

indeedio

hey everyone.

This is probably really straightforward but I was wondering if I could get some help making an LED flash at x rate for 5 seconds then another rate for 5 seconds.

This is the kind of thing I want to do. …..

void setup() {               
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(10, OUTPUT);     
}

void loop() {
  digitalWrite(10, HIGH);   //
  delay(20);              // do this HIGH/LOW set for 5 seconds
  digitalWrite(10, LOW);    //
  delay(20);

digitalWrite(10, HIGH);   //
  delay(10);              // do this HIGH/LOW set for 5 seconds
  digitalWrite(10, LOW);    //
  delay(10);             
 
   digitalWrite(10, HIGH);   //
  delay(15);              // do this HIGH/LOW set for 5 seconds
  digitalWrite(10, LOW);    //
  delay(15); 
 
  //GO BACK AND REPEAT WHOLE THING AGAIN CONTINIOUSLY
}


Any help would be greatly appreciated!

PaulS

What you need is a state machine. At certain times (as detected by differences in the values returned by millis()), you change the state (which blinking interval to use).

At certain times, based on the difference in the values returned by millis(), and the state (which defines the on/off interval), you turn the LED on or off.

Look at the blink without delay example, without delay.

indeedio

Hi,


I had a look at the blink without delay but I don't really understand how i would do it for the different rates?

PaulS

The rate at which the blink without delay example blinks the LED is fixed. Yours needs to be variable, based on some criteria - either time or index in a loop. Create an array of intervals, and check the elapsed duration against the appropriate interval, to determine if it is time to change the on/off status of the LED.

Msquare

indeedio, you coded:
Code: [Select]
void loop() {
 digitalWrite(10, HIGH);   //
 delay(20);              // do this HIGH/LOW set for 5 seconds
 digitalWrite(10, LOW);    //
 delay(20);

digitalWrite(10, HIGH);   //
 delay(10);              // do this HIGH/LOW set for 5 seconds
 digitalWrite(10, LOW);    //
 delay(10);              

And of course that means you get one on/off flash at the "20"rate, and one at the "10" rate.

And if you want the same thing multiple times, you use for-loop.

How many loops? If you know the rate (20ms delay = 50/sec) then you do it 125 times (125*(20+20)=5000 ms = 5sec.

That is not a particular good way to do it, but it will suffice with the requirements you give. If your next post says, you need to do something else while it is blinking (monitoring a switch f.ex.) then you realize why PaulS recomended the nodelayblink sketch.

Go Up