D4nski
March 14, 2018, 5:53am
1
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.
D4nski
March 14, 2018, 6:16am
3
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?
D4nski
March 14, 2018, 7:03am
5
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%