Turn LEDs on in binary sequence

Could I have some help getting the following code work?

My purpose is to turn on 4(or N) LEDs in a binary sequence with 4 (or N) bits, A,B,C,D. When bit=1, LED on, otherwise LED off,...

I will have 16 combinations, 2^4=16, so:

• 0000 => All leds off
• 0001 => All leds off except the one on the right
• ...

Here's the code I came up with:

``````int LEDblu=4;
int LEDgiallo=2;
int LEDrosso=5;
int LEDverde=7;
int num_led;
String numeri="";
String output;
int i=0;
int p;

void setup() {
// put your setup code here, to run once:
pinMode(LEDblu,OUTPUT);
pinMode(LEDgiallo,OUTPUT);
pinMode(LEDrosso,OUTPUT);
pinMode(LEDverde,OUTPUT);

Serial.begin(9600);

if(Serial.available())                //Dimmi numero LED
{
Serial.print(num_led);
}
}

void loop() {
// put your main code here, to run repeatedly:
int vect[num_led];
for(int i=num_led-1, c=0; i>=0; i--, c++)
{
int num=2^i;
vect[c]=num;
}

output="";

if(i<2^num_led)
{
p=i;

for(int c=0; c<num_led; c++)     //Convert binary to decimal
{
if(p>=vect[c])
{
output+="1";
int d=vect[c];
p-=d;
}
else
output+="0";
}

Serial.println(output);

if(output[0]=="1")
digitalWrite(LEDrosso,HIGH);
else
digitalWrite(LEDrosso,LOW);

if(output[1]=="1")
digitalWrite(LEDverde,HIGH);
else
digitalWrite(LEDverde,LOW);

if(output[2]=="1")
digitalWrite(LEDgiallo,HIGH);
else
digitalWrite(LEDgiallo,LOW);

if(output[3]=="1")
digitalWrite(LEDblu,HIGH);
else
digitalWrite(LEDblu,LOW);
}

delay(1000);
i++;
}
``````
• ^ is XOR. I think you want << bitshifting Commented May 14, 2017 at 18:11
• @Majenko I guess he thinks `^` is for power. For powers use pow (e.g. `pow(2, num_led)`) Commented May 14, 2017 at 18:24
• For powers of 2 use bitshifting. pow() is very heavyweight. Commented May 14, 2017 at 18:25

I don't see any specific initialization of `num_led`; it should be set to some appropriate value, like 4.

As noted in some comments, the `^` operator is bitwise exclusive-Or. For example, 2^2 is 0, 2^4 is 6, 2^7 is 5, etc.

To compute the nth power of 2, say `1<<n`.

Your convert-binary-to-decimal code is clunky and unnecessary. Instead, do something like the following, which repeatedly strips off the least significant bit of `t`:

``````t = p;
digitalWrite(LEDrosso,t&1);   t>>=1;
digitalWrite(LEDverde,t&1);   t>>=1;
digitalWrite(LEDgiallo,t&1);  t>>=1;
digitalWrite(LEDblu,t&1);
``````

If you want the blue LED instead of the red one to represent the least significant bit, reverse the order of color names in the `digitalWrite` statements from rosso, verde, giallo, blu to blu, giallo, verde, rosso.

To print data out in binary, use the `Streaming.h` library, and say, for example,

``````Serial << _BIN(p) << endl;
``````
• `Serial.println(p, BIN);` should be working too. But Streaming library is usually more usefull.
– KIIV
Commented May 14, 2017 at 22:49
• @KIIV, thanks for that note. I didn't remember the syntax for `Serial.println(p, BIN)` and was short of time for looking it up. Commented May 15, 2017 at 2:59

Note that this is one of those situations where direct port access just makes things easier. You did not specify what board you are using, and I will assume you have some AVR-based board. On those boards, I/O pins come in groups called ports, with up to eight pins per port. You can update all the pins of a port at once by writing a number to the port output register. And guess what? The number you write comes out the pins in binary, jut like everything works in binary inside the microcontroller.

For example, on an Arduino Uno, you could wire your LEDs to the first four pins of port B: i.e. pins 8 (lest significant bit) through 11 (most significant bit). Then you can output a number to the LEDs just by writing `PORTB = number`, and your loop would become:

``````void loop()
{
static uint8_t number;
PORTB = number;              // output to pins 8..11
Serial.println(number, 2);   // print the value in binary
number = (number + 1) % 16;  // increment modulo 16
delay(1000);
}
``````