Help with arduino uno and led 1x10 array program

Hi! i’m new to the arduino and i’ve bought several components to start learning how it works. i’ve written a small program to count from 0 to 1023, and display the number as a binary value on a 1x10 LED array.

short description of the hardware setup, i’ve done it all on a protoshield. the LED array is connected with the anodes to pins 4 through 13. and the cathodes are connected via one shared resistor to the ground. one potentiometer is connected to the analog pin 0, and 5v and ground of course.

I’ve posted a video on youtube, but since this is my first post here i’m not allowed to post the link yet.

If you watched the video, then you’d see the arduino doesn’t quite do what I want. it works for the first 4 bits perfectly, but skips the 5th, and the 6th behaves oddly.

here’s the code for it.

#include <math.h>

int pin[10],pot = 0;

int val = 0;

void setup(){
  for(int i = 0;i < 10; i++){
    pin[i] = 4+i;
    pinMode(pin[i], OUTPUT);
  }
}
int n = 0;
void loop(){
  
  val = analogRead(pot);
  if(n & 0x0001)
    digitalWrite(pin[9],HIGH);
  else
    digitalWrite(pin[9],LOW);
  if(n & 0x0002)
    digitalWrite(pin[8],HIGH);
  else
    digitalWrite(pin[8],LOW);
  if(n & 0x0004)
    digitalWrite(pin[7],HIGH);
  else
    digitalWrite(pin[7],LOW);
  if(n & 0x0008)
    digitalWrite(pin[6],HIGH);
  else
    digitalWrite(pin[6],LOW);
  if(n & 0x0010)
    digitalWrite(pin[5],HIGH);
  else
    digitalWrite(pin[5],LOW);  
  if(n & 0x0020)
    digitalWrite(pin[4],HIGH);
  else
    digitalWrite(pin[4],LOW);
  if(n & 0x0040)
    digitalWrite(pin[3],HIGH);
  else
    digitalWrite(pin[3],LOW);
  if(n & 0x0080)
    digitalWrite(pin[2],HIGH);
  else
    digitalWrite(pin[2],LOW);
  if(n & 0x0100)
    digitalWrite(pin[1],HIGH);
  else
    digitalWrite(pin[1],LOW);
  if(n & 0x0200)
    digitalWrite(pin[0],HIGH);
  else
    digitalWrite(pin[0],LOW);
  delay(val);  
  n = (n+1) % 1024;
}

please help with this, i’ve obviously made some errors in the code, or my arduino’s broken, most likely it’s my code that’s busted.

Well I won't comment of the code for now, but the way you have it wired will not work very well. By using just one resistor to limit current the leds will dim as more and more of them are turned on. The proper way would be a resistor for each output pin to each individual anode of a led, then all the led cathodes can wire to ground. Then the brightness of each led will be independent to how many of the leds are turned on. Size the resistors to limit current to whatever the recommended current if for your leds, 10-20ma is a common current rating.

Caution, if you run 10 leds on at 20ma each (for count = 1023) you are then running right at the maximum rated total current consumtion for the AVR processor, so best limit it to 10ma or less for each led.

Lefty

Alright, I will put one resistor on each anode. but I don't think that will solve the other problem with the leds behaving the way they do.

http://www.youtube.com/watch?v=-qObxPHtk4g

  n = n+1 % 1024;

Do you know whether the + operator or the % operator has higher precedence? Parentheses would remove the ambiguity.

Changed it, no change in behaviour.

Try a simple sketch that simply loops from 0 to 9. In the loop, set all pins LOW, then set one pin HIGH, and delay.

Perhaps it is a wiring problem.

Okay, I tried that now. and like the counter it totally skips the same pins, am I unable to use the digital pins with PWM as normal digital pins?

int pin[10];

void setup(){
  for(int i = 0; i < 10; i++){
    pin[i] = 4+i;
    pinMode(pin[i],OUTPUT);
  }
  
}

void loop(){
  digitalWrite(pin[0],HIGH);
  digitalWrite(pin[9],LOW);
  delay(100);
  digitalWrite(pin[1],HIGH);
  digitalWrite(pin[0],LOW);
  delay(100);
  digitalWrite(pin[2],HIGH);
  digitalWrite(pin[1],LOW);
  delay(100);
  digitalWrite(pin[3],HIGH);
  digitalWrite(pin[2],LOW);
  delay(100);
  digitalWrite(pin[4],HIGH);
  digitalWrite(pin[3],LOW);
  delay(100);
  digitalWrite(pin[5],HIGH);
  digitalWrite(pin[4],LOW);
  delay(100);
  digitalWrite(pin[6],HIGH);
  digitalWrite(pin[5],LOW);
  delay(100);
  digitalWrite(pin[7],HIGH);
  digitalWrite(pin[6],LOW);
  delay(100);
  digitalWrite(pin[8],HIGH);
  digitalWrite(pin[7],LOW);
  delay(100);
  digitalWrite(pin[9],HIGH);
  digitalWrite(pin[8],LOW);
  delay(100);
}

am I unable to use the digital pins with PWM as normal digital pins?

PWM pins can be used as regular digital pins. This code, though, is missing the pinMode statements that set all the LED pins as OUTPUT.

I'd take a really close look at the wiring. If you tell pin 8 to go HIGH, and the wrong LED segment lights up, that is not a pin problem.

Opps, well I fixed the pinMode thing in setup, the LEDs light up more brightly now, but pins 4,5,6,7, and 9 doesn't light up, I couldn't have wired it wrong. all digital pins 4 through 13 are directly connected to one side of the LED array. the other side are all connected together with one 470ohm resistor to the ground, I know this is bad and will change later when trying to light more than one led at a time.

but pins 4,5,6,7, and 9 doesn't light up,

So, it's not related to PWM, then, since pins 4 and 7 are not PWM pins.

I'm afraid that you have toasted some of the Arduino pins. Or, perhaps the LED array is not wired the way you think it is. Got a link to it?

look at the youtube video a few posts up, I might as you said have toasted the pins that do not work, but since they never worked once the times i've tried and i've never shorted the pin with ground or anything like that.

I moved the whole rigg to another breadboard, and everytning worked fine, so it’s my protoshield that’s crap. thanx for your time guys :slight_smile: