Help for change blink code plz

Hi! I'm a beginner in arduino and I'm having trouble with this code for make high and low 12 output one after one with millis. I have this code from @paulpaulson in this topic

this code (step 1 = light first 2 led, step 2 = light 2nd two, turn off first 2, etc.)
this code is good enough for me, but this code have just on interval time, I need 2 interval time in this code, one of them for onTime interval and one for offTime interval.

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)); 
  }
}

can any body help me for edit this code for 12 outputs and 2 interval time, one interval for on time and one interval for off time.

So that seems quite advanced code for a beginner.

Sure, here we help, we generally do not actually do it for you.

First we need to know what sort of Arduino you have so we know what other pins are available. Also what value of series resistor are you using on the LEDs.

  1. Remove the const from the blink variable.
  2. Then after the last line of code but before the closing brace, add an if statement which compares the value of the variable blink with its initial value, in your case 1000. If it is equal to this make the variable blink have a new number like 400. This defines the off time. if it is not equal to 1000, using the else clause in the if statement make the blink variable equal to 1000 again, this is the on time.

This sounds like school assignment, when is it due.

assume @paulpaulson can help

High level how to:

  1. add pin definitions
  2. add pin direction statements to setup
  3. determine how to modify that value blink, or in some other way change the timing
  4. add code to achieve (3)
    Done!
    As others have said, we will help you understand what you're doing, fix what your doing, improve what your doing, but you're doing, not us.
    Have fun!

I'm using arduino UNO, and 220ohm resistor.

By the way, better ask prof for a clarification - since this code, when modified, will always have two LEDs ON, what are we applying the onTime and offTime time intervals to. Or is it that two LEDS will be on for onTime, then all will be off for offTime, then a different two will be on... etc. Your problem statement isn't clear at all, to me.

I just want 2 interval for on and off every 2 leds. in this code I just have one interval time that every 2 led blink to end pin. I want 2 different time for on and off leds.

Hello mma1919

Consider this led sequencer and modify the sketch to your project.

enum LedState {o, i};

constexpr uint8_t LedPins[] {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

struct LEDSEQUENCER
{
  const uint32_t IntervalMillis;
  uint8_t pattern[sizeof(LedPins) / sizeof(LedPins[0])];
  uint32_t previousMillis;
  uint8_t timerControl;
};
LEDSEQUENCER ledSequencers[]
{
  { 1000, i, i, o, o, o, o, o, o, o, o, o, o, 0, HIGH},
  { 1000, o, i, i, o, o, o, o, o, o, o, o, o, 0, LOW},
  { 1000, o, o, i, i, o, o, o, o, o, o, o, o, 0, LOW},
  { 1000, o, o, o, i, i, o, o, o, o, o, o, o, 0, LOW},
  { 1000, o, o, o, o, i, i, o, o, o, o, o, o, 0, LOW},
  { 1000, o, o, o, o, o, i, i, o, o, o, o, o, 0, LOW},
  { 1000, o, o, o, o, o, o, i, i, o, o, o, o, 0, LOW},
  { 1000, o, o, o, o, o, o, o, i, i, o, o, o, 0, LOW},
  { 1000, o, o, o, o, o, o, o, o, i, i, o, o, 0, LOW},
  { 1000, o, o, o, o, o, o, o, o, o, i, i, o, 0, LOW},
  { 1000, o, o, o, o, o, o, o, o, o, o, i, i, 0, LOW},
  // add pattern if needed
};
void setup()
{
  for (auto LedPin : LedPins)
  {
    pinMode ( LedPin, OUTPUT);
    digitalWrite(LedPin, HIGH);
    delay(1000);
    digitalWrite(LedPin, LOW);
    delay(1000);
  }
}
void loop()
{
  uint32_t currentMillis = millis();
  for (auto &ledSequencer : ledSequencers)
  {
    if (currentMillis - ledSequencer.previousMillis >= ledSequencer.IntervalMillis && ledSequencer.timerControl)
    {
      static uint8_t number = 0;
      uint8_t element = 0;
      ledSequencer.timerControl = LOW;
      number = (number + 1) % (sizeof(ledSequencers) / sizeof(ledSequencers[0]));
      ledSequencers[number].timerControl = HIGH;
      ledSequencers[number].previousMillis = currentMillis;
      for (auto LedPin : LedPins) digitalWrite(LedPin, ledSequencers[number].pattern[element++]);
    }
  }
}

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

@paulpaulson i see a unique timer for each sequence, not separate times for on and off

Each sequence can be configured by timing and led state.

Play around and figure out.

consider
each sequence in ON for MsecOn and OFF for MsecOff

   byte leds []  =  { 9, 8, 7,6,  5, 4, 3, 2 };
// byte leds []  =  { 13, 12, 11, 10,  5, 4, 3, 2 };
// byte leds []  =  { 13, 12, 11, 10 };
const unsigned Nleds = sizeof(leds);

enum { Off = HIGH, On = LOW };

#define ___ Off

byte ledSequ [][8] { 
    {  On, ___, ___, ___, ___, ___, ___, ___},
    { ___,  On, ___, ___, ___, ___, ___, ___},
    { ___, ___,  On, ___, ___, ___, ___, ___},
    { ___, ___, ___,  On, ___, ___, ___, ___},
    { ___, ___,  On, ___, ___,  On, ___, ___},
    { ___,  On, ___, ___, ___,  On,  On, ___},
};
const unsigned Nseq = sizeof(ledSequ) / 8;
byte     onOff [Nleds];
unsigned ledSeqIdx;

const unsigned long MsecOn  = 1000;
const unsigned long MsecOff =  200;
      unsigned long msecPeriod;
      unsigned long msecLst;

// -----------------------------------------------------------------------------
void loop()
{
    unsigned long msec = millis ();
    if (msec - msecLst >= msecPeriod ) {
        msecLst = msec;

        Serial.print (ledSeqIdx);

        if (On == onOff [ledSeqIdx]) {
            Serial.println ("   timer On:");
            for (unsigned n=0; n < Nleds; n++)
                digitalWrite (leds[n], Off);

            onOff [ledSeqIdx] = Off;
            msecPeriod = MsecOff;

            if (Nseq <= ++ledSeqIdx)
                ledSeqIdx = 0;
        }
        else  {
            Serial.println ("   timer Off:");

            for (unsigned n=0; n < Nleds; n++)
                digitalWrite (leds[n], ledSequ [ledSeqIdx][n]);

            onOff [ledSeqIdx] = On;
            msecPeriod = MsecOn;
        }
    }
}

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

    for (unsigned n = 0; n < Nleds; n++)  {
        pinMode (leds [n], OUTPUT);
        digitalWrite (leds[n], On);
    }
}

Thanks to @gcjr and @paulpaulson for helping me.

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