Help Needed on Quick Flash happening during LED chase sequence

I'm relatively new to Arduino, but I've created a chase sequence using 12 LEDs. I'm trying to use custom functions to learn about how they work. The code compiles fine and downloads to my Arduino Nano. The intent is for the LED chase to start slow and increase in speed until all LEDs stay lit before turning off and resetting for the next chase sequence. The trouble is that I have a "blip" or a "flash" happening on all LEDs just before the first LED lights in the chase sequence. I can't for the life of me explain why this is happening. Can someone please look at the code to see what I might be doing wrong? I've tried commenting the code to help show my intention. Thanks in advance for any help you may offer.

//this code is create an LED Chaser using 12 LEDs: three blue, 3 green, three yellow, and three white
//the wiring runs from Nano I/O pin to resistor; resistor is connected to LED anode; LED cathode is connected to ground
int bLED1 = 2;
int bLED2 = 3;
int bLED3 = 4;
int gLED1 = 5;
int gLED2 = 6;
int gLED3 = 7;
int yLED1 = 8;
int yLED2 = 9;
int yLED3 = 10;
int wLED1 = 11;
int wLED2 = 12;
int wLED3 = 13;
int dT = 100;  //delay time
int dTchase;   //delay time variable for chase function

void setup() {
  // put your setup code here, to run once:
  // pinModes set as outputs for LEDs
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  //to call the function use: voidname();
  allLEDoff();  //call function to digitalWrite all LED pins low
  delay(dT);
  chase();        //runs the chase function
  dTchase = 125;  //changes the delay variable
  // the pattern continues gradually decreasing the delay so the LEDs appear to move faster
  chase();
  dTchase = 100;
  chase();
  dTchase = 83;
  chase();
  dTchase = 75;
  chase();
  dTchase = 62;
  chase();
  dTchase = 50;
  chase();
  dTchase = 38;
  chase();
  dTchase = 25;
  chase();
  dTchase = 12;
  chase();
  dTchase = 12;
  chase();
  dTchase = 8;
  chase();
  dTchase = 8;
  chase();
  dTchase = 5;
  chase();
  dTchase = 5;
  chase();
  dTchase = 4;
  chase();
  dTchase = 3;
  chase();
  dTchase = 2;
  chase();
  dTchase = 1;
  allLEDon();   // calls the function to turn on all the LEDs
  delay(3000);  // holds the LEDs on
  allLEDoff();  //calls the function to turn off all LEDs
  delay(6000);  // holds LEDs off
}

void chase() {  //this is where you digital write the LED pins on and off in chase pattern
  delay(dT);
  digitalWrite(bLED1, HIGH);
  delay(dTchase);
  digitalWrite(bLED2, HIGH);
  delay(dTchase);
  digitalWrite(bLED3, HIGH);
  delay(dTchase);
  digitalWrite(gLED1, HIGH);
  digitalWrite(bLED1, LOW);
  delay(dTchase);
  digitalWrite(gLED2, HIGH);
  digitalWrite(bLED2, LOW);
  delay(dTchase);
  digitalWrite(gLED3, HIGH);
  digitalWrite(bLED3, LOW);
  delay(dTchase);
  digitalWrite(yLED1, HIGH);
  digitalWrite(gLED1, LOW);
  delay(dTchase);
  digitalWrite(yLED2, HIGH);
  digitalWrite(gLED2, LOW);
  delay(dTchase);
  digitalWrite(yLED3, HIGH);
  digitalWrite(gLED3, LOW);
  delay(dTchase);
  digitalWrite(wLED1, HIGH);
  digitalWrite(yLED1, LOW);
  delay(dTchase);
  digitalWrite(wLED2, HIGH);
  digitalWrite(yLED2, LOW);
  delay(dTchase);
  digitalWrite(wLED3, HIGH);
  digitalWrite(yLED3, LOW);
  delay(dTchase);
  digitalWrite(wLED1, LOW);
  delay(dTchase);
  digitalWrite(wLED2, LOW);
  delay(dTchase);
  digitalWrite(wLED3, LOW);
  delay(dT);
}
void allLEDon() {
  digitalWrite(bLED1, HIGH);
  digitalWrite(bLED2, HIGH);
  digitalWrite(bLED3, HIGH);
  digitalWrite(gLED1, HIGH);
  digitalWrite(gLED2, HIGH);
  digitalWrite(gLED3, HIGH);
  digitalWrite(yLED1, HIGH);
  digitalWrite(yLED2, HIGH);
  digitalWrite(yLED3, HIGH);
  digitalWrite(wLED1, HIGH);
  digitalWrite(wLED2, HIGH);
  digitalWrite(wLED3, HIGH);
}
void allLEDoff() {
  digitalWrite(bLED1, LOW);
  digitalWrite(bLED2, LOW);
  digitalWrite(bLED3, LOW);
  digitalWrite(gLED1, LOW);
  digitalWrite(gLED2, LOW);
  digitalWrite(gLED3, LOW);
  digitalWrite(yLED1, LOW);
  digitalWrite(yLED2, LOW);
  digitalWrite(yLED3, LOW);
  digitalWrite(wLED1, LOW);
  digitalWrite(wLED2, LOW);
  digitalWrite(wLED3, LOW);
}

