Pages: [1]   Go Down
Author Topic: Light and time  (Read 328 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45997
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45997
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 25
Posts: 1133
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

indeedio, you coded:
Code:
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.
Logged

Pages: [1]   Go Up
Jump to: