Some LED Trouble

You are very welcome, that is why we are here. And thank you for being very cooperative in your responses, surprisingly not all people asking questions here actually want to learn stuff.

@soccerbot-2-0 here's hwere I ended up with your sketch.

Since it was a mashup of two, it did not read very well.

In particular, the similarity of the names for the LEDs led some to think you only had one set of three, and were needlessly talking about them too much. And there was some dead code, and some repetition.

Since it is basically two independent processes, and both are wonderfully non-blocking, I made functions out of them so we have a nice simple loop, viz:

unsigned long now;    // the time for the entire loop iteration

void loop() {
  
  now = millis();

  blinkRGB();
  onOffThreeOthers();
}

which readers can see is two things going on at once. Provided there is no use of delay() or any blocking loops. Since Arduino uses the setup/loop structure, typically readers will skip to read the loop() function - and either be more or less rapidly up to speed on what you up to, or dismayed to find a few hundred lines of code. I usually search for delay() and millis() in any sketch I've never seen - it gives a very fast picture of what is going on and how the writer is attempting to accomplish her goals.

Now here there is only one call to millis(). Since the loop runs fast it is possible to have one value for the current time. It was currentMillis but that strikes me as a bit pretentious. I've called it now since forever, so I put that small personal stamp on it. now is set at the top of the loop() function, and everyone everywhere else uses that value.

I gave the blinkers some arbitrary rates. Anything works better than zero! You can tinker with them easily; I chose unrelated numbers but if you use constants that are half or double you can get some interesting effects.

Try this to see what I mean and get ideas of your own:

const long BLINK_INTERVAL_1 = 500;  
const long BLINK_INTERVAL_2 = 1000;
const long BLINK_INTERVAL_3 = 500;

I've had the sketch under the microscope in the wokwi simulator for some time. There is an RGB LED part available in the simulator, so I just wired that in parallel with the three regular LEDs I had been using. Note that the simulator will tolerate LEDs in parallel, and LEDs without series current limiting resistors won't burn out. Real life is no so forgiving. If I was not so lazy, I'd put the nine required resistors into the diagram.

The code is here
// https://forum.arduino.cc/t/some-led-trouble/1228574
// https://wokwi.com/projects/390975398041823233

const int RGB_PIN_1 = 3;            
const int RGB_PIN_2 = 6;
const int RGB_PIN_3 = 10;

const long BLINK_INTERVAL_1 = 500;  
const long BLINK_INTERVAL_2 = 700;
const long BLINK_INTERVAL_3 = 900;

// Variables will change:
int ledState_1 = LOW;   // ledState used to set the LED 1
int ledState_2 = LOW;
int ledState_3 = LOW;

unsigned long previousMillis_1;   // will store last time LED 1 was updated
unsigned long previousMillis_2;
unsigned long previousMillis_3;

// three other LEDs
#define LED_1_PIN 5
#define LED_2_PIN 2
#define LED_3_PIN 4

#define BUTTON_PIN 7

#define LED_NUMBER 3

byte LEDPinArray[LED_NUMBER] = { LED_1_PIN,
                                LED_2_PIN,
                                LED_3_PIN};

unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;

byte lastTimeButtonState = HIGH;

byte LEDState = LOW; 

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

// setup for RGB blinkation
  pinMode(RGB_PIN_1, OUTPUT);
  pinMode(RGB_PIN_2, OUTPUT);
  pinMode(RGB_PIN_3, OUTPUT);

// setup for on/off three others
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  for (int i = 0; i < LED_NUMBER; i++) {
    pinMode(LEDPinArray[i], OUTPUT);
 }
}

unsigned long now;    // the time for the entire loop iteration

void loop() {
  
  now = millis();

  blinkRGB();
  onOffThreeOthers();
}

