Go Down

### Topic: Blink without delay for 2 different time intervals (Read 5726 times)previous topic - next topic

#### nbailey512

##### Jun 26, 2012, 03:43 am
I am looking to use the basic blink without delay program but instead of having the the light blink on and off for only one period of time, I would like to keep the light on for one period and off for another.  I need to keep with millis() instead of any delay functions because I need to monitor inputs while this is going on.  Is someone able to show me some basic code that would accomplish this?

#### marco_c

#1
##### Jun 26, 2012, 03:46 am
Try the Multi_Blink example in the Playground.
Arduino Libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com
Arduino++ blog https://arduinoplusplus.wordpress.com

#2
##### Jun 26, 2012, 04:33 am

Looking at Multi-blink, I get lost in a hurry. I could probably work it out but, it is not a beginner sketch imho.

How about blinking 5 leds at individual rates without delay! The sketch could be made smaller but, it should be easy to see a pattern.

Code: [Select]
`/* Blink without Delay Based on Blink without Delay http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay */// constants won't change. Used here to set pin numbers:  // Pin 13: Arduino has an LED connected on pin 13  // Pin 11: Teensy 2.0 has the LED on pin 11  // Pin 6: Teensy++ 2.0 has the LED on pin 6const int led1 =  13;      // the number of the LED pinconst int led2 =  5;       // the number of the second LEDconst int led3 =  6;       //3rdconst int led4 =  7;       //4thconst int led5 =  8;       //5th// Variables will change:int ledState1 = LOW;             // ledState used to set the LEDint ledState2 = LOW;             // ledState used to set the LEDint ledState3 = LOW;             // ledState used to set the LEDint ledState4 = LOW;             // ledState used to set the LEDint ledState5 = LOW;             // ledState used to set the LEDlong previousMillis1 = 0;        // will store last time LED was updatedlong previousMillis2 = 0;        // will store last time LED was updatedlong previousMillis3 = 0;        // will store last time LED was updatedlong previousMillis4 = 0;        // will store last time LED was updatedlong previousMillis5 = 0;        // will store last time LED was updated// the follow variables is a long because the time, measured in miliseconds,// will quickly become a bigger number than can be stored in an int.long interval1 = 500;           // interval at which to blink (milliseconds)long interval2 = 700;           // interval at which to blink (milliseconds)long interval3 = 800;           // interval at which to blink (milliseconds)long interval4 = 400;           // interval at which to blink (milliseconds)long interval5 = 550;           // interval at which to blink (milliseconds)void setup() {  // set the digital pin as output:  pinMode(led1, OUTPUT);  pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); pinMode(led4, OUTPUT); pinMode(led5, OUTPUT); }void loop(){   unsigned long currentMillis = millis();   if(currentMillis - previousMillis1 > interval1) {    // save the last time you blinked the LED     previousMillis1 = currentMillis;       // if the LED is off turn it on and vice-versa:    if (ledState1 == LOW)      ledState1 = HIGH;    else      ledState1 = LOW;    // set the LED with the ledState of the variable:    digitalWrite(led1, ledState1);  }   if(currentMillis - previousMillis2 > interval2) {    // save the last time you blinked the LED     previousMillis2 = currentMillis;       // if the LED is off turn it on and vice-versa:    if (ledState2 == LOW)      ledState2 = HIGH;    else      ledState2 = LOW;    // set the LED with the ledState of the variable:    digitalWrite(led2, ledState2);  }   if(currentMillis - previousMillis3 > interval3) {    // save the last time you blinked the LED     previousMillis3 = currentMillis;       // if the LED is off turn it on and vice-versa:    if (ledState3 == LOW)      ledState3 = HIGH;    else      ledState3 = LOW;    // set the LED with the ledState of the variable:    digitalWrite(led3, ledState3);  }   if(currentMillis - previousMillis4 > interval4) {    // save the last time you blinked the LED     previousMillis4 = currentMillis;       // if the LED is off turn it on and vice-versa:    if (ledState4 == LOW)      ledState4 = HIGH;    else      ledState4 = LOW;    // set the LED with the ledState of the variable:    digitalWrite(led4, ledState4);  }   if(currentMillis - previousMillis5 > interval5) {    // save the last time you blinked the LED     previousMillis5 = currentMillis;       // if the LED is off turn it on and vice-versa:    if (ledState5 == LOW)      ledState5 = HIGH;    else      ledState5 = LOW;    // set the LED with the ledState of the variable:    digitalWrite(led5, ledState5);  }}`
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

#### Jack Christensen

#3
##### Jun 26, 2012, 04:56 am
I think the OP just wants to blink one LED, but with something other than a 50% duty cycle. I use code similar to this:

Code: [Select]
`#define LED_PIN 13#define LED_ON 800       //milliseconds#define LED_OFF 200unsigned long ms;        //time from millis()unsigned long msLast;    //last time the LED changed stateboolean ledState;        //current LED statevoid setup(void){    pinMode(LED_PIN, OUTPUT);}void loop(void){    ms = millis();    blinkLED();}void blinkLED(void){    if (ms - msLast > (ledState ? LED_ON : LED_OFF)) {        digitalWrite(LED_PIN, ledState = !ledState);        msLast = ms;    }}`

#4
##### Jun 26, 2012, 05:11 am
Quote
I think the OP just wants to blink one LED, but with something other than a 50% duty cycle. I use code similar to this:

Sorry, after reading the post again, I agree. Darn and it took me a bit of time to write that sketch too! lol
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

#### Antennas

#5
##### Aug 25, 2012, 09:49 pm
No thanks very much for this code. I want to blink LED's like a police car. For fun and to learn. Such as headlights that go back and forth and red and blue lights that blink fast and then even a set of strobes that blink fast as well, but not as fast as the red and blue's but faster than the headlights. If you wanted to go full meal deal then another set that strobe as weill in a pattern (the ambers)

I am new to the arduino but I hope to dissect your code and learn what it is doing.  I find I learn faster when I can look at something and make changes and see what happens. Such as it doesn't work anymore or the rates change.

THANKS!!

Andrew

#### lloyddean

#6
##### Aug 26, 2012, 01:45 am
Code: [Select]
`// <http://arduino.cc/forum/index.php/topic,111702.msg903903.html#new>#include <WProgram.h>//const uint8_t         pinLED      = 13;   // Arduino Uno//const uint8_t         pinLED      = 13;   // Arduino Mega//const uint8_t         pinLED      = 13;   // Digilent chipKIT Uno32//const uint8_t         pinLED      = 77;   // Digilent chipKIT Max32//const uint8_t         pinLED      = 54;   // Digilent Cerebot MX7 ckconst unsigned long     dtON_DELAY  = 500UL;const unsigned long     dtOFF_DELAY = 250UL;const uint8_t           LED_OFF     = LOW;const uint8_t           LED_ON      = HIGH;uint8_t                 stateLED    = LED_OFF;unsigned long           dtTrigger;bool hasTimeArrived(const unsigned long& dtTrigger){    return (((long)(millis() - dtTrigger)) >= 0);}unsigned long blink(){    stateLED = ((stateLED == LED_ON) ? LED_OFF : LED_ON);    digitalWrite(pinLED, stateLED);        return ((stateLED == LED_ON) ? dtON_DELAY : dtOFF_DELAY);}void loop(){    if ( hasTimeArrived(dtTrigger) )    {        dtTrigger += blink();    }}void setup(){    dtTrigger = millis();        pinMode(pinLED, OUTPUT);     }`

#### PGT

#7
##### Aug 26, 2012, 04:58 am
turn on led  i%4
turn off led (i-1)%4  //% = modulo   ..4 leds   // i a counter

#8
##### Jan 31, 2015, 09:41 am

you can use my timer library, very simple to use. no delays, small footprint.

after you import the library, check the sample and test it.

Code: [Select]
`/*     Title: Simple Switch Timer     Library: sstimer.ino     Description:      Created Date: January 17, 2015     Created By: Rolly Falco Villacacan     Released into the public domain.     http://www.mvsadnik.com/microcontrollers/     https://www.facebook.com/groups/pinoymikrocontrollerscommunity/     https://www.facebook.com/pages/Pinoy-Mikro-Controllers-Community/1397572657207414*/#include <sstimer.h>//WATER_PUMP SETUPint PIN_NO1 = 13;long unsigned OFF_DURATION = 500; //in milliseconds HOW LONG YOU WANT THE PIN TO BE OFF STATElong unsigned ON_DURATION = 500; //in milliseconds //HOW LONG YOU WANT THE PIN TO BE ON STATE//LIGHTHING SETUPint PIN_NO2 = 12;long unsigned OFF_DURATION2 = 1000; //in millisecondslong unsigned ON_DURATION2 = 1000; //in millisecondssstimer WATER_PUMP(PIN_NO1, ON_DURATION, OFF_DURATION); //create instance for water pumpsstimer LIGHTHING(PIN_NO2, ON_DURATION2, OFF_DURATION2);//create instance for lightingvoid setup(){//...  }void loop(){//...WATER_PUMP.startsstimer();LIGHTHING.startsstimer();//..  }`/

#### UKHeliBob

#9
##### Jan 31, 2015, 09:58 am
Code: [Select]
`void loop(){//...WATER_PUMP.startsstimer();LIGHTHING.startsstimer();//..  }`Start the timers each time trough loop()
Is that right ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### UKHeliBob

#10
##### Jan 31, 2015, 10:00 am
Quote
I want to blink LED's like a police car. For fun and to learn. Such as headlights that go back and forth and red and blue lights that blink fast and then even a set of strobes that blink fast as well, but not as fast as the red and blue's but faster than the headlights. If you wanted to go full meal deal then another set that strobe as weill in a pattern (the ambers)
Have a look at Several things at the same time
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#11
##### Jan 31, 2015, 10:09 am
opppsss

I got your point now, sorry I think i'ved name the function wrong...i should have not use the work "start" if should be any name, and i admit it could cause confusion now.

I'll can change it to "check"

thanks man,

#12
##### Jan 31, 2015, 10:13 am
done,

Code: [Select]
`void loop(){//...WATER_PUMP.check();LIGHTHING.check();//..  }`

#### pYro_65

#13
##### Jan 31, 2015, 10:17 am

Please stop waking up old threads just to plug your library, like this one from 2011 http://forum.arduino.cc/index.php?topic=74861.msg2068728#msg2068728

https://forum.arduino.cc/index.php?action=dlattach;topic=327736.0;attach=128670 New EEPROM library released
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ

#14
##### Jan 31, 2015, 10:22 am
sorry friend,
never happen again. its honest mistake.

Go Up

Please enter a valid email to subscribe