Blink w/Out Delay issue.

I am trying to modify the Blinkw/o Delay program.

But what i want isnt working quite right:

i want leds to light in this pattern:

o
oo
ooo
oooo
ooooo

o
oo
ooo
oooo
ooooo
-etc.

THIS IS MY CODE:

const int ledPin = 31;   
const int ledPinA = 33; 
const int ledPinB = 35; 
const int ledPinC = 37; 
const int ledPinD = 39; 
const int ledPinE = 41; 
const int ledPinF = 43; 
const int ledPinG = 45;
const int ledPinH = 47; 
const int ledPinJ = 49; 
const int ledPinK = 51; 
const int ledPinL = 53; 

int ledState = LOW;    
int ledStateA = LOW;
int ledStateB = LOW;
int ledStateC = LOW;
int ledStateD = LOW;
int ledStateE = LOW;
int ledStateF = LOW;
int ledStateG = LOW;
int ledStateH = LOW;
int ledStateJ = LOW;
int ledStateK = LOW;
int ledStateL = LOW;

long previousMillis = 0;        
long interval = 200;         
long previousMillisA = 0;        
long intervalA = 400;  
long previousMillisB = 0;        
long intervalB = 600;  
long previousMillisC = 0;        
long intervalC = 800;  
long previousMillisD = 0;        
long intervalD = 1000;  
long previousMillisE = 0;        
long intervalE = 1200;  
long previousMillisF = 0;        
long intervalF = 1400;  
long previousMillisG = 0;        
long intervalG = 1600; 
long previousMillisH = 0;        
long intervalH = 1800;  
long previousMillisJ = 0;        
long intervalJ = 2000;  
long previousMillisK = 0;        
long intervalK = 2200;  
long previousMillisL = 0;        
long intervalL = 2400;  

void setup() {

  pinMode(ledPin, OUTPUT);      
  pinMode(ledPinA, OUTPUT); 
  pinMode(ledPinB, OUTPUT); 
  pinMode(ledPinC, OUTPUT); 
  pinMode(ledPinD, OUTPUT); 
  pinMode(ledPinE, OUTPUT); 
  pinMode(ledPinF, OUTPUT); 
  pinMode(ledPinG, OUTPUT); 
  pinMode(ledPinH, OUTPUT); 
  pinMode(ledPinJ, OUTPUT); 
  pinMode(ledPinK, OUTPUT); 
  pinMode(ledPinL, OUTPUT); 
}

