# Led binary counter for xmas ! Help me!

Hello, world... I'm wondering if you can help me founding an error that i couldnt found...

I writed this code for 6 LED, this code tries to be a display binary counter which turns 6 leds counting from zero like this:
zero 000000
one 000001
two 000010
three 000011
four 000100
...
like that to decimal number equivalent of 11111 that is 63 according to programmer windows 8 calculator

First, I write the code equals to the code down excepting i declared the variables using:

``````int A1=8;
int B1=7;
int C1=6;
int D1=5;
int E1=4;
int F1=3;
int intervalo=500;
``````

But, its the same thing...

``````const int A1=8;
const int B1=7;
const int C1=6;
const int D1=5;
const int E1=4;
const int F1=3;
const int intervalo=500;

void setup(){
pinMode(A1,OUTPUT);
pinMode(B1,OUTPUT);
pinMode(C1,OUTPUT);
pinMode(D1,OUTPUT);
pinMode(E1,OUTPUT);
pinMode(F1,OUTPUT);
}

void limpiar(){
digitalWrite(A1,LOW);
digitalWrite(B1,LOW);
digitalWrite(C1,LOW);
digitalWrite(D1,LOW);
digitalWrite(E1,LOW);
digitalWrite(F1,LOW);
}

void 2bit(const int a,const int b){
digitalWrite(a,HIGH);
delay(const intervalo);
digitalWrite(a,LOW);

digitalWrite(b,HIGH);
delay(const intervalo);
digitalWrite(b,LOW);

digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
delay(const intervalo);
digitalWrite(a,LOW);
digitalWrite(b,LOW);
}

void 3bit(const int a,const int b,const int c){
2bit(a,b);
digitalWrite(c,HIGH);
limpiar();
2bit(a,b);
digitalWrite(c,LOW);
}

void 4bit(const int a,const int b,const int c,const int d){
3bit(a,b,c);
digitalWrite(d,HIGH);
limpiar();
3bit(a,b,c);
digitalWrite(d,LOW);
}

void 5bit(const int a,const int b,const int c,const int d,const int e){
4bit(a,b,c,d);
digitalWrite(e,HIGH);
limpiar();
4bit(a,b,c,d);
digitalWrite(e,LOW);
}

void 6bit(const int a,const int b,const int c,const int d,const int e,const int f){
5bit(a,b,c,d,e);
digitalWrite(f,HIGH);
limpiar();
5bit(a,b,c,d,e);
digitalWrite(f,LOW);
}

void loop(){
2bit(A1,B1);
3bit(A1,B1,C1);
4bit(A1,B1,C1,D1);
5bit(A1,B1,C1,D1,E1);
6bit(A1,B1,C1,D1,E1,F1);
}
``````

And errors it shoots in my hearts are:

``````... // A lot of this same thing:
Numeros_binarios_6LEDS.ino:53:3: error: invalid suffix "bit" on integer constant
... // And more of that...

Numeros_binarios_6LEDS:12: error: expected unqualified-id before numeric constant
... // More of that...

Numeros_binarios_6LEDS:9: error: conflicting declaration 'const int A1'
C:\...\arduino\variants\standard/pins_arduino.h:50: error: 'A1' has a previous
declaration as 'const uint8_t A1'
Numeros_binarios_6LEDS:10: error: expected unqualified-id before numeric constant
... //A lots of that to the end...
// And stressed me in my code this line:
int D1=5;
// I dont get any error with my brain
``````

And sorry, today is going to be christmas !

But i dont get nothing helpful, only semicolon common mistakes...

Hi, you cannot name a variable/const or function that begins with a digit. They must begin with a letter.

Also, A1 is not a good name to use because that is the name of an Arduino pin.

To make your code much smaller and simpler, have a look at the bitRead() function on the language reference page.

Paul

See if this works.

``````const int pins[]={ 8, 7, 6, 5, 4, 3};
const int intervalo=500;

void setup(){
for(int n=0;n<6;n++)
pinMode(pins[n],OUTPUT);
}

byte value=0;

void loop()
{
showValue();
value++;
delay(intervalo);
}

void showValue()
{
for (int n =0; n<6; n++)
{
if( value & (1 << n) )
digitalWrite(pins[n],HIGH);
else
digitalWrite(pins[n],LOW);
}
}
``````

PaulRB, thanks for that little big mistake i missed Im going to learn that functions and use them on my counter for simplifying my code…
And KenF. That was ingenious, first time i imagined the code, i was thinking in a FOR cycle, but later i didnt know how writes that… I tried that, its amazing!! But, one LED dont works how it must to do, that doesnt matter, is a wires problem…
In Venezuela still isnt christmas, but Happy Christmas in advance !

aggf1996:
In Venezuela still isnt christmas, but Happy Christmas in advance !

Happy Christmas, and check that LED is the right way around. You may have it in backwards

Thanks ! The problem was a wire, it was badly cut… I fixed that when i get out of the forum…

Hello and welcome,

This is a good case where port manipulation is very useful. So if possible, use that

For example, if you have an arduino mega and you connect your LEDs to pins A0 to A5, you can do something like that:

``````void setup()
{
// set pins A0 to A5 as outputs
DDRF = 0b00111111;
}

void loop()
{
static uint8_t value = 0;

// write value to the port
PORTF = value;

// reset value if needed
if ( value++ == 0b00111111 )
value = 0;

// pause one second
delay( 1000 );
}
``````

That's all, a binary counter in a few lines..

See Port manipulation and this table.

Thanks guix ! That is smaller and easier... I have an arduino UNO, but that code may be works, i will prove it... I'm new in Arduino, i use the basic kinds of variables, and basic functions... I will take a look to the port manipulation!...
That things i will learn in this christmas...

aggf1966 - If you want to use this code with a Uno you have to change the code slightly. The provided example for a Mega is using port F, but as there is no port F on an Uno it needs to be changed to port D which consists of digital pins 0 - 7. This is also using the serial communication pins D0 and D1, but it will not affect the circuit operation because their is no serial communication required after the initial code upload to the board. I also reduced the delay to 300.

Thanks to guix for the original code; nice clean efficient code. Got to love DPM 8)

``````// Code by guix of Arduino forum. Modified from http://forum.arduino.cc/index.php?topic=287694.0
void setup()
{
// set pins D0 to D7 as outputs
DDRD = 0b11111111;
}

void loop()
{
static uint8_t value = 0;

// write value to the port
PORTD = value;

// reset value if needed
if ( value++ == 0b11111111 )
value = 0;

// pause one second
delay( 300 );
}
``````

Thanks Pedro147 for aclaration, i also sometime i have been reading about that digital ports and bitRead() function in Arduino reference a 3 or 2 weeks ago...
I dismounted that circuit in my protoboard, but i guess it will be works...
I try to do an simple alarm with a buzzer and LDR now, but don't works probably because i'm using the wrong pins xD I will fix it by myself, i will post here the results briefly...
I use the analog pins now, almost it works !

I have been learning more about digital ports, and i think it's awesome... I'm starting to like program Arduino just like i never haven't got the Arduino board... Jaja
I think i get the idea of the variables PORTX and DDRX...
This would be simplest, guix and Pedro147...

``````/*
*/

void setup()
{
DDRD = 0b11111111;
PORTD = 0b00000000;
}

void loop()
{
if(PORTD == 0b00111111) PORTD = 0b00000000;
else PORTD++;

delay(500);    // Also that '500' would be controlled by potenciometer variable :P
}
``````

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:

``````/*
This uses the 10 LED display
It'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 you
int ledPins[] = {12,11,10,9,8,7,6,5,4,3};

// the setup function runs once when you press reset or power the board
void 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 forever
void 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 bits
void 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:

``````/*
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 zero
sorry, 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 board
void 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 forever
void 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 bits
void 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!