Go Down

Topic: Need Help with LED Binary Counter (Read 2 times) previous topic - next topic

JediMasterAubie

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

Krodal

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: [Select]

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: [Select]

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.

James C4S


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: [Select]

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?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

JediMasterAubie

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.

James C4S


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?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Krodal

#5
Apr 22, 2012, 02:09 am Last Edit: Apr 22, 2012, 02:17 am by Krodal Reason: 1
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: [Select]

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.

JediMasterAubie

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 );
  }
}

Jack Christensen

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...
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

JediMasterAubie

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.

JediMasterAubie

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

Jack Christensen


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?
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

JediMasterAubie

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

Jack Christensen

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: [Select]
#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 );
   }
}




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

Jack Christensen


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? ;)

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

Code: [Select]
#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);
}
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

James C4S


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? ;)


Huh.  So... uh.. .what happens when one of these LEDs are connected directly to 5V and GND (through the resistor)?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Go Up