void blinkRGB()
{
  // check to see if it's time to blink the LED 1
  if (now - previousMillis_1 >= BLINK_INTERVAL_1) {
    // if the LED is off turn it on and vice-versa:
    ledState_1 = (ledState_1 == LOW) ? HIGH : LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(RGB_PIN_1, ledState_1);

    // save the last time you blinked the LED
    previousMillis_1 = now;
  }

  // check to see if it's time to blink the LED 2
  if (now - previousMillis_2 >= BLINK_INTERVAL_2) {
    // if the LED is off turn it on and vice-versa:
    ledState_2 = (ledState_2 == LOW) ? HIGH : LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(RGB_PIN_2, ledState_2);

    // save the last time you blinked the LED
    previousMillis_2 = now;
  }
  
  // check to see if its time to blink the LED 3
  if (now - previousMillis_3 >= BLINK_INTERVAL_3) {
    // if the LED is off turn it on and vice-versa:
    ledState_3 = (ledState_3 == LOW) ? HIGH : LOW;
    
    // set the LED with the ledState of the variable:
    digitalWrite(RGB_PIN_3, ledState_3);
    
    // save the last time you blinked the LED
    previousMillis_3 = now;
  }
}

void onOffThreeOthers()
{
// we already have a loop time variable   unsigned long timeNow = millis();
  if (now - lastTimeButtonStateChanged > debounceDuration) {
    byte buttonState = digitalRead(BUTTON_PIN);
    if (buttonState != lastTimeButtonState) {
      lastTimeButtonStateChanged = now;
      lastTimeButtonState = buttonState;
      if (buttonState == HIGH) { // button has been released
        LEDState = (LEDState == LOW) ? HIGH : LOW;
        powerOnAllLEDs(LEDState == HIGH);
      }
    }
  }
}

void powerOnAllLEDs (bool powerOn)
{
  for (int i = 0; i < LED_NUMBER; i++) {
    if (powerOn) {
      digitalWrite (LEDPinArray[i], HIGH);
    }
    else {
      digitalWrite (LEDPinArray[i], LOW);
    }
  }
}

And you can see it run in the simulator at this link

Wokwi_badge UA RGB on/off


The wokwi is entirely a life changer. It makes testing and experimenting easy and best part is no broken components and nothing to buy.

I'm not going to any more fun with this, but I am compelled to point out this code plucked out of the middle of blinkRGB():

  // check to see if it's time to blink the LED 2
  if (now - previousMillis_2 >= BLINK_INTERVAL_2) {
    // if the LED is off turn it on and vice-versa:
    ledState_2 = (ledState_2 == LOW) ? HIGH : LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(RGB_PIN_2, ledState_2);

    // save the last time you blinked the LED
    previousMillis_2 = now;
  }

We say here any time you create more code by copy/paste/editing existing code, you should think about making a function that handles a general case, then use the function for all instances of the real hardware.

A corollary to this idea is that any time you find yourself creating new variables by appending a differentiating digit or character to the name of one that already exists, you should think about using array variables.

And you will see if you do decide to look into functions and array variables how nicely they work with loops...

That code for LED 2 is just the code for LED 1 with some editing, same same LED 3. Those three could be instead calls to one function that handles LED number N, where you tell the function which N 0, 1 or 2 it is to perform the logic on.

This is good in may ways. Shorter code is easier to read. And if there are mistakes, only one place to edit for a fix. Same with modifications and enhanclements. One function, one place to worry about any of that.

HTH

a7

Would it be possible to code the RGB LED to change colors in a sequence?

For example, you have 3 LEDs in a row, they start off as green, then from right to left go to red.

Yes, probably anything you can imagine.

Right now I am confused, as you seemed to have 6 LEDs in the code, but if you are using both the red and green parts of three RGB LEDs as well as three regular LEDs you need nine output pins.

I had decided you had one RGB LED.

Anyway, there are many ways to accomplish your goal. If you look at the blank page and don't know where to start, it just means you have to start reading code everywhere you find it performing anything like you want your own code to do.

And if the sketch in form of you right here makes no sense, it just means you are near where we a,, started, knowing nothing about nothing. In which case working through an organized course of instruction or even just starting with the examples on offer in the IDE.

I just googled

  red green led chaser arduino diy

I suggest you make up some searches - that one led to some plausible looking projects. If you find a few and built them in the wokwi simukator and… study the code to inform your learning, you'll be able to make stuff up and code it yourself.

Just don't expect it to be next week. Some of us will admit to still learning after never mind how long at this stuff.

a7