delay() - millis()

Hello,

I’m a beginner in “Arduino world” and I need some help.

I’m trying to create a code in which there are 8 LEDS :

  • the LED1 and the LED2 have to blink without delay (LED1 - 50 milliseconds and LED2 - 100 milliseconds) ;
  • the LED3 have to stay light ;
  • the 5 others LEDS have to blink one by one (like the K2000’s eye in the TV show) but without create a delay in the blink of LED1 and LED2.

To summarize, my problem is to find a way to combine these two codes :

Code 1 - LED1 / LED2 / LED3

const int ledAvantBlanche = 2;
const int ledAvantJaune = 3;
const int ledArriere = 4;

const unsigned long intervalLedAvantBlanche = 50;
const unsigned long intervalLedAvantJaune = 100;

void setup()
{
 for (int led = 2; led < 5; led ++)
 {
   pinMode(led, OUTPUT);
   digitalWrite(led, LOW);
 }
 digitalWrite(ledArriere, HIGH);
}

void loop()
{
 allumageLedAvantBlanche();
 allumageLedAvantJaune();
}

void allumageLedAvantBlanche() 
{
 static unsigned long previousMillisLedAvantBlanche = 0;
 static byte etatLedAvantBlanche = LOW;

 unsigned long currentMillis = millis();

 if (currentMillis - previousMillisLedAvantBlanche >= intervalLedAvantBlanche)
 {
   previousMillisLedAvantBlanche = currentMillis;
   etatLedAvantBlanche = !etatLedAvantBlanche;
   digitalWrite(ledAvantBlanche, etatLedAvantBlanche);
 }
}

void allumageLedAvantJaune()
{
 static unsigned long previousMillisLedAvantJaune = 0;
 static byte etatLedAvantJaune = LOW;

 unsigned long currentMillis = millis();

 if (currentMillis - previousMillisLedAvantJaune >= intervalLedAvantJaune)
 {
   previousMillisLedAvantJaune = currentMillis;
   etatLedAvantJaune = !etatLedAvantJaune;
   digitalWrite(ledAvantJaune, etatLedAvantJaune);
 }
}

Code 2 - the 5 others LEDS

int neon[5] = {8, 9, 10, 11, 12};

void setup() 
{
 for (int led = 8; led < 13; led ++)
 {
   pinMode(led, OUTPUT);
   digitalWrite(led, LOW);
 }
}

void loop() 
{
 for (int ledBleueSens1 = 0; ledBleueSens1 < 4; ledBleueSens1 ++)
 {
   digitalWrite(neon[ledBleueSens1], HIGH);
   delay(50);
   digitalWrite(neon[ledBleueSens1], LOW);
   delay(50);
 }

 for (int ledBleueSens2 = 4; ledBleueSens2 > 0; ledBleueSens2 --)
 {
   digitalWrite(neon[ledBleueSens2], HIGH);
   delay(50);
   digitalWrite(neon[ledBleueSens2], LOW);
   delay(50);
 }
}

I hope to have been clear in my question.

Thank you in advance for your help.

Somewhere, there are instructions for combining code. Sorry, I don't do this and don't know where the instructions are.

Look for the "Blink Without Delay" example if you want to learn how to blink multiple LEDs at various rates.

Please read this:-
How to use this forum it will tell you how to post code and how to ask a question.

You can't use delay in a blink without delay without spoiling a blink without delay code.

All the code must be written as a state machine not just part of it.

See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once

My take on merging code is here Merging Code but it won't help you much as your problem is using delay in your code.

OK, first things first. If we are going to discuss the code, it must be presented in a usable fashion. Until we have that, there is little point discussing the details.

You need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the “More → Modify” option below the right hand corner of your post - to mark up your code as such using the “</>” icon in the posting window. Just highlight each section of code (or output) from the IDE and click the icon. In fact, the IDE has a “copy for forum” link to put these markings on a highlighted block for you so you then just paste it here in a posting window.

It is inappropriate to attach it as a “.ino” file. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And that would also assume they are using a PC and have the IDE running on that PC.

But don’t forget to use the “Auto-Format” (Ctrl-T) option first to make it easy to read. If you do not post it as “code”, it can be quite garbled and is always more difficult to read.

Also tidy up your blank space. Use blank lines only between functional blocks.

some time ago I did first trials to go the "BlinkWithoutDelay" example the object oriented way.
And so I tried to add a bouncing effect with 5 LEDs also.

Imho it's so much easier to add more and more light effects if you go OOP without the usage of delay:

create a new object, call a method in setup & call a method in the loop. The more LEDs you add, the more advantage you will get from OOP.

/* Blink a LED in a specific rhythm

  Turns on and off light emitting diodes (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.

  Version 2019-02-12: added class for BouncingLed http://forum.arduino.cc/index.php?topic=597083.0
  Version 2018-11-24: changed .setup method to .begin for Arduino 1.8.7

  provided by noiasca

*/

