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?
Thanks in advance!
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
}