Pages: [1]   Go Down
Author Topic: Blink without delay for 2 different time intervals  (Read 1697 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1185
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try the Multi_Blink example in the Playground.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Greenville, IL
Offline Offline
Edison Member
*
Karma: 11
Posts: 1309
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 Looking at Multi-blink, I get lost in a hurry. I could probably work it out but, it is not a beginner sketch imho.
http://arduino.cc/playground/Code/MultiBlink

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:
/* 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 6
const int led1 =  13;      // the number of the LED pin
const int led2 =  5;       // the number of the second LED
const int led3 =  6;       //3rd
const int led4 =  7;       //4th
const int led5 =  8;       //5th
// Variables will change:
int ledState1 = LOW;             // ledState used to set the LED
int ledState2 = LOW;             // ledState used to set the LED
int ledState3 = LOW;             // ledState used to set the LED
int ledState4 = LOW;             // ledState used to set the LED
int ledState5 = LOW;             // ledState used to set the LED
long previousMillis1 = 0;        // will store last time LED was updated
long previousMillis2 = 0;        // will store last time LED was updated
long previousMillis3 = 0;        // will store last time LED was updated
long previousMillis4 = 0;        // will store last time LED was updated
long 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);
  }
}
Logged


Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3515
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#define LED_PIN 13
#define LED_ON 800       //milliseconds
#define LED_OFF 200

unsigned long ms;        //time from millis()
unsigned long msLast;    //last time the LED changed state
boolean ledState;        //current LED state

void 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;
    }
}
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Greenville, IL
Offline Offline
Edison Member
*
Karma: 11
Posts: 1309
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


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

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
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// <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 ck

const 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);     
}

Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 212
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

turn on led  i%4
turn off led (i-1)%4  //% = modulo   ..4 leds   // i a counter
Logged

Pages: [1]   Go Up
Jump to: