Pages: [1] 2 3   Go Down
Author Topic: Need Help with LED Binary Counter  (Read 2517 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have been trying to get a binary counter to work for the past couple of months as my first project trying to get familiar with the Arduino Uno.  Had to take a break from it and giving it a second go but I'm running into the same problems as before.  I'm using LED's to represent each bit, presently 5 bits.  The problem I'm having is the bits representing a number are not illuminated simultaneously; instead, (all the correct bits) are strobing from one to the next.

I have available, my code, a schematic, and a video showing what it's doing:

The Schematic      https://picasaweb.google.com/lh/photo/2fKDMbs4-bzWqC8GOTlEoNMTjNZETYmyPJy0liipFm0?feat=directlink
The Video            http://www.youtube.com/watch?v=Fgn2yi_lr8E&feature=youtu.be

#define LED0 7
#define LED1 8
#define LED2 9
#define LED3 10
#define LED4 11
int pinShift = 7;
int numBits = 5;
int startNum = 0;
int endNum = pow( 2, numBits );

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

}

void loop()
{
for ( int i = startNum; i < endNum + 1; i++ )
{
for ( int j = LED0; j < LED4 + 1; j++ )
{
// Read our bit from the number (0 - 15 ) and see if the bit (0 - 3)
// is on or off
//
digitalWrite( j, bitRead( i, j - pinShift ) );

}
delay( 1000 );
}
}

All help would be very appreciated.  Thanks!
Mike
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You might want to rewrite your loop.
The digitalWrite uses HIGH and LOW, but the bitRead returns '0' or '1'. I don't know if that is valid, but I think it is not.

Perhaps you can start simple:
Code:
if bitRead(i, 0)
  digitalWrite(LED0,HIGH);
else
  digitalWrite(LED0,LOW);
That would result in a blinking led.

And I would do the same for all bits:
Code:
for ( int j = LED0; j <= LED4; j++ )
{
  // Check the bit in counter 'i',
  // and set the according led.
  if bitRead(i, j-pinShift)
    digitalWrite(j,HIGH);
  else
    digitalWrite(j,LOW);
}

As you can see, I use "j <= LED4". That is to avoid problems, since "LED4 + 1" might not be valid in some cases. You could do the same for the loop with the "i" counter.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The digitalWrite uses HIGH and LOW, but the bitRead returns '0' or '1'. I don't know if that is valid, but I think it is not.
There's no difference between HIGH and 1, or LOW and 0:
Code:
Arduino.h:#define LOW  0x0
Arduino.h:#define HIGH 0x1

Jedi, what happens if you change the delay to something extreme like 5000.  Does the behavior change in any way?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Changing the delay to 5000 does something quite interesting.  For 1, the first bit blinks 5 times.  For 2, the second bit blinks 5 times.  For 3, the second bit blinks then the first bit blinks (repeating this 5 times); this pattern continues for the rest of the count.

They're blinking in pulses just as short as before but it's as if the delay is telling it to blink once every 1000ms.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Changing the delay to 5000 does something quite interesting.  For 1, the first bit blinks 5 times.

What value resistor are you using on the LED?
Is that the correct code you posted?  (Please wrap it with code tags and run Tools -> Auto Format before posting).
I can't tell for sure, but your GND connections all line up to the same row, right?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's very strange.
It could be a low voltage, or watchdog timer, but then the result should not be like this.
So begin with something even more simpler: A blinking led.
Code:
voide loop()
{
  digitalWrite (LED0, HIGH);
  delay(100);
  digitalWrite (LED0, LOW);
  delay(300);
}
Would this result in a fast blinking led.
If not, go for something even more simpler: just turn on some leds and see if they stay on.

Are you sure that the code is actually what you test. Or do you use an analogWrite() or tone() function that uses outputs. I checked the video, and every second something happens, but sometimes there are small delays between the leds within that second. That is almost impossible.
« Last Edit: April 21, 2012, 07:17:49 pm by Krodal » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

James, I'm using 470 ohm resistors.  And here is the formatted script:

#define LED0 7
#define LED1 8
#define LED2 9
#define LED3 10
#define LED4 11
int pinShift = 7;
int numBits = 5;
int startNum = 0;
int endNum = pow( 2, numBits );

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

}

