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