Need some help with LED binary counter, not sure if it's hardware or software

Hi,

I'm very new to Arduino and not very experienced with writing code. I have been trying to figure out how to write a script to make an LED binary counter without any additional chips. I've made progress borrowing a few codes I've found online but I'm having a problem, which I'm not sure if it's hardware or code. The problem being that for a number with multiple bits, ie 7 = 1110, instead of illuminating all 3 bits at once, the three just strobe down the breadboard, one after the other. I have the sketch set to delay for 1 second on each count but this behavior is just beyond me.

As for hardware, it's an Uno, 4 LED's, and jumpers.

As for the code:

const int MAX = 15;
int currentCount = 0;
/*

  • Pins 2 - 11 connected through voltage regulating resistor, LEDs then to GND.
  • Least significant bit is first as array is backwards.
    */
    int leds[] = {2, 3, 4, 5};

char data[4];

void setup() {
for(int i=2; i < 6; i++) {
pinMode(i, OUTPUT);
}
}

void loop() {
if (currentCount > MAX) {currentCount = 0; }

binaryFromNumber(currentCount++);
displayNumber();
}

/*

  • Turns on LEDs that are set to 1 and turns off any others
    /
    void displayNumber() {
    for (int i=4; i >= 0; i--) {
    if (data == '1') {
    _digitalWrite(leds
    , HIGH);_
    _
    } else {_
    _digitalWrite(leds, LOW);
    }
    }
    delay(1000);
    }
    /

    * Fills char array with reversed binary number.
    * The leds array is backwards so this displays fine.
    /
    void binaryFromNumber(long decimal) {
    char temp[4];
    int i=0;
    do {
    int remain = decimal % 2;
    decimal = decimal / 2;
    temp[i++] = remain + '0';
    } while (decimal > 0);
    for(int j=0; j < 4; j++) {
    data[j] = (j < i) ? temp[j] : '0';
    }
    }*
    Anyone have any suggestions? All advice is much appreciated.
    Thanks,
    Mike_

Okay - This first line describes leds as an array. those element are leds[0]=2, leds[1]=3,leds[2]=4 and leds[3]=5 which I take to mean pins 2, 3, 4 & 5
Arrays in C start with element 0.
int leds[] = {2, 3, 4, 5};

data is also defined as an array data[0] through data[3] for 4 elements.
char data[4];

Now in this function you use leds but without a subscript (that's what the number between the [ ] is called) and I should go from 3 down. You have no idea what is in leds[4] because you didn't initialize it.
void displayNumber() {
for (int i=3; i > 0; i--) { // old line - for (int i=4; i >= 0; i--) {
if (data == '1') { // old line - if (data == '1') {
_ digitalWrite(leds*, HIGH); // old line - digitalWrite(leds, HIGH);_
_
} else {_
_ digitalWrite(leds, LOW); // old line - digitalWrite(leds, LOW);
}
}
delay(1000);
}*
Also try some indenting as it makes it much easier to read and debug._

Thanks for the tips. I've made the adjustments to the code buy I'm still getting the strobing effect from 1 LED to the next.

The one other thing I should note is there was a comment put in there by the original writer that I left in there but didn't implement that is worth mentioning should this be something on the hardware end of things. They said they used resistors in the circuit. I honestly don't see how this would get multiple LED's to light up at once and prevent the strobing; my education says that would only dim the LED's.

Any ideas?

The resistors should be there to protect the Arduino. They prevent the LEDs from drawing too much current. You can damage the output pins without them.

you need a 220 Ohm to 470 Ohm resistor between the Arduino output and the LED. Without it you will damage the Arduino outputs.

What you might be seeing is the turning on and off of the 1 bit as you count up. Every other number is ODD so the 1 bit would be lit for every other number.

Don't have my Arduino here at work so I can't test the code.

Aside from a little thing here and there found in the code, a friend came over and started messing around with it and testing it with a really simple sketch to turn all 4 LED's on & off simultaneously and starting to believe I may have gotten a bad board. Wrote the simplest code possible and yet the 4 LED's started lighting up in a pattern that varied over time as seen in this vid i uploaded to youtube.

and just for the sake of showing I'm not on crazy pills, here's the code:

void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
pinMode(2, OUTPUT);

}

void loop() {
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
digitalWrite(3, HIGH);
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
digitalWrite(2, LOW);

}

Another thing adding to the case that i've got a bad board. You know that blink code in the basic examples? I tried running it and playing with the delays and though I could get the built in LED's timing to change, I could not get a bread boarded LED jumped to pin 13 to flash in sync with the one built in.

Anyone agree that I've got a bad board or is there something basic I'm missing here?

Thanks!

Fix your code to turn them off briefly also:

unsigned long nextinterval = 0;
void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);  
  pinMode(3, OUTPUT);  
  pinMode(2, OUTPUT);

}

void loop() {
if (millis()>= nextinterval){
nextinterval=nextinterval+1000;
 digitalWrite(5, HIGH);
 digitalWrite(4, HIGH);
 digitalWrite(3, HIGH);
 digitalWrite(2, HIGH);
 delay(490);
 digitalWrite(5, LOW);
 digitalWrite(4, LOW);
 digitalWrite(3, LOW);
 digitalWrite(2, LOW);
 }
 }

That resulted in the LED's not turning on at all. Is this further evidence the board is toast?

No, probably just coding error somewhere.
You can make the LEDs turn on/off so the board is good.
Making them flash steadily just requires better coding.

That is interesting.

I hope the limiting resistor is about 220 to 470.

Can you try this code. Just modify the array and the number of output being use. If you know how. If not, well it should work even nothing is connected anyway.

The code do : a "blink" for each digital output pin. Turn on for 1 second, turn off for 1/2 second. For EVERY digital pins except pin 1 and 0.

Try it and see what happen.

// check all digital pins except pin 1 and 0
// Those a reserved pins for Com link

const byte the_pins[12] = {13,12,11,10,9,8,7,6,5,4,3,2};
int number_of_output = 12;

void setup() 
{                
  for (int i=0;i<number_of_output;i++)
  {
    pinMode(the_pins[i], OUTPUT);
    digitalWrite(the_pins[i], LOW);
  }    
}

void loop() 
{
  for (int i=0;i<number_of_output;i++)
  {
    digitalWrite(the_pins[i], HIGH);
    delay(1000);
    digitalWrite(the_pins[i], LOW);
    delay(500);
  }  
}

Success!!! Thank you so much.

Certainly saves a trip to radio shack for a new board. Now I just gotta figure out how to get the code to count in binary!

That is good.

Here a code I did. It is a way to do it.

And please go visite this site A LOT... Arduino - Home

It will help you to code the Arduino.

/*

Version 1.0
Size : 1182 Bytes

This program will count 0 to 15 and display the result in binary.

TESTED and WORKING

The hardware is connected to a transistor to use as a switch.

Need : 4 Transistor : Common NPN ex: 2N3904
       4 1 K resistor : between the digital pin and the base of the transistor.
       4 LED : Any color
       4 330 ohms resistor : current limiting for the LED.
       
And try to power the LEDs circuit seperated. And don't forget to connect the gnd of the
circuit with the Arduino GND.

Created by Serge Desjardins  --->  Techone
           Toronto, ON   CANADA

Idea from "Bit Math Tutorial by CosineKitty" in Arduino Playground.
           
*/

