Code for Binary Counting for LEDs

Hey Guys,

For school I have to do a binary counter with 5 LEDs and I’ve tried to figure it out but i’m stuck.

We were told to use digital bitRead and the if and else functions in the code

Thanks for the help

Code below of what I have.

int count = 0;
int LED0=5;
int LED1=6;
int LED2=7;
int LED3=8;
int LED4=9;

void setup() {
pinMode(LED0,OUTPUT);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(LED4,OUTPUT);

}

void loop()
{
if (count<31);
if (count++);

else
count = 0;

digitalWrite(LED0,!bitRead(count,0));
digitalWrite(LED1,!bitRead(count,1));
digitalWrite(LED2,!bitRead(count,2));
digitalWrite(LED3,!bitRead(count,3));
digitalWrite(LED4,!bitRead(count,4));

}

your problem is here:
///////////////////////////////////
if (count<31);
if (count++);

else
count = 0;
///////////////////////////////////

change it to:
///////////////////////////////////
if (count<31){
count++;
}
else{
count = 0;
}
///////////////////////////////////

Hope that helps.

I’ve just replaced the code and it has turned all the LEDs on

D4nski:
I've just replaced the code and it has turned all the LEDs on

makes sense since they are turning ON/OFF super fast! to slow it down try adding "delay(1000);" at the end of loop(), that would introduce a 1s delay between each count iteration.

curiousity on my part but, in your digitalWrite's what happens if you remove the "!" in front of your bitRead's?

Thanks, the delay solved the problem, and getting rid off the "!" in the code makes it go in reverse by the looks of it.

Thanks for all the help

And if you now use an array you reduce the amount of code by 50% :wink: