Go Down

Topic: Using millis more than two LEDs (Read 1 time) previous topic - next topic


How do I light 14 (on one side, 14 on the other) sequentially without using a FOR loop?
First, I would put your LEDs into an array. But instead of using a for() loop to turn them on, increment the index variable with a millis() "timer." That way you can control how quickly they turn on.

Partial/Untested code
Code: [Select]

unsigned long previousMillisIncrement = 0;
unsigned long incrementInterval = 100;
unsigned byte groupIndex = 0;
const unsigned byte maxIndex = 14;
bool onFlag = false;
byte ledPins[maxIndex] = {}; // filled with the right pins

void loop() {
  if (onFlag && (millis() - previousMillisIncrement >= incrementInterval)) {
    previousMillisIncrement = millis();
    if (groupIndex >= maxIndex) {
      // stop turning stuff on
      groupIndex = 0;
      onFlag = false;
    } else {
      digitalWrite(ledPins[groupIndex], HIGH);

Somewhere in your code, you need to set onFlag to true to make the whole thing happen. I would also "reset" prevousMillisiIncrement at the same time.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com


I'm giving up on millis.   Too tedious, doesn't work for more than 2 LEDs.

Trying arrays and interrupts.


You are making it worse for yourself by doing that.
"The problem is in the code you didn't post."


Gave up on millis.   Solved my problem by doing what's in code blocks.   A little ugly for the arrays but it works.   I can create the arrays in 3 statements, but there is no saving in space, and then it's not that intuitive, so I will stay with the ugliness.   I basically just ran the one array in reverse, so now 14 lights follow each other in sequence CW, while the other 14 rotate CCW.   KISS!

Code: [Select]
SequenceArray2[0] = 0x0001;  //create this array using shift bit, function "SetArray" <<
    SequenceArray2[1] = 0x0002;
    SequenceArray2[2] = 0x0004;
    SequenceArray2[3] = 0x0008;
    SequenceArray2[4] = 0x0010;
    SequenceArray2[5] = 0x0020;
    SequenceArray2[6] = 0x0040;
    SequenceArray2[7] = 0x0080;
    SequenceArray2[8] = 0x0100;
    SequenceArray2[9] = 0x0200;
    SequenceArray2[10] = 0x0400;
    SequenceArray2[11] = 0x0800;
    SequenceArray2[12] = 0x1000;
    SequenceArray2[13] = 0x2000;
    SequenceArray2[14] = 0x4000;


I'm giving up on millis.   Too tedious, doesn't work for more than 2 LEDs.

Trying arrays and interrupts.
It DOES work for as many things as you want.  It's not tedious.  If you can handle reading a clock in day to day life then you can use millis.  It's the same concept. 

There's a huge difference between you not being able to figure it out and it not working.  Your problem was that you had the for loops turning all the lights on at once.  Let the loop function do the looping.  The loop function needs code to decide if it is time to take an action and if so the code to take it. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.



Jun 17, 2018, 06:35 am Last Edit: Jun 17, 2018, 06:37 am by polymorph
I can barely program, and I wrote code to blink 4 LEDs asynchronously using millis().

Code: [Select]

/* Blink Four LEDs Without Delay
  Adapted from Blink Without Delay
  by Steven J Greenfield, aka Polymorph
  Use however you like

// constants won't change. Used here to
// set pin numbers:
const int ledPin0 =  2;      // the number of the LED0 pin
const int ledPin1 =  3;      // the number of the LED1 pin
const int ledPin2 =  4;      // the number of the LED2 pin
const int ledPin3 =  5;      // the number of the LED3 pin

// Variables will change:
int ledState0 = LOW;             // ledState used to set the LED0
int ledState1 = LOW;             // ledState used to set the LED1
int ledState2 = LOW;             // ledState used to set the LED2
int ledState3 = LOW;             // ledState used to set the LED3

unsigned long previousMillis0 = 0;        // will store last time LED0 was updated
unsigned long previousMillis1 = 0;        // will store last time LED1 was updated
unsigned long previousMillis2 = 0;        // will store last time LED2 was updated
unsigned long previousMillis3 = 0;        // will store last time LED3 was updated

// the follow variables is a long because the time, measured in milliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long interval0 = 1000;           // interval at which to blink LED0 (milliseconds)
unsigned long interval1 = 457;           // interval at which to blink LED1 (milliseconds)
unsigned long interval2 = 1020;           // interval at which to blink LED2 (milliseconds)
unsigned long interval3 = 742;           // interval at which to blink LED3 (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin0, OUTPUT);   
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);   

void loop()
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.

  // save the current time so it doesn't change during an operation
  unsigned long currentMillis = millis();

  // LED0
  if(currentMillis - previousMillis0 >= interval0) {
    // save the last time you blinked the LED0
    previousMillis0 += interval0;   // prevents time creep

    // if the LED is off turn it on and vice-versa:
    if (ledState0 == LOW)
      ledState0 = HIGH;
      ledState0 = LOW;

  if(currentMillis - previousMillis1 >= interval1) {
    // save the last time you blinked the LED0
    previousMillis1 += interval1;   

    // if the LED is off turn it on and vice-versa:
    if (ledState1 == LOW)
      ledState1 = HIGH;
      ledState1 = LOW;

  if(currentMillis - previousMillis2 >= interval2) {
    // save the last time you blinked the LED0
    previousMillis2 += interval2;   

    // if the LED is off turn it on and vice-versa:
    if (ledState2 == LOW)
      ledState2 = HIGH;
      ledState2 = LOW;

  if(currentMillis - previousMillis3 >= interval3) {
    // save the last time you blinked the LED0
    previousMillis3 += interval3;   

    // if the LED is off turn it on and vice-versa:
    if (ledState3 == LOW)
      ledState3 = HIGH;
      ledState3 = LOW;

    // set the LEDs with the ledStates of the variable:
    digitalWrite(ledPin0, ledState0);
    digitalWrite(ledPin1, ledState1);
    digitalWrite(ledPin2, ledState2);
    digitalWrite(ledPin3, ledState3);


There are other ways to do this using arrays rather than separate variables.
Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - https://tinyurl.com/Technote8
Multitasking: forum.arduino.cc/index.php?topic=223286.0
gammon.com.au/blink - gammon.com.au/serial - gammon.com.au/interrupts

Go Up