Need Help with LED Binary Counter

Krodal:
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:

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?

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.

JediMasterAubie:
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?

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.

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.

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

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...

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.

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

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.

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?

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

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:

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

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

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? :wink:

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

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

[quote author=Jack Christensen link=topic=102425.msg768340#msg768340 date=1335055772]
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? ;)[/quote]

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

does the sketch need to be modified at all each time I add an LED?

JediMasterAubie:
does the sketch need to be modified at all each time I add an LED?

Nope, it's blinking them all simultaneously, plus the onboard LED. The intent is just to ramp up the load slowly and see what happens.

James' suggestion is a good one, have you tried the LEDs just alone?

James, I hooked it up to the 5v out and the ground (with the resistor & LED in the circuit) and it's not illuminating the LED at all.

Jack, it appears to behave exactly as you said it would. started with them all unplugged and added em all one by one and they're all blinking in unison

Don't understand why one wouldn't illuminate directly off the power supply. Wasn't wired backwards by any chance?

Well if they're all blinking in unison, then don't touch anything :wink: just reload the original sketch!

We await with bated breath...

ARGG!!!! No joy. not illuminating the multiple bits in unison

So like the video in your original post?