Binary numbers problem using multiple loops, for & delay

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?

Just use a regular int integer variable as a counter, yes you could use a for loop running from 0 to 31.

Then in the loop set each LED according to the value of the corresponding bit in that number, 5 LEDs, one for each binary bit in the number.

A delay once in the loop to allow to see the current pattern.

See if this

is enough to give you an idea.

There is no need to have five separate timed LEDs, although you have the timing exactly right it is much more complicated to do it that way even if it would be fun.

You could also place all five bits on the output pins using “direct port manipulation”, that’s oddly both harder and easier… google that, add Arduino to focus your search, perhaps it might make sense right away don’t worry if it doesn’t.

a7

1 Like

See BlinkWithoutDelay.

Lots of ways to do this...

Maybe something like (untested)...

int pins[6] = {3, 4, 5, 6, 7, 8};  


void setup()
{
  for (uint8_t x = 0; x < 6; x++)
  {
    pinMode(pins[x], OUTPUT);
  }
}


void loop ()
{
  for (uint8_t nbr = 0; nbr < 32; nbr++)
  {
    for (uint8_t x = 0; x < 6; x++)
    {
      digitalWrite(pins[x], bitRead(nbr, x));
    }

    delay(500);
  }
}

Hello dsaade
Welcome to the Arduino Forum.
See below a sketch for an other way like above mentioned. The sketch is tested with three LED´s and may be tailored to your needs.

/* BLOCK COMMENT
  ATTENTION: This Sketch contains elements of C++.
  https://www.learncpp.com/cpp-tutorial/
  Many thanks to LarryD
  https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
  https://forum.arduino.cc/t/binary-numbers-problem-using-multiple-loops-for-delay/962043
*/
#define ProjectName "Binary numbers problem using multiple loops, for & delay"
// HARDWARE AND TIMER SETTINGS
// YOU MAY NEED TO CHANGE THESE CONSTANTS TO YOUR HARDWARE AND NEEDS
constexpr byte LedPins[] {9, 10, 11};     // portPin o---|220|---|LED|---GND
// CONSTANT DEFINITION
enum {One, Two, Three};
// -------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);  // used as heartbeat indicator
  //  https://www.learncpp.com/cpp-tutorial/for-each-loops/
  for (auto Output_ : LedPins) pinMode(Output_, OUTPUT);
}
void loop () {
  // local used variable
  unsigned long currentTime = millis();
  static unsigned long stamp = 0;
  digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
  // BWOD
  if (currentTime - stamp >= 1000) {
    stamp = currentTime;
    Serial.println ("blink");
    // local used variable
    static unsigned int counter = 0;
    // https://www.learncpp.com/cpp-tutorial/comma-and-conditional-operators/
    digitalWrite(LedPins[One],  (counter & 0b00000001) ? HIGH : LOW);
    digitalWrite(LedPins[Two],  (counter & 0b00000010) ? HIGH : LOW);
    digitalWrite(LedPins[Three],(counter & 0b00000100) ? HIGH : LOW);
    counter++;
  }
}

Have a nice day and enjoy coding in C++.

My suggestions and some of the code may indeed be a bit more advanced. A bit, haha.

So I agree @DrDiettrich, look at “blink without delay”, you are juxt asking to blink 5 different LEDs at different frequencies, or with different periods, so that model will work well.

But we ask now what is the real problem you working on? Sometimes the question posed, when placed into a larger context, suggests a different answers,

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.