const int led[4] = {9,10,11,12};

/* Set Output LEDs  --->  A B C D   MSD --- LSD
          Digital pin    12 11 10 9

   Common variables
*/

int pin;
int counting;


void setup()

/*
  Set the pin 12, 11 , 10 , 9  to OUTPUT
*/
{
   for (pin =0; pin < 4; pin++ )
   {
     pinMode (led[pin], OUTPUT);
   }   
}

void loop()

/*  
  Counting routine from 0 to 15 and wait for 1 second
*/
{
 for ( counting = 0; counting < 16; counting++)
 {
    LEDview();
    delay (1000); // The One second delay
 }
}

void LEDview()

/*
  The LED display subroutine. The trick is to brake down the 4 bit data
  ( 0 to 15 ---> 0000 to 1111, so I need HIGH or LOW ( 1 or 0 ) Humm ... boolean
 isolate the proper bit and shift the bit, so it equal in value 1 or 0, 
 so I use the AND operator & ( to isolate ) and the SHIFT RIGHT >> ( to move at 
the proper spot )

My idea came from "Bit Math Tutorial by CosineKitty" in the Arduino playground

From there, you can use a 7447 to count the data ( change 16 to 10 in the counter line ) or 
other use if need parallel data, the only limitation is the digital lines do not come out at the same 
time, but in this way, it is almost at the same time. Bear in mind the small delay after each digitalWrite 
instruction.

*/
{
  digitalWrite (led[3], boolean ((counting & 8)>>3));
  digitalWrite (led[2], boolean ((counting & 4)>>2));
  digitalWrite (led[1], boolean ((counting & 2)>>1));
  digitalWrite (led[0], boolean (counting & 1));
}

Mike:

You should consider looking into port manipulation.

This information says that "Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board" but for your application it will probably make things a lot easier as well. You will be limited to using sequential port pins but that is probably the only drawback.

Don

Thanks Techone.

I've been going through the Bit Math tutorial and I'm starting to understand everything in your code but I noticed in your comments that you said I need transistors. I understand the fundamentals of how a transistor works but I don't understand why they are needed here; my only experience with them is when there's two power sources for signal and for operation. And to that regard, I'm a little lost where the leads should go in the circuit since there's only one power source to the breadboard, the arduino.

If you can help me understand this a little better I would be very grateful.
Mike

JMA,
Techone has the essential change you need. Figure out the 4 data levels at once by using the "bitwise and" to pick off bits.
The values and therefore the single bit masks for each "digit" are powers of 2. Thus:
data4 = i & 8;
data3 = i & 4;
data2 = i & 2;
data1 = i & 1;
Then output data4 through data 1 to all 4 leds.

@JediMasterAubie

I am sorry that I confuse you about transistors. In your example-project, the need of transistors is optional. But in some projetcs, a transistor is being use to switch on or off a bigger currents like a relay, a higher led current. The Arduino chip can only produce a current max of 40 mA <--- Close to that value is "risky" Go ahead if you want to lose your expensive Ardiuno board. A transistor is cheaper, in case something goes wrong <-- who care about a $0.25 per transistor. I prefer loose a quarter transistor compare to loose a $40 Arduino board.

About learning about transistors use as a switch... here a link : Transistor Circuits