Go Down

Topic: Multiple blinking LEDs (Read 470 times) previous topic - next topic

gfvesley

I want to have 2 sets of red/green LEDs blink, red to green, at different rates.  Below is the code.  Both blink at same rate.  How do I change the rate?

I tried to use "<scheduler.h>", Arduino tutorial/multipleblinks, but received error "no such file or directory".  Where do I find the file?

Thanks for your help.

George


Code: [Select]
//sketch_Bi_color
// Bi-color  1 LED red/green
//1 led red/green on/off or 2 LED on/off
//include scheduler since we want to manage multiple task
//#include<scheduler.h>


int redled = 12;
int greenled = 13;
int redled2 = 11;
int greenled2 = 10;

void setup() {
    pinMode(redled, OUTPUT);
    pinMode(greenled, OUTPUT);
    pinMode(redled2, OUTPUT);
    pinMode (greenled2, OUTPUT);

    //Scheduler.startLoop(loop2);      //add loop2,  loop always started by default
}
    //task 1
    void loop(){

   digitalWrite(redled, HIGH);
   digitalWrite(greenled, LOW);

   delay(100);
   
   digitalWrite(redled2, HIGH);
   digitalWrite(greenled2, LOW);
   
    delay(4000);
//task 2
//   void loop2() {
   
   digitalWrite(redled, LOW);
   digitalWrite(greenled, HIGH);

    delay(100);
   
    digitalWrite(redled2, LOW);
   digitalWrite(greenled2, HIGH);

    delay(4000);

}

lastchancename

To run two different 'tasks' at different rates - get rid of the delay()s, and implement two separate millis() timers.

The two states may be implemented as function()s, or if you are calling more than two functions - perhaps a state-machine would be more relevant.

If you follow the millis() tutorials, this will make sense very quickly.
Experienced responders have a nose for laziness, (they were beginners once)... expecting the poster to contribute to the learning experience.

dougp

So two neutrinos went into a bar.  Nothing happened.  They were just passing through.

Koepel

This schedular is for the Arduino Due and Arduino Zero. Each task has its own stack.

When using millis() to do multiple things, the same stack is used. It is an efficient way for the Arduino Uno.
Using millis() is explained in the BlinkWithoutDelay example and Nick Gammon has a tutorial about it.

gfvesley

Thanks for the explanation of schedule.  I will follow your reference.

George

lastchancename

Before you get in too deep, while it's useful knowledge, *your* requirement is very straightforward.

Perhaps two hours *at most* for an experienced millis() coder to finish a complete, tidy sketch.

Threading and tasking is intended for much more complex applications than blinking LEDs out of sync.
Experienced responders have a nose for laziness, (they were beginners once)... expecting the poster to contribute to the learning experience.

gfvesley

I reviewed your suggestion (gammom blink) and rewrote code using millis function.  Code below.  However I am missing something.  LED red, green are two LEDs, LED2 is a bi-color red/green LED.  Using the code, LED2 goes red/green as expected. The LEDs red comes on, goes off and green comes on, then both come on and then are off for a second and the cycle repeats (red,green both,off)  What am missing? 

I am using this as an example for something in the future.

Thanks for your help.


Code: [Select]

//sketch_blink_2_sets_LEDs
//Blink 2 set of LEDs at different rates
//gammon.com.au/blink

const int redledPin = 12;              //red led pin
const int greenledPin = 13;            //green led pin
const int redled2Pin = 11;             //red led 2 pin
const int greenled2Pin = 10;           //green led 2 pin
//const int redbutton = 2;               // 5v red input
//const int greenbutton = 3;             // 5v green input
//const int redbutton2 = 4;               // 5v red input 2
//const int greenbutton2 = 5;            // 5v green input 2

//time peroid of blink
const unsigned long redledPininterval = 1000;
const unsigned long greenledPininterval = 2000;
const unsigned long redled2Pininterval = 5000;
const unsigned long greenled2Pininterval = 000;

//variablle holding the timer value

unsigned long redledPintimer;
unsigned long greenledPintimer;
unsigned long redled2Pintimer;
unsigned long greenled2Pintimer;

