Go Down

### Topic: binary counter (Read 1 time)previous topic - next topic

#### jonscone

##### Mar 15, 2016, 12:32 am
I'm trying to write code to create a 5-bit binary counter. The counter should count up once per second and display the current count value (from 00000 to 11111) using the 5 LEDs I have. I've tried using the function bitRead(x,n) to work out what the value to each bit is to assign it to the LED's, but I honestly have no idea what I'm doing. Someone please help me!!!

#### larryd

#1
##### Mar 15, 2016, 12:55 am
Welcome.

Show us the sketch that you have at this point.
.
No technical PMs.
If you are asked a question, please respond with an answer.
If you need clarification, ask for help.

#### jonscone

#2
##### Mar 15, 2016, 01:04 am
This is what I have so far:
int led1 = 9;
int led2 = 8;
int led3 = 7;
int led4 = 6;
int led5 = 5;

void setup() {
// initialize the digital pin as an output.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
}

void loop() {
count=count+1;
delay(1000);
}

#### larryd

#3
##### Mar 15, 2016, 01:26 amLast Edit: Mar 15, 2016, 01:32 am by LarryD
Almost:

Code: [Select]
`const byte led1 = 9;const byte led2 = 8;const byte led3 = 7;const byte led4 = 6;const byte led5 = 5;byte count = 0;void setup() {                  // initialize the digital pin as an output.  pinMode(led1, OUTPUT);  pinMode(led2, OUTPUT);  pinMode(led3, OUTPUT);  pinMode(led4, OUTPUT);   pinMode(led5, OUTPUT);  }void loop() {     digitalWrite(led5, bitRead(count,4));  digitalWrite(led4, bitRead(count,3));  digitalWrite(led3, bitRead(count,2));  digitalWrite(led2, bitRead(count,1));   digitalWrite(led1, bitRead(count,0));  count=count+1;  delay(1000);}`

If your LEDs are reversed

Code: [Select]
`  digitalWrite(led5, !bitRead(count,4));  digitalWrite(led4, !bitRead(count,3));  digitalWrite(led3, !bitRead(count,2));  digitalWrite(led2, !bitRead(count,1));   digitalWrite(led1, !bitRead(count,0));`
No technical PMs.
If you are asked a question, please respond with an answer.
If you need clarification, ask for help.

#### larryd

#4
##### Mar 15, 2016, 01:27 am
How are you wiring your LEDs?
No technical PMs.
If you are asked a question, please respond with an answer.
If you need clarification, ask for help.

#### guix

#5
##### Mar 15, 2016, 03:04 pm
Hello and welcome,

You may find this post interesting

#### AndrewGhattas

#6
##### May 26, 2019, 06:28 am
I haven't found anyone who did it like this. It doesn't use modulo or binary masks, or any fancy library functions.

Basically, the idea is that any given bit will go from zero to one if all the bits that are less-significant than it were ones. So if you find "n" continuous ones beginning from the least significant bit, flip the "n+1" least significant bits.

Here's one way I did it:

Code: [Select]
`/*This uses the 10 LED displayIt's a binary counter written by Andrew Ghattas *//* sorry, I didn't bother checking if this boolean array was necessary or if I could use a pin output state in an "if" statement*/bool pinstates[] = {false,false,false,false,false,false,false,false,false,false};// you can arrange these the opposite way, depending on which way your circuit is "looking" at youint ledPins[] = {12,11,10,9,8,7,6,5,4,3};// the setup function runs once when you press reset or power the boardvoid setup() {  // initialize digital pins as outputs  for (int i = 0; i < 10;i++) {  pinMode(ledPins[i], OUTPUT); }}// the loop function runs over and over again forevervoid loop() {delay(1000);if (pinstates[8] && pinstates[7] && pinstates[6] && pinstates[5] && pinstates[4] && pinstates[3] && pinstates[2] && pinstates[1] && pinstates[0])    fliploop(9);else if (pinstates[7] && pinstates[6] && pinstates[5] && pinstates[4] && pinstates[3] && pinstates[2] && pinstates[1] && pinstates[0])  fliploop(8);  else if (pinstates[6] && pinstates[5] && pinstates[4] && pinstates[3] && pinstates[2] && pinstates[1] && pinstates[0])  fliploop(7);  else if (pinstates[5] && pinstates[4] && pinstates[3] && pinstates[2] && pinstates[1] && pinstates[0])  fliploop(6);  else if (pinstates[4] && pinstates[3] && pinstates[2] && pinstates[1] && pinstates[0])  fliploop(5);  else if (pinstates[3] && pinstates[2] && pinstates[1] && pinstates[0])  fliploop(4);  else if (pinstates[2] && pinstates[1] && pinstates[0])  fliploop(3);  else if (pinstates[1] && pinstates[0])  fliploop(2);else if (pinstates[0])  fliploop(1);   else  fliploop(0); }//starting from least significant bitsvoid fliploop(int numberofbitstoflip) {  for (int i = 0; i <= numberofbitstoflip; i++)    flipbit(i);}void flipbit(int i) {  pinstates[i] = ! pinstates[i];  if (pinstates[i])    digitalWrite(ledPins[i], HIGH);  else    digitalWrite(ledPins[i],LOW);}`

A more elegant way of writing it looks like this:

Code: [Select]
`/*This uses the 10 LED display to display a binary counter.Written by Andrew Ghattas. */int ledPins[10] = {12,11,10,9,8,7,6,5,4,3};const int numberofleds = 10;/* pins start at zerosorry, I didn't bother checking if this array was necessary or if I could use pin output states in an "if" statement*/int pinstates[10] = {0,0,0,0,0,0,0,0,0,0};// the setup function runs once when you press reset or power the boardvoid setup() {  // initialize digital pins as outputs.  for (int i = 0; i < numberofleds;i++)    pinMode(ledPins[i], OUTPUT);}// the loop function runs over and over again forevervoid loop() {  delay(100);  fliploop(leastsignificantones());  }int leastsignificantones() {  int i = 0;  while (pinstates[i] == 1 && i <= numberofleds - 2) {    i = i + 1;  }  return i ;}//starting from least significant bitsvoid fliploop(int numberofbitstoflip) {  for (int i = 0; i <= numberofbitstoflip; i++)    flipbit(i);}void flipbit(int i) {  if (pinstates[i] == 0) {    digitalWrite(ledPins[i], HIGH);    pinstates[i] = 1;   }  else {    digitalWrite(ledPins[i],LOW);    pinstates[i] = 0;  }}`

I hope you find this code insightful...  or at least entertaining!

#### Paul__B

#7
##### May 26, 2019, 07:49 am
I hope you find this code insightful...  or at least entertaining!
Well, someone might, but "jonscone" has not been to this forum for over three years, so I doubt he will!

Go Up