-2

I want to make LED blink patterns with multiple LEDs with the millis() function. I researched about it but did not find a perfect solution.

I found one blog, but it is using a LED pin number array; I need a solution without an array. Any help will be appreciated.

This is my code which I have written:

const int Button_code_1 = 2;
const int Button_code_2 = 3;
const int Button_code_stop = 4;

const int LED_one = 6;
const int LED_two = 8;
const int LED_three = 10;
const int LED_four = 12;

int buttonState_code_1 = 0;
int buttonState_code_2 = 0;
int buttonState_code_stop = 0;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);

  pinMode(Button_code_1,INPUT);
  pinMode(Button_code_2,INPUT);
  pinMode(Button_code_stop,INPUT);

  pinMode(LED_Button_one,INPUT);
  pinMode(LED_Button_two,INPUT);
  pinMode(LED_Button_three,INPUT);
  pinMode(LED_Button_four,INPUT);
  
  pinMode(LED_one, OUTPUT);
  pinMode(LED_two, OUTPUT);
  pinMode(LED_three, OUTPUT);
  pinMode(LED_four, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

  buttonState_code_1 = digitalRead(Button_code_1);
  Serial.println(buttonState_code_1);
  buttonState_code_2 = digitalRead(Button_code_2);
  Serial.println(buttonState_code_2);
  delay(1000);
  buttonState_code_stop = digitalRead(Button_code_stop);
  
  if(buttonState_code_1==1)
  {
    pattern_one();
  }
  if(buttonState_code_2==1)
  {
    pattern_two();
  }

}

void pattern_one()
{
  digitalWrite(LED_one, LOW);
  digitalWrite(LED_two, HIGH);
  digitalWrite(LED_three, LOW);
  digitalWrite(LED_four, LOW);
  delay(3000);
  digitalWrite(LED_one, LOW);
  digitalWrite(LED_two, LOW);
  digitalWrite(LED_three, LOW);
  digitalWrite(LED_four, HIGH);
  delay(3000);
  digitalWrite(LED_one, HIGH);
  digitalWrite(LED_two, LOW);
  digitalWrite(LED_three, LOW);
  digitalWrite(LED_four, LOW);
  delay(3000);
  digitalWrite(LED_one, LOW);
  digitalWrite(LED_two, LOW);
  digitalWrite(LED_three, HIGH);
  digitalWrite(LED_four, LOW);
  delay(3000);
}

void pattern_two()
{
  digitalWrite(LED_one, HIGH);
  digitalWrite(LED_two, LOW);
  digitalWrite(LED_three, LOW);
  digitalWrite(LED_four, LOW);
  delay(3000);
  digitalWrite(LED_one, LOW);
  digitalWrite(LED_two, LOW);
  digitalWrite(LED_three, HIGH);
  digitalWrite(LED_four, LOW);
  delay(3000);
  digitalWrite(LED_one, LOW);
  digitalWrite(LED_two, HIGH);
  digitalWrite(LED_three, LOW);
  digitalWrite(LED_four, LOW);
  delay(3000);
  digitalWrite(LED_one, LOW);
  digitalWrite(LED_two, LOW);
  digitalWrite(LED_three, LOW);
  digitalWrite(LED_four, HIGH);
  delay(3000);
}

This is my sample code with four LEDs. I have written this code with delay(), so when I want to change the pattern I can't because delay() is blocking the code.

I researched this and found the millis() function as the solution. How can I implement this code using the millis() function?

5
  • 3
    1. Why without an array? Thats a weird requirement. Is it for a school exercise? 2. You didn't give us enough information to help you. What exactly is your goal and what is your problem in inplementing it
    – chrisl
    Jul 3, 2022 at 14:43
  • I have 9 LEDs and i need to blink one after another with delay of 3s and i have 2 different blink patterns and two buttons to select the pattern and one button to stop the program. so for using different patterns i want that it should not be array otherwise if i will get more patterns then i should take more array one for each pattern.Hope it clears the query
    – coder
    Jul 3, 2022 at 16:46
  • So your pattern are just which LED lights up next and the durations are always the same? Then what is so bad with one array for each pattern, or better one array of arrays (2 dimensional array)? Still seems to be an easy approach. With the 2 dimensional array you can write it in a way that adding a pattern is just adding another pattern to the array.
    – chrisl
    Jul 3, 2022 at 17:47
  • 1
    Please put all the information into the question by editing it. Also please describe in detail where exactly you are stuck. What is the actual problem? What exactly is it that you don't understand. Then we can write a good answer fitting your question
    – chrisl
    Jul 3, 2022 at 17:49
  • I have edited my question.
    – coder
    Jul 4, 2022 at 6:17

1 Answer 1

1

This is one of the effects for 3 (sets) of LEDs from my light alarm clock sketch:

void cycleEffect() {

  static unsigned long previousMillis;
  static uint8_t cycleCounter;

  if (millis() - previousMillis > 100) {
    previousMillis = millis();
    digitalWrite(LEDS_CENTRAL_PIN, cycleCounter == 0);
    digitalWrite(LEDS_LEFT_PIN, cycleCounter == 1);
    digitalWrite(LEDS_RIGHT_PIN, cycleCounter == 2);
    cycleCounter++;
    if (cycleCounter == 3) {
      cycleCounter = 0;
    }
  }
}
1
  • @Juray: you can replace the last three lines by a more common way to do this: cycleCounter = (cycleCounter + 1) % 3 (although I just realize this might be when performance is more important) Jul 3, 2022 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.