void loop()
{

  unsigned long currentMillis = millis();
  unsigned long currentMillisA = millis();
  unsigned long currentMillisB = millis();
  unsigned long currentMillisC = millis();
  unsigned long currentMillisD = millis();
  unsigned long currentMillisE = millis();
  unsigned long currentMillisF = millis();
  unsigned long currentMillisG = millis();
  unsigned long currentMillisH = millis();
  unsigned long currentMillisJ = millis();
  unsigned long currentMillisK = millis();
  unsigned long currentMillisL = millis();  
  
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    digitalWrite(ledPin, ledState);

    if(currentMillisA - previousMillisA > intervalA) {
      // save the last time you blinked the LED 
      previousMillisA = currentMillisA;   
      // if the LED is off turn it on and vice-versa:
      if (ledStateA == LOW)
        ledStateA = HIGH;
      else
        ledStateA = LOW;
      digitalWrite(ledPinA, ledStateA);
    }{
      if(currentMillisB - previousMillisB > intervalB) {
        // save the last time you blinked the LED 
        previousMillisB = currentMillisB;   
        // if the LED is off turn it on and vice-versa:
        if (ledStateB == LOW)
          ledStateB = HIGH;
        else
          ledStateB = LOW;
        digitalWrite(ledPinB, ledStateB);
      }{
        if(currentMillisC - previousMillisC > intervalC) {
          // save the last time you blinked the LED 
          previousMillisC = currentMillisC;   
          // if the LED is off turn it on and vice-versa:
          if (ledStateC == LOW)
            ledStateC = HIGH;
          else
            ledStateC = LOW;
          digitalWrite(ledPinC, ledStateC);
        }{
          if(currentMillisD - previousMillisD > intervalD) {
            // save the last time you blinked the LED 
            previousMillisD = currentMillisD;   
            // if the LED is off turn it on and vice-versa:
            if (ledStateD == LOW)
              ledStateD = HIGH;
            else
              ledStateD = LOW;
            digitalWrite(ledPinD, ledStateD);
          }{
            if(currentMillisE - previousMillisE > intervalE) {
              // save the last time you blinked the LED 
              previousMillisE = currentMillisE;   
              // if the LED is off turn it on and vice-versa:
              if (ledStateE == LOW)
                ledStateE = HIGH;
              else
                ledStateE = LOW;
              digitalWrite(ledPinE, ledStateE);
            }{
              if(currentMillisF - previousMillisF > intervalF) {
                // save the last time you blinked the LED 
                previousMillisF = currentMillisF;   
                // if the LED is off turn it on and vice-versa:
                if (ledStateF == LOW)
                  ledStateF = HIGH;
                else
                  ledStateF = LOW;
                digitalWrite(ledPinF, ledStateF);
              }{
                if(currentMillisG - previousMillisG > intervalG) {
                  // save the last time you blinked the LED 
                  previousMillisG = currentMillisG;   
                  // if the LED is off turn it on and vice-versa:
                  if (ledStateG == LOW)
                    ledStateG = HIGH;
                  else
                    ledStateG = LOW;
                  digitalWrite(ledPinG, ledStateG);
                }{
                  if(currentMillisH - previousMillisH > intervalH) {
                    // save the last time you blinked the LED 
                    previousMillisH = currentMillisH;   
                    // if the LED is off turn it on and vice-versa:
                    if (ledStateH == LOW)
                      ledStateH = HIGH;
                    else
                      ledStateH = LOW;
                    digitalWrite(ledPinH, ledStateH);
                  }{
                    if(currentMillisJ - previousMillisJ > intervalJ) {
                      // save the last time you blinked the LED 
                      previousMillisJ = currentMillisJ;   
                      // if the LED is off turn it on and vice-versa:
                      if (ledStateJ == LOW)
                        ledStateJ = HIGH;
                      else
                        ledStateJ = LOW;
                      digitalWrite(ledPinJ, ledStateJ);
                    }{
                      if(currentMillisK - previousMillisK > intervalK) {
                        // save the last time you blinked the LED 
                        previousMillisK = currentMillisK;   
                        // if the LED is off turn it on and vice-versa:
                        if (ledStateK == LOW)
                          ledStateK = HIGH;
                        else
                          ledStateK = LOW;
                        digitalWrite(ledPinK, ledStateK);
                      }
                      {
                        if(currentMillisL - previousMillisL > intervalL) {
                          // save the last time you blinked the LED 
                          previousMillisL = currentMillisL;   
                          // if the LED is off turn it on and vice-versa:
                          if (ledStateL == LOW)
                            ledStateL = HIGH;
                          else
                            ledStateL = LOW;
                          digitalWrite(ledPinL, ledStateL);
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

what’s happening looks more like an 8-bit counter than a steady incremental sequence.

Any words of wisdom?

THANKS!

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

How many LEDs? The diagram shows 5? But 12 in the code? Maybe you just simplified the diagram?

Im using a mega. And those are the pins im using.

There are 12 lights that need to be in sequence.

Give this a shot

byte LED[] = {31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53};
int ledMask;
unsigned long ms, msLast;
#define ledInterval 200    //milliseconds

void setup(void)
{
    for (int i=0; i<sizeof(LED); i++) {
        pinMode(LED[i], OUTPUT);
    }
}

void loop(void)
{
    ms = millis();
    if (ms - msLast > ledInterval) {
        msLast = ms;
        ledMask = ledMask * 2 + 1;
        if (ledMask > (1 << sizeof(LED)) - 1) ledMask = 0;
        for (int i=0; i<sizeof(LED); i++) {
            digitalWrite(LED[i], ledMask & (1 << i));
        }
    }
}

Works...except for the last 4 leds...

i keep looking over the code...(which kinda makes sense, im new to this)

but i don't see where it wouldnt WANT to trigger 47, 49, 51, 53.

its like it may be "overflowing" before it gets to 47, because up to that output it works fine.

Cheers and thanks for your guidance!

I believe with digitalWrite(pin, value), value is just an 8 bit value, thus once your input mask exceeds 8 bits, the cast drops those bits and you end up with an input value of zero (or low).

Try setting your value to: ledMask & (1 << i) > 0

This should evaluate to true/false → High/Low for all values of ledMask.

ok i did that^ and now they all flash, only they do it all at once…

my head hurts…

but cant I do use: unsigned int. instead? it’s 16-bit.

or is my logic still flawed?

Sorry about that, digitalWrite does want an 8-bit value, doesn’t it. See if this is any better.

byte LED[] = {31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53};
int ledMask;
unsigned long ms, msLast;
#define ledInterval 200    //milliseconds

void setup(void)
{
    for (int i=0; i<sizeof(LED); i++) {
        pinMode(LED[i], OUTPUT);
    }
}

void loop(void)
{
    ms = millis();
    if (ms - msLast > ledInterval) {
        msLast = ms;
        ledMask = ledMask * 2 + 1;
        if (ledMask > (1 << sizeof(LED)) - 1) ledMask = 0;
        for (int i=0; i<sizeof(LED); i++) {
            digitalWrite(LED[i], (ledMask & (1 << i)) != 0);
        }
    }
}

RazorFly: but cant I do use: unsigned int. instead? it's 16-bit.

Either will work in this case, since only 12 bits are needed.

EDIT: But I might prefer unsigned as a matter of style, because it better reflects how the variable is used. What can I say, I wrote it in a hurry :blush:

Awesome...Works Great! Its so simple!

Good deal! :)