void loop()
{
  for ( int i = startNum; i < endNum + 1; i++ )
  {
    for ( int j = LED0; j < LED4 + 1; j++ )
    {
      // Read our bit from the number (0 - 15 ) and see if the bit (0 - 3)
      // is on or off
      //
      digitalWrite( j, bitRead( i, j - pinShift ) );

    }
    delay( 5000 );
  }
}
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4092
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I couldn't see any problem with the code, so I downloaded it and wired up five LEDs ... works perfectly.

You must have hardware gremlins...
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've actually had that exact suspicion.  So I bought a second Uno today and it's behaving exactly the same as it did on the first.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Should I have anything wired differently?  I posted a link to my schematic, with pin numbers, in the first post.
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4092
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've actually had that exact suspicion.  So I bought a second Uno today and it's behaving exactly the same as it did on the first.

No kidding. Yet sketches upload OK. How are the Unos being powered?

I'd go back to the absolute basic: Disconnect everything except the USB cable. Upload the blink sketch (File > Examples > Basics > Blink). Should blink the onboard LED, on for 1 sec, off for 1 sec. Is that what happens?
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Powered by the USB.  Blink seems to be working just fine.  The onboard LED blinks exactly as it should; and is responding correctly to alterations to the delay
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4092
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

There is a minor issue with the code, but it is not causing the issue here, actually it's a case where two wrongs make a right.

pow() returns a float. Floats being inexact, pow(2, numBits) must be returning 31.999 or some such, and therefore endNum ends up being 31, not 32 as I might assume at first.

But in the first for loop, this is compensated for, with i < endNum + 1. So as written, the code works exactly as intended. However, I'd do this instead:

Code:
#define LED0 7
#define LED1 8
#define LED2 9
#define LED3 10
#define LED4 11
int pinShift = 7;
int numBits = 5;
int startNum = 0;
int endNum = 1 << numBits;

void setup() {
    Serial.println(endNum, DEC);
    pinMode( LED0, OUTPUT );
    pinMode( LED1, OUTPUT );
    pinMode( LED2, OUTPUT );
    pinMode( LED3, OUTPUT );
    pinMode( LED4, OUTPUT );

}

void loop()
{
    for ( int i = startNum; i < endNum; i++ )
    {
        for ( int j = LED0; j < LED4 + 1; j++ )
        {
            // Read our bit from the number (0 - 15 ) and see if the bit (0 - 3)
            // is on or off
            //
            digitalWrite( j, bitRead( i, j - pinShift ) );

        }
        delay( 500 );
    }
}



Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4092
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Powered by the USB.  Blink seems to be working just fine.  The onboard LED blinks exactly as it should; and is responding correctly to alterations to the delay

Well I'm about ready to call Mulder and Scully. Someone didn't slip you some of those LEDs with the built-in blinker did they? smiley-wink

Try the sketch below. Start with no LEDs connected to the board, then add your LEDs one at a time as before.

Code:
#define LED0 7
#define LED1 8
#define LED2 9
#define LED3 10
#define LED4 11
#define ledDelay 1000
int pinShift = 7;
int numBits = 5;
int startNum = 0;
int endNum = 1 << numBits;

void setup()
{
    Serial.println(endNum, DEC);
    pinMode( LED0, OUTPUT );
    pinMode( LED1, OUTPUT );
    pinMode( LED2, OUTPUT );
    pinMode( LED3, OUTPUT );
    pinMode( LED4, OUTPUT );
}

void loop()
{
    for (int i=LED0; i<=LED4; i++) {
        digitalWrite(i, HIGH);
        digitalWrite(13, HIGH);
    }
    delay(ledDelay);

    for (int i=LED0; i<=LED4; i++) {
        digitalWrite(i, LOW);
        digitalWrite(13, LOW);
    }
    delay(ledDelay);
}
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well I'm about ready to call Mulder and Scully. Someone didn't slip you some of those LEDs with the built-in blinker did they? smiley-wink

Huh.  So... uh.. .what happens when one of these LEDs are connected directly to 5V and GND (through the resistor)?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Pages: [1] 2 3   Go Up
Jump to: