# Program for converting decimals to binary with led output of the binary values

Hi,

I’m currently trying to code an exercise for school in which we have to convert all the numbers in the interval 0-15 to binary and display the binary value using 4 led’s (first led: 8, 2nd led: 4, …).

Now, I’m kinda stuck because it doesn’t work. I’m sure that the inner loops work correctly because I have tested these seperately. But when I add the outer loop for going through the numbers in 0-15, nothing works.

Can anybody find my mistake?

``````int ledpins [] = {2, 3, 4, 5};    //pins that have a led connected

void setup() {
for (int i = 0; i < 4; i++) {
pinMode(ledpins[i], OUTPUT);
}
}

void loop() {
for (int number = 0; number < 16; number++) {    //for number from 0-15
int var = number;
delay(2000);

for (int i = 0; i < 4; i++) {         //turn all leds off
digitalWrite(ledpins[i], LOW);
}

int j = 0;
for (int i = 8; i >= 1; i -= (i / 2)) {   //so i = 8, i = 4, i = 2, i = 1
if (var >= i) {
digitalWrite(ledpins[j], HIGH);
var -= i;
}
j++;
}
}
for(;;){}   //everything is only executed once

}
``````

Just test the individual bits of the variable - the number already is binary, there is no need for a conversion.
Just use shift and bitwise AND operators.

TheMemberFormerlyKnownAsAWOL:
Just test the individual bits of the variable - the number already is binary, there is no need for a conversion.

Yes, I know that it is already binary. But I'm learning to code and the exercise is made for us to learn and use for-loops, variables,...etc

Ok, I found my mistake. One of the inner loops wasn't correct.
In the 2nd one, it would just go on forever as "i" would never be smaller than 1.

`for(;;){}  //everything is only executed once`ah yeah just like `while(1);`

``````#define EVER (;;)

for EVER;
``````

TheMemberFormerlyKnownAsAWOL:

``````#define EVER (;;)
``````

for EVER;

LOL !