I'm working on a code that will make LEDs light up depending on a binary count (ex. I have 5 LEDS, when number 10 comes up the LED n1 (value of 1) will be LOW, n2 (value of 2) will be HIGH, n3 (value of 4) will be LOW, n4 (value of 8) will be HIGH and n5 (value of 16) will be LOW).
I can go the easy way and do a 10 line code for each of the 32 outcomes, but I know there's a smarter way to solve this problem.
I'm thinking that I can use for and delay on a way that you only need to state for how long the LED must be on/off and automatically sync with the other LEDs
Let's say that every number form 0 to 31 lasts 500 milliseconds.
- The LED of value 1 should go off and on every second. (1 round of LOW, 1 round of LOW).
- LED of value 2 should go off and on every 2 second (2 round of LOW, 2 round of LOW)
- LED of value 4 should go off and on every 4 second (4 round of LOW, 4 round of LOW)... and that goes until the fifth LED
So, the patter for all the LEDS to be sync would be:
void loop()
{
digitalWrite(LED_1,LOW);
delay(500);
digitalWrite(LED_1,HIGH);
delay(500);
digitalWrite(LED_2,LOW);
delay(1000);
digitalWrite(LED_2,HIGH);
delay(1000);
digitalWrite(LED_4,LOW);
delay(2000);
digitalWrite(LED_4,HIGH);
delay(2000);
digitalWrite(LED_8,LOW);
delay(4000);
digitalWrite(LED_8,HIGH);
delay(4000);
digitalWrite(LED_16,LOW);
delay(8000);
digitalWrite(LED_16,HIGH);
delay(8000);
}
EACH OF THEM SHOULD START AND END AT THE SAME TIME TO WORK. But I have little to no clue on how to achieve this.
I'm thinking about something along the lines of:
for (x amount of milliseconds, thinking about a delay command){
(switch LOW to HIGH and then back
}
I'm new to Arduino and I'm still learning, any ideas on how to tackle the problem?