The blinking LEDs wont run in Sequence 3 codes

Hi I would appreciate help with the following codes. My project is a 12 LED clock, a LED is on for 5 mins. then next and start again. I can get LEDs blinking every whcih way but not a long string of 12 LED . I am studying Millis and trying to reduce Delay

Using Arduino Uno 1.8.18 The test setup is a simple 4 LED hook up with resistors
I am a bit mistified here as posting code within the tags , the code does not apeear in color
The first code is great but the Timer function freezes if more than 30 secs.

/*
  TIMER freezes  slower than 30 secs
  ARRAYS https://www.arduino.cc/en/Tutorial/BuiltInExamples/Arrays
  30,000 = 30 secs  300,000 = 5 mins   50,000 = .83 mins
  int timer = 5UL*60UL*1000UL;  // 1,200,000 = 20 mins  // 1,800,000 = 30 mins
*/
int timer = 1000;
int ledPins[] = { 4, 5, 6, 7};
int pinCount = 4;

void setup()
{

  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
  }
}

void loop()
{
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    // turn the pin on:
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(ledPins[thisPin], LOW);

  }
}
/////////////////////////////////////////////

the next code is brilliant but my knowledge of C++ is insuffecient to change it

/* previous LED slow to turn off
  a brilliant sketch by paulpaulson
  from forum "how do I blink multiple LEDs in sequence"
*/
int ledPin9 = 9;
int ledPin8 = 8;
int ledPin7 = 7;
int ledPin6 = 6;
int ledPin5 = 5;
int ledPin4 = 4;
int ledPin3 = 3;
int ledPin2 = 2;
/// new
byte ledSequ [][8]
{
  {1, 1, 0, 0, 0, 0, 0, 0},
  {0, 1, 1, 0, 0, 0, 0, 0},
  {0, 0, 1, 1, 0, 0, 0, 0},
  {0, 0, 0, 1, 1, 0, 0, 0},
  {0, 0, 0, 0, 1, 1, 0, 0},
  {0, 0, 0, 0, 0, 1, 1, 0},
  {0, 0, 0, 0, 0, 0, 1, 1},
};
// new
byte leds[] {ledPin2, ledPin3, ledPin4, ledPin5, ledPin6, ledPin7, ledPin8, ledPin9};
void setup()
{
  Serial.begin(9600);
  pinMode(ledPin9, OUTPUT);
  pinMode(ledPin8, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}
void loop() {
  const unsigned long blink = 1000;
  static unsigned long blinkMillis;
  if (millis() - blinkMillis >= blink ) {
    static int nummer;
    blinkMillis = millis();
    Serial.println("blink");
    for (unsigned int n = 0; n < sizeof(leds); n++) digitalWrite(leds[n], ledSequ[nummer][n]);
    nummer++;
    nummer = nummer % (sizeof(ledSequ) / sizeof(leds));
  }
}
/////////////////////////////////////////////////

The next code is twoledswalking which I am unable to modify

/*
  need single LEDs blinking in sequence
  from wokwi twoledswalking.ino
*/
const int ledPin[] = { 2, 3, 4, 5, 6, 7, 8, 9 };
const int numPins = sizeof(ledPin) / sizeof(ledPin[0]);
const int numStates = numPins / 2;

const unsigned long interval = 1000;
unsigned long previousTime;
int state;

void setup()
{
  for ( int i = 0; i < numPins; ++i )
  {
    pinMode(ledPin[i], OUTPUT);
    digitalWrite(ledPin[i], LOW );
  }
  digitalWrite(ledPin[0], HIGH );
  digitalWrite(ledPin[1], HIGH );//
}

void loop() {

  unsigned long currentMillis = millis();

  if (currentMillis - previousTime >= interval) {
    previousTime = currentMillis;
    digitalWrite(ledPin[state * 2], LOW);
    digitalWrite(ledPin[state * 2 + 1], LOW);
    state++;
    if ( state >= numStates ) {
      state = 0;
    }
    digitalWrite(ledPin[state * 2], HIGH);
    digitalWrite(ledPin[state * 2 + 1], HIGH);
  }
}

Thanks for having a look at this.
This a great forum with some very good programmers

you messed up the use of the code tags, I fixed that for you this time as you tried.

Take a moment to read How to get the best out of this forum and post accordingly in the future


you would benefit in reading information and examples at

not sure what your 3 programs are trying to do

look this over

it turns on the next 5 min LED
it turns all but the 1st LED off after the 5 min interval expires when all 12 are on. it starts with them all off

const byte PinMinLeds [] = {
    13, 12, 11,  10,  9,  8, 
     7,  6,  5,  A4, A3, A2
};
const int NminLeds = 4;  //sizeof(PinMinLeds);

int minLedIdx;

enum { Off = HIGH, On = LOW };

const unsigned long FiveMins = 3000;    // 5 * 60 * 1000L;
unsigned long       msecLst;

// -----------------------------------------------------------------------------
void
loop (void)
{
    unsigned long msec = millis ();

    if (msec - msecLst >= FiveMins)  {
        msecLst = msec;

        if (NminLeds > minLedIdx)  {
            digitalWrite (PinMinLeds [minLedIdx], On);
            Serial.println (minLedIdx);
            minLedIdx++;
        }
        else {
            minLedIdx = 1;
            for (int n = 1; n < NminLeds; n++)
                digitalWrite (PinMinLeds [n], Off);
            Serial.println ("reset");
        }
    }
}

void
setup (void)
{
    Serial.begin (9600);

    for (int n = 0; n < NminLeds; n++)  {
        pinMode      (PinMinLeds [n], OUTPUT);
        digitalWrite (PinMinLeds [n], Off);
    }
}

Thanks I made mistake of deleting the [ ] after coping and pasting

I am sorry I wasnt clear. The codes I posted I waish to modify so the LED comes on one by one not all toghether. If you could modify one of the existing codes, so i learn something

Thank you gcjr . What I wish to do is 5 mins each LED on one at a time
Your code is great but enum is too advanced for me at the moment.

if you are getting short of pins try using shift registers to control the led

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

this version is actually simpler

const byte PinMinLeds [] = {
    13, 12, 11,  10,  9,  8, 
     7,  6,  5,  A4, A3, A2
};
const int NminLeds = 4;  //sizeof(PinMinLeds);

int   idx = NminLeds-1;

enum { Off = HIGH, On = LOW };

const unsigned long FiveMins = 3000;    // 5 * 60 * 1000L;
unsigned long       msecLst;

// -----------------------------------------------------------------------------
void
loop (void)
{
    unsigned long msec = millis ();

    if (msec - msecLst >= FiveMins)  {
        msecLst = msec;

        digitalWrite (PinMinLeds [idx], Off);

        if (NminLeds <= ++idx)
            idx = 0;
        digitalWrite (PinMinLeds [idx], On);
        Serial.println (idx);
    }
}

void
setup (void)
{
    Serial.begin (9600);

    for (int n = 0; n < NminLeds; n++)  {
        pinMode      (PinMinLeds [n], OUTPUT);
        digitalWrite (PinMinLeds [n], Off);
    }
}

see bottom pg 38 in The C Programming Language

use of Off/On makes clear what is being done

isn't it weird that Off is HIGH?

it depends on the hardware. LED_BUILTIN on the arduino board is active HIGH. the LEDs on the multi-function shield that i'm using are active LOW. i work with model RR signals that are both common anode and common cathode.

Thanks gcjr, I tried that on my test setup. but the LEDs are blinking randomly.

have you changed the pin numbers?

do you understand how the code works?

Our world is gone mad. I was on the DroneBot workshop website, a very informative website, he tried code on ChatGPT and it worked.
I put the following into ChatGPT
“write code on arduino using Millis to flash each of 12 LEDs for 5 minutes one after another”. The result works except for the first LED not lighting up for 5 mins. with random blinks after the sequence.

/*
//works except first led not lighting until after interval
// from ChatGPT "write code on arduino using Millis to flash each of 12 LEDs for 5 minutes one after another
*/
const int numLEDs = 4;//have only 4 LEDs set up for testing
const int ledPins[] = {7, 6, 5, 4};  // Replace with your actual pin numbers

const unsigned long interval = 300000;  // 5 minutes in milliseconds
                                             //changed the 5 min. (5 * 60 * 1000) for testing
unsigned long previousMillis = 0;
int currentLED = 0;

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

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

  if (currentMillis - previousMillis >= interval) {
    // Reset the timer
    previousMillis = currentMillis;

    // Turn off the current LED
    digitalWrite(ledPins[currentLED], LOW);

    // Move to the next LED
    currentLED = (currentLED + 1) % numLEDs;

    // Turn on the next LED
    digitalWrite(ledPins[currentLED], HIGH);
  }
}