void setup() {
 pinMode (redledPin, OUTPUT);
 pinMode (greenledPin, OUTPUT);
 pinMode (redled2Pin, OUTPUT);
 pinMode (greenled2Pin, OUTPUT);
 
 redledPintimer = millis ();
 greenledPintimer = millis ();
 redled2Pintimer = millis ();
 greenled2Pintimer = millis ();
}

 void toggleredledPin ()
 {
  if (digitalRead (redledPin) == LOW)
     digitalWrite (redledPin, HIGH);
  else
     digitalWrite (redledPin, LOW);

     redledPintimer = millis ();
 }

 void togglegreenledPin () {
  if (digitalRead (greenledPin) == LOW)
    digitalWrite (greenledPin, HIGH);
    else
    digitalWrite (greenledPin, LOW);

       greenledPintimer = millis ();
 }
 
void toggleredled2Pin ()
{
  if (digitalRead (redled2Pin) == LOW)
     digitalWrite (redled2Pin, HIGH);
  else
     digitalWrite (redled2Pin, LOW);

     redled2Pintimer = millis ();
 }

 void togglegreenled2Pin ()
 {
 if (digitalRead (greenled2Pin) == LOW)
       digitalWrite (greenled2Pin, HIGH);
 else
    digitalWrite (greenled2Pin, LOW);

    greenled2Pintimer = millis ();
   
 }

 void toggledredled2Pin ()
 {
  if (digitalRead (redled2Pin) == LOW)
       digitalWrite (redled2Pin, HIGH);
       else
       digitalWrite (redled2Pin, LOW);

       redled2Pintimer = millis ();
 }
  void loop ()     
  {     
  if ( (millis() - greenledPintimer) >= greenledPininterval)  //handling the blink of green led
  togglegreenledPin ();

  if ( (millis () - redledPintimer) >= redledPininterval)
  toggleredledPin ();

  if ( (millis() - greenled2Pintimer) >= greenled2Pininterval)  //handling the blink of green led 2
  togglegreenled2Pin ();

  if ( (millis () - redled2Pintimer) >= redled2Pininterval)   // handling the blink of red led 2
  toggleredled2Pin ();
}


Koepel

#7
Apr 16, 2018, 12:44 am Last Edit: Apr 16, 2018, 12:44 am by Koepel
Zero is zero.
Code: [Select]
const unsigned long greenled2Pininterval = 000;
The 000 is the same as 00000 is the same as 0.

Do you want the leds to blink in sync or independent of each other ?

lastchancename

#8
Apr 16, 2018, 12:52 am Last Edit: Apr 16, 2018, 12:52 am by lastchancename
You may like to keep a boolean state for each LED, then the code becomes even simpler...
Code: [Select]

if (digitalRead (redled2Pin) == LOW)
     digitalWrite (redled2Pin, HIGH);
  else
     digitalWrite (redled2Pin, LOW);

BECOMES
Code: [Select]

{global - up top}
bool redState;

{local - to your function}
  redState = !redState;
  digitalWrite (redled2Pin, redState);
Experienced responders have a nose for laziness, (they were beginners once)... expecting the poster to contribute to the learning experience.

gfvesley

I want to have 2 sets of leds going red/green at different rates, independent of each other.

Why does the bi-color led (led2) blink differently the two separate leds?

Thanks.

outsider

#10
Apr 16, 2018, 03:43 am Last Edit: Apr 16, 2018, 03:57 am by outsider
If your bicolor is a 2 pin, you need to drive it with 2 output pins, lets say 8 and 9, connect one end of the LED to pin 8, other end to current limit resistor (330 Ohm) other end of resistor to pin 9, then:
Code: [Select]
digitalWrite(8,HIGH); // RED ON
digitalWrite(9,LOW);  //     "

digitalWrite(8,LOW);  // GREEN ON
digitalWrite(9,HIGH); //      "

digitalWrite(8,LOW); // OFF
digitalWrite(9,LOW); //     "

lastchancename

That's assuming they're 3-pin RG LEDs

if they're 2-pin RG LEDs, you can simply use 2 output pins of the Arduino, and manipulate them to get R, G, or R/G output.

A good chance to practice using millis() to mix the RG on times.
Experienced responders have a nose for laziness, (they were beginners once)... expecting the poster to contribute to the learning experience.

gfvesley

Using the code I posted on 4/15/18, the red, green leds blinks red, green, both on, off, red, green, both on, off....  The millis value set at 1000, 2000.   With millis value 1000, 1000 blink both on, both off.  If millis value is 1000, 0, one led is on all the time and the other blinks.  I can not make blink red, green, red, green...  How do I correct this? 

The bi-color (2 leads) led2, blinks red, green, red, green... as desired.  In this case the millis value are 2000 and 0.  Can anyone explain this?

Thank you.

Go Up