I have not looked at your code in detail but the first thing that I would urge you to do is to learn to read about and use arrays. Your code could be reduced by at least 50%, probably more, and this would make it easier to debug and maintain

Hello chiphowat

As mentioned by UKHeliBob arrays and structs are your friends.

For your project, I found a chaser sketch in my sketchbox that you can may customise to your needs.

//https://forum.arduino.cc/t/help-needed-on-quick-flash-happening-during-led-chase-sequence/1206800
//https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
#define ProjectName "Help Needed on Quick Flash happening during LED chase sequence"
#define NotesOnRelease "Arduino MEGA tested"
// make variables
constexpr uint8_t LedPins[] {9, 10, 11, 12};
struct CHASER
{
  uint32_t chaseTime;
  uint8_t   sequence[sizeof(LedPins)];
} chasers[]
{
  // wait time,led0,led1,led2,led3
  {1000, 0, 0, 0, 0},
  {1000, 1, 0, 0, 0},
  {1000, 0, 1, 0, 0},
  {1000, 0, 0, 1, 0},
  {1000, 0, 0, 0, 1},
};
// make application
void setup()
{
  Serial.begin(115200);
  Serial.print("Source: "), Serial.println(__FILE__);
  Serial.print(ProjectName), Serial.print(" - "), Serial.println(NotesOnRelease);
  // initialize leds
  for (auto LedPin : LedPins) pinMode(LedPin, OUTPUT);
  Serial.println(" =-> and off we go\n");
}
void loop()
{
  uint32_t currentMillis = millis();
  static uint32_t chaseNow = currentMillis;
  static uint32_t chaseInterval = 0;
  if (currentMillis - chaseNow >= chaseInterval)
  {
    static uint8_t element = 0;
    chaseNow = currentMillis;
    chaseInterval = chasers[element].chaseTime;
    uint8_t pattern = 0;
    for (auto LedPin : LedPins) digitalWrite(LedPin, chasers[element].sequence[pattern++]);
    element = (element + 1) % (sizeof(chasers) / sizeof(chasers[0]));
  }
}

Have a nice day and enjoy coding in C++.

2 Likes

Increase the resistance of the current limiting resistors you are using. Even the simulator sees the immediate draw of current, biasing the LEDs to "flash". Compare these two simulations...

Simulator is remembering values from previous sketch. After loading the simulation, F5/refresh your browser to remove artifacts from previous sketch.

Flash (300 ohm resistors)

No flash (300k ohm resistors)

Thanks, I'll take some time to figure out arrays and how to use them.

The wokwi site is pretty amazing. Had no idea that it existed. I'll try swapping out the resistors. I'm currently using 220 ohm resistors so I'll go for something bigger and see what happens. Plenty of things for me to unpack and learn about how you coded a chase sequence.

The simulator seems to "flash" all the time now.

I tried setting all the anode pinMode() to INPUT in setup(), then set the pins LOW before setting the pinMode() to OUTPUT... but still the flash... I hope the realworld project goes better.

In the WOKWI, try adding the highlighted line:

image

All, UPDATE: I tried changing out the resistors, but the flash persisted. I then took the advice to learn and use arrays and reworked the code. As it turns out, I solved the flash issue in the redo. I'll copy the solution I came up with below. Thanks to everyone for all the help and support.

/*
this code is create an LED Chaser using 12 LEDs: three blue, 3 green, three yellow, and three white
the chase sequence gradually speeds up until all LEDs stay lit, then turns off and resets
the circuit goes from Nano I/O pin to 330 ohm resistor;
330 resistor to LED anode; LED cathode is connected to ground
*/

int timer;                                                   // lower is faster, higher is slower
int ledPins[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };  // an array of pin numbers to which LEDs are attached
int pinCount = 12;                                           // the number of pins (i.e. the length of the array)
int arrayPin;
int dT = 3000;

void chase() {
  // loop from the lowest pin to the highest:
  for (arrayPin = 0; arrayPin < pinCount; arrayPin++) {
    // turn the pin on:
    digitalWrite(ledPins[arrayPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[arrayPin], LOW);
  }
}

void allLEDon() {
  // turns all pins on at same time
  for (arrayPin = 0; arrayPin < pinCount; arrayPin++) {
    // turn the pin on:
    digitalWrite(ledPins[arrayPin], HIGH);
  }
}

void allLEDoff() {
  //turns all LEDs off at same time
  for (arrayPin = 0; arrayPin < pinCount; arrayPin++) {
    // turn the pin on:
    digitalWrite(ledPins[arrayPin], LOW);
  }
}
void setup() {
  // the array elements are numbered from 0 to (pinCount - 1)
  // use a for loop to initialize each pin as an output:
  for (arrayPin = 0; arrayPin < pinCount; arrayPin++) {
    pinMode(ledPins[arrayPin], OUTPUT);
  }
}

void loop() {
  timer = 80;
  while (timer >= 15) {
    timer = timer - 5;
    chase();
  }
  if (timer == 10) {
    chase();
    chase();
    chase();
    chase();
    chase();
  }
  allLEDon();
  delay(dT);
  allLEDoff();
  delay(dT);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.