do want us to debug the ChatGPT code?

Thank you for looking at this. Please If you could debug that code, It works but the first LED doesnt light up for 5 mins. after that the sequence starts. At end of the sequence it hangs or stops on one LED.

You forgot to use the [sarcasm] tag when posting this :smiley:

Hi UKHelibob, Thanks for looking at this. Forget ChatGPT, Please look at the second code I posted above by paulpaulson. Copied is the part I dont understand and how could I modify it to light up the 12 LEDs for 5 mins one after another. Thanks

[code]
    static int nummer;
    blinkMillis = millis();
    Serial.println("blink");
    for (unsigned int n = 0; n < sizeof(leds); n++) digitalWrite(leds[n], ledSequ[nummer][n]);
    nummer++;
    nummer = nummer % (sizeof(ledSequ) / sizeof(leds));

[/code]

i don't see any post by paulpaul

did you look at the code in post #10 that changed the behavior of my previous code so that only one LED was on at a time. the first LED to come on is the one at 5 minutes

Try this

const byte ledPins[] = { 3, 5, 6, 9 };  //a list of LED pin numbers
const byte LED_COUNT = sizeof(ledPins) / sizeof(ledPins[0]);    //calculate number of LEDs
const int PERIOD = 1000;                   //LED period in milliseconds
byte currentLed = 0;

void setup()
{
    for (int p = 0; p < LED_COUNT; p++)     //set up pinMode()s for LEDs
    {
        pinMode(ledPins[p], OUTPUT);
        digitalWrite(ledPins[p], HIGH);          //start with LEDs off
        digitalWrite(ledPins[currentLed], LOW);  //turn on first LED
    }
}

void loop()
{
    Serial.println(currentLed);
    delay(PERIOD);
    digitalWrite(ledPins[currentLed], HIGH);  //turn off current LED
    currentLed += 1;                          //move to next LED
    if (currentLed >= LED_COUNT)              //wrap around to first LED
    {
        currentLed = 0;
    }
    digitalWrite(ledPins[currentLed], LOW);  //turn on next LED
}

NOTE
This was written for a system where LOW turns the LEDs off and HIGH turns them off. Adjust the values used by digitalWrite() to suit your system

Put your LED pin numbers in the ledPins array and change the PEROID variable after you have tested the sketch with a short period