Blink_Without_Delay Modification

Hello,

I have read through the examples and am trying to teach myself C++ so this may be a basic question but I have not seen an example in which I can modify to meet my requirments so I am reaching out. I would like to modify the Blink_Without_Delay code to include a counter that I can use to trigger other LEDs to activate at specific counts. I would like to make the code robust enough to have 34 LEDs but I would like to understand the full concept with one or two prior to expanding.

I cannot create the code that will activate the additional LEDs off of a counter. The code below will run without any problems but just LED1 will preform the desired task properly.

Program Scope: LED1 : to activate and deactivate at a constant interval (Blink_without_Delay) Counter: to track the number of times LED1 has activated LED2: To ativate on counts 5,8,10,15 and to be deactivated on all other counts LED3: To ativate on counts 3,7,12,13 and to be deactivated on all other counts

Example Code: (Have tried multiple variations but this is the example that will show the direction I am heading.
/* Blink without Delay Modified

Turns on and off a light emitting diode(LED) connected to a digital  
pin, without using the delay() function.  This means that other code
can run at the same time without being interrupted by the LED code.

The circuit:
* LED attached from pin 13 to ground.
* Note: on most Arduinos, there is already an LED on the board
that's attached to pin 13, so no hardware is needed for this example.


created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen

This example code is in the public domain.


http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin
const int ledPin_a = 9;

// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
int ledPincount = 0;
int ledState_a=LOW;
// 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 interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
 // set the digital pin as output:
 pinMode(ledPin, OUTPUT);  
 pinMode(ledPin_a, OUTPUT); 
Serial.begin(9600); 
}

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.
 unsigned long currentMillis = 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;

   // set the LED with the ledState of the variable:
   digitalWrite(ledPin, ledState);
 }
 {
    if (ledState != ledState) {
   // if the state has changed, increment the counter
   if (ledState == HIGH) {
     // if the current state is HIGH then the button
     // wend from off to on:
     ledPincount++;
     
   }
    }
 }
 {
 if (ledPincount == 5,8,10,15 )
 ledState_a =HIGH;
 else
 ledState_a=LOW;

 }
}

I apprciate an insight that can be provided into my problem and thank you in advance.

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

You should merge these two IF statements into one

 if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
 
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
  {
     if (ledState != ledState) {
    // if the state has changed, increment the counter
    if (ledState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      ledPincount++;
     
    }
    }

and you have unnecessary {}

For this piece

if (ledPincount == 5,8,10,15 )
  ledState_a =HIGH;
  else
  ledState_a=LOW;
 
  }

you will need a more complex IF statement, or for better clarity several of them

ledState_a = LOW;
if (ledPincount == 5 ) {
  ledState_a =HIGH;
}
else if (ledPinCount == 8) {
  ledState_a = HIGH;
}
// etc
}

You will also need code somewhere to set the count back to 0 (assuming you want the sequence to repeat.

However if the purpose of the counting is to give different leds different timings it may be easier to use a few different sets of

if(currentMillis - previousMillis > interval) {

with, for example, intervalA, intervalB etc.

The demo Several Things at a Time is an extended example of BWod.

…R

}

    if (ledState != ledState)

How likely is it that the value of a variable will not equal the value of the same variable ?
Shouldn’t there be a previousLedState variable in there somewhere ?

    if (ledPincount == 5, 8, 10, 15 )

I think you mean

if (ledPincount == 5 || ledPincount == 8 || ledPincount == 10 || ledPincount == 15 )

Thank you for your quick response. (I forgot to include I am using a MEGA)

I should explain the purpose of my project to give a little more for insight into my attempt at programming.

This is going to be a tool that I can use to teach violin to myself. So the LED1 is acting as the beat of the music. The addition LEDs will light up at the note/spot on the neck of the violin to indicate which note should be played . Once I understand the concept of triggering with count I will program a reset and start function (with a push button (I understand the example program so I am hoping I will not have an issue). As of right now I believe I am missing something very obvious but I just cannot see it. I believe the manner in which I have programmed the count function is not working properly or the read function for the LEDs is incorrect.

I read the Several Things at a Time program multiple times and it was extremely informative but I am afraid that if I use a similar structure the code will become to cumbersome to program and entire song of sheet music. This is why I am wanting to use the count function for the LED1 as the central trigger to all other LED states. My mindset may be stuck with the principals of ladder logic and G-code, so if you see me stuck because I am missing a concept please feel free to point me towards a resource and I will do my part to teach it myself.

Sidenote:(I am using the following as my guides to teach myself, if there are better references please let me know) Adruino Sketches: By James A. Langbridge C++ : By Siddhartha Roa And Google (Can find everything on Google)

I took you feedback above and modified my program. (Count function is still not triggering.) I will continue to work on this and post if I resolve this prior to your response.

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin
const int ledPin_a = 9;

// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
int ledPincount = 0;
int ledState_a=LOW;
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
 // set the digital pin as output:
 pinMode(ledPin, OUTPUT);  
 pinMode(ledPin_a, OUTPUT); 
Serial.begin(9600); 
}

void loop()
{
 
 unsigned long currentMillis = 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;
 
   // set the LED with the ledState of the variable:
   digitalWrite(ledPin, ledState);  
       if (ledState == HIGH) {
         ledPincount++;
         }
    }
 if (ledPincount == 5 || ledPincount == 8 || ledPincount == 10 || ledPincount == 15 ) {
 ledState_a =HIGH;
}
else  {
 ledState_a = LOW;
}
 
 }

I would use a switch for the functionality, like this:

const byte led1Pin = 13;
const byte led2Pin = 9;
const byte led3Pin = 8;

unsigned long previousMillis;
unsigned long interval = 1000;

void setup() {
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
}

byte numberOfBlinks;

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;

    digitalWrite(led1Pin, !digitalRead(led1Pin));
    if (digitalRead(led1Pin)) {
      if (++numberOfBlinks > 15) {
        numberOfBlinks = 0;
      }
      switch (numberOfBlinks) {
        case 5:
        case 8:
        case 10:
        case 15:
          digitalWrite(led2Pin, HIGH);
          digitalWrite(led3Pin, LOW);
          break;
        case 3:
        case 7:
        case 12:
        case 13:
          digitalWrite(led3Pin, HIGH);
          digitalWrite(led2Pin, LOW);
          break;
        default:
          digitalWrite(led2Pin, LOW);
          digitalWrite(led3Pin, LOW);
      }
    }
  }
}

Whandall,

Thank you for the introduction to case this is exactly what I was looking for. This should allow me to compete this experiment in a compact manner.

I will make one change to (besides adding all of the additional LEDs)

byte numberOfBlinks;

I will change it to an unsigned int , to increase the number of blinks it is capable of counting.

Once again thank you to everyone for their input.

As simple as I can make it :)

tested works 100%

/*Program Scope:
LED1 : to activate and deactivate at a constant interval (Blink_without_Delay)
Counter: to track the number of times LED1 has activated
LED2: To ativate on counts 5,8,10,15 and to be deactivated on all other counts
LED3: To ativate on counts 3,7,12,13 and to be deactivated on all other counts
*/
int LED1Pin = 13;
int LED2Pin = 6;
int LED3Pin = 7;

// COUNTS                 0    1    2    3    4    5    6    7    8    9   10    11   12   13   14  15    16   17   18   19
bool LED2onArray[20] = { LOW, LOW, LOW, LOW, LOW, HIGH, LOW, LOW, HIGH, LOW, HIGH, LOW, LOW, LOW, LOW, HIGH, LOW, LOW, LOW, LOW};
bool LED3onArray[20] = { LOW, LOW, LOW, HIGH, LOW, LOW, LOW, HIGH, LOW, LOW, LOW, LOW, HIGH, HIGH, LOW, LOW, LOW, LOW, LOW, LOW};

int Counter = 0;

void setup() {
  Serial.begin(115200); //115200
  while (!Serial) {

  }
  pinMode(LED1Pin, OUTPUT);
  pinMode(LED2Pin, OUTPUT);
  pinMode(LED3Pin, OUTPUT);
}

void loop() {
  for (static unsigned long QTimer = millis(); (long)( millis() - QTimer ) >= 1000; QTimer = millis() ) { // one second delay
    if (Counter == 20)Counter = 0;
    digitalWrite(LED1Pin, !digitalRead(LED1Pin));
    digitalWrite(LED2Pin, LED2onArray[Counter]);
    digitalWrite(LED3Pin, LED3onArray[Counter]);
    Serial.print(millis());
    Serial.print("\t Counter = "); Serial.print(Counter);
    Serial.print("\t LED1 = "); Serial.print(digitalRead(LED1Pin));
    Serial.print("\t LED2 = "); Serial.print(digitalRead(LED2Pin));
    Serial.print("\t LED3 = "); Serial.print(digitalRead(LED3Pin));
    Serial.println();
    Counter++;
  }
}

oops after re-reading i spotted a sequence error

Counter: to track the number of times LED1 has activated

my prior code activated counter at each time interval

Corrected code:

/*Program Scope:
LED1 : to activate and deactivate at a constant interval (Blink_without_Delay)
Counter: to track the number of times LED1 has activated
LED2: To ativate on counts 5,8,10,15 and to be deactivated on all other counts
LED3: To ativate on counts 3,7,12,13 and to be deactivated on all other counts
*/
int LED1Pin = 13;
int LED2Pin = 6;
int LED3Pin = 7;

// COUNTS                 0    1    2    3    4    5    6    7    8    9   10    11   12   13   14  15    16   17   18   19
bool LED2onArray[20] = { LOW, LOW, LOW, LOW, LOW, HIGH, LOW, LOW, HIGH, LOW, HIGH, LOW, LOW, LOW, LOW, HIGH, LOW, LOW, LOW, LOW};
bool LED3onArray[20] = { LOW, LOW, LOW, HIGH, LOW, LOW, LOW, HIGH, LOW, LOW, LOW, LOW, HIGH, HIGH, LOW, LOW, LOW, LOW, LOW, LOW};

int Counter = 0;

void setup() {
  Serial.begin(115200); //115200
  while (!Serial) {

  }
  pinMode(LED1Pin, OUTPUT);
  pinMode(LED2Pin, OUTPUT);
  pinMode(LED3Pin, OUTPUT);
}

void loop() {
  for (static unsigned long QTimer = millis(); (long)( millis() - QTimer ) >= 1000; QTimer = millis() ) { // one second delay
    digitalWrite(LED1Pin, !digitalRead(LED1Pin));
    Counter += digitalRead(LED1Pin);
    if (Counter == 20)Counter = 0;
    digitalWrite(LED2Pin, LED2onArray[Counter]);
    digitalWrite(LED3Pin, LED3onArray[Counter]);
    Serial.print(millis());
    Serial.print("\t Counter = "); Serial.print(Counter);
    Serial.print("\t LED1 = "); Serial.print(digitalRead(LED1Pin));
    Serial.print("\t LED2 = "); Serial.print(digitalRead(LED2Pin));
    Serial.print("\t LED3 = "); Serial.print(digitalRead(LED3Pin));
    Serial.println();
    
  }
}

Serial output:

1000     Counter = 1     LED1 = 1    LED2 = 0    LED3 = 0
2000     Counter = 1     LED1 = 0    LED2 = 0    LED3 = 0
3000     Counter = 2     LED1 = 1    LED2 = 0    LED3 = 0
4000     Counter = 2     LED1 = 0    LED2 = 0    LED3 = 0
5000     Counter = 3     LED1 = 1    LED2 = 0    LED3 = 1
6000     Counter = 3     LED1 = 0    LED2 = 0    LED3 = 1
7000     Counter = 4     LED1 = 1    LED2 = 0    LED3 = 0
8000     Counter = 4     LED1 = 0    LED2 = 0    LED3 = 0
9000     Counter = 5     LED1 = 1    LED2 = 1    LED3 = 0
10000    Counter = 5     LED1 = 0    LED2 = 1    LED3 = 0
11000    Counter = 6     LED1 = 1    LED2 = 0    LED3 = 0
12000    Counter = 6     LED1 = 0    LED2 = 0    LED3 = 0
13000    Counter = 7     LED1 = 1    LED2 = 0    LED3 = 1
14000    Counter = 7     LED1 = 0    LED2 = 0    LED3 = 1
15000    Counter = 8     LED1 = 1    LED2 = 1    LED3 = 0
16000    Counter = 8     LED1 = 0    LED2 = 1    LED3 = 0
17000    Counter = 9     LED1 = 1    LED2 = 0    LED3 = 0
18000    Counter = 9     LED1 = 0    LED2 = 0    LED3 = 0
19000    Counter = 10    LED1 = 1    LED2 = 1    LED3 = 0
20000    Counter = 10    LED1 = 0    LED2 = 1    LED3 = 0
21000    Counter = 11    LED1 = 1    LED2 = 0    LED3 = 0
22000    Counter = 11    LED1 = 0    LED2 = 0    LED3 = 0
23000    Counter = 12    LED1 = 1    LED2 = 0    LED3 = 1
24000    Counter = 12    LED1 = 0    LED2 = 0    LED3 = 1
25000    Counter = 13    LED1 = 1    LED2 = 0    LED3 = 1
26000    Counter = 13    LED1 = 0    LED2 = 0    LED3 = 1
27000    Counter = 14    LED1 = 1    LED2 = 0    LED3 = 0
28000    Counter = 14    LED1 = 0    LED2 = 0    LED3 = 0
29000    Counter = 15    LED1 = 1    LED2 = 1    LED3 = 0
30000    Counter = 15    LED1 = 0    LED2 = 1    LED3 = 0
31000    Counter = 16    LED1 = 1    LED2 = 0    LED3 = 0
32000    Counter = 16    LED1 = 0    LED2 = 0    LED3 = 0
33000    Counter = 17    LED1 = 1    LED2 = 0    LED3 = 0
34000    Counter = 17    LED1 = 0    LED2 = 0    LED3 = 0
35000    Counter = 18    LED1 = 1    LED2 = 0    LED3 = 0
36000    Counter = 18    LED1 = 0    LED2 = 0    LED3 = 0
37000    Counter = 19    LED1 = 1    LED2 = 0    LED3 = 0
38000    Counter = 19    LED1 = 0    LED2 = 0    LED3 = 0
39000    Counter = 0     LED1 = 1    LED2 = 0    LED3 = 0
40000    Counter = 0     LED1 = 0    LED2 = 0    LED3 = 0
41000    Counter = 1     LED1 = 1    LED2 = 0    LED3 = 0
42000    Counter = 1     LED1 = 0    LED2 = 0    LED3 = 0
43000    Counter = 2     LED1 = 1    LED2 = 0    LED3 = 0
44000    Counter = 2     LED1 = 0    LED2 = 0    LED3 = 0
45000    Counter = 3     LED1 = 1    LED2 = 0    LED3 = 1