class BouncingLed {                    // five bouncing LEDs like KITT
    byte state = 2;                    // 0 off, 1 run
    unsigned long previousMillis;      // last blink timestamp
    uint16_t on = 50;                  // milliseconds on time for the first LED = off time for the second LED
    uint16_t off = 50;                 // milliseconds off time for the first LED = on time for the secon LED
    const byte ledPin[5];              // the five led pins
    byte current = 0;                  // current position in pattern

  public:
    BouncingLed(byte _ledPin1, byte _ledPin2, byte _ledPin3, byte _ledPin4, byte _ledPin5):
      ledPin{_ledPin1, _ledPin2, _ledPin3, _ledPin4, _ledPin5}
    {}

    void begin() {
      for (auto &currentPin : ledPin)
      {
        pinMode(currentPin, OUTPUT);
      }
    }

    void set(uint16_t _on, uint16_t _off) { // modify on/off times during runtime
      on = _on;
      off = _off;
    }

    void setState(byte _state) {           // 1 start bouncing; 0 switch off bouncing
      state = _state;
    }

    void tick() {
      if (state > 0) {
        const uint8_t ledPattern[] = {0, 1, 2, 3, 4, 3, 2, 1};  // the order how to light the LEDs
        const byte totalPatterns = sizeof(ledPattern) / sizeof(ledPattern[0]);
        uint32_t currentMillis = millis();
        byte actualLed = ledPattern[current];

        if (state == 1 && currentMillis - previousMillis >= on) {
          // time to switch off
          Serial.print(F("D62 on  state=")); Serial.print(current); Serial.print(F(" actual=")); Serial.println(actualLed);
          previousMillis = currentMillis;
          for (auto &currentPin : ledPin)
          {
            digitalWrite(currentPin, LOW);
          }
          state = 2;
        }
        else if (state == 2 && currentMillis - previousMillis >= off) {
          // time to switch on next LED
          Serial.print(F("D70 off state=")); Serial.print(current); Serial.print(F(" actual=")); Serial.println(actualLed);
          previousMillis = currentMillis;
          digitalWrite(ledPin[actualLed], HIGH);
          current++;
          if (current >= totalPatterns) current = 0;
          state = 1;
        }
      }
      else // switch off all LEDs
      {
        for (auto &currentPin : ledPin)
        {
          digitalWrite(currentPin, LOW);
        }
      }
    }
};


class BlinkLed {
    byte state = 1;                    // 0 off, 1 blink
    unsigned long previousMillis;      // last blink timestamp
    uint16_t on = 180;
    uint16_t off = 320;                // 180/320 is according ECE
    const byte ledPin;                 // a GPIO for the LED

  public:
    BlinkLed(byte attachTo, uint16_t _on = 180, uint16_t _off = 320):
      ledPin(attachTo),
      on (_on),
      off (_off)
    {}
    void begin() {
      pinMode(ledPin, OUTPUT);
    }
    void set(uint16_t _on, uint16_t _off) { // modify on/off times during runtime
      on = _on;
      off = _off;
    }
    void setState(byte _state) {           // 1 Switch on blinking; 0 switch off blinking
      state = _state;
    }
    void tick() {
      if (state) {
        uint32_t currentMillis = millis();
        if (digitalRead(ledPin) && currentMillis - previousMillis >= on) {
          // save the last time you blinked the LED
          previousMillis = currentMillis;
          digitalWrite(ledPin, LOW);
        }
        else if (!digitalRead(ledPin) && currentMillis - previousMillis >= off) {
          // save the last time you blinked the LED
          previousMillis = currentMillis;
          digitalWrite(ledPin, HIGH);
        }
      }
    }
};


// declare your LED objects (and assign GPIOs for your LEDs)

BouncingLed myBouncingLedGroup(2, 3, 4, 5, 6);     // a LED group with bouncing effect
BlinkLed anotherBlinkLed(7);                       // a simple blink LED on a GPIO with 500ms on, 500ms off
BlinkLed myBlinkLed(13);                           // a simple blink LED on a GPIO with 500ms on, 500ms off

void setup() {
  // each LED object has to be called once in the setup
  myBouncingLedGroup.begin();
  anotherBlinkLed.begin();
  myBlinkLed.begin();

  // modify some default values
  anotherBlinkLed.set(50, 50); // on/off time
  myBlinkLed.set(100, 100); // on/off time
  
  Serial.begin(115200);    //only used for debug
  Serial.println(F("BlinkWithoutDelayRhythm2"));
  Serial.print  (__DATE__);
  Serial.print  (F(" "));
  Serial.println(__TIME__);
}

void loop() {
  // each LED object has to be called once in the loop
  myBouncingLedGroup.tick();
  anotherBlinkLed.tick();
  myBlinkLed.tick();
 
  // put here other code which needs to run:

}