I've just started using Arduino and trying some basic examples and ran into a problem I can't solve early on... I have an Arduino UNO R3 and connected it to a common anode 4 digit 7 segment display (datasheet) and a 74HC595 shift register as is shown in the attached schematic. Then I ran this simple program to test it out:
#define LATCH_PIN 8
#define DATA_PIN 11
#define CLOCK_PIN 12
int curDigit = 0;
int digitPin[] = {2,3,5,7};
int value[] = {3, 159, 37, 13, 153, 73, 65, 31, 1, 9};
void setup() {
// put your setup code here, to run once:
pinMode(LATCH_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(7, OUTPUT);
Serial.begin(57600);
}
void loop()
{
displayDigit(curDigit, value[curDigit]);
curDigit++;
curDigit %= 4;
}
void displayDigit(int digit, int value)
{
// if(digit != 1) return;
digitalWrite(LATCH_PIN, LOW);
digitalWrite(digitPin[digit], HIGH);
shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, value);
digitalWrite(digitPin[digit], LOW);
digitalWrite(LATCH_PIN, HIGH);
delayMicroseconds(100);
}
I would expect it to print [color=red]3210[/color]
but to my surprise it prints [color=red]2103[/color]
! And to make it even more strange, if I uncomment the if in displayDigit and have it working with only one digit, then it prints it in the correct place. So, if that line is
if(digit != 0) return
then 0 is displayed at the right most place on the display... If I change the pins in digitPin array to int digitPin[] = {7,2,3,5};
then the four digits are displayed correctly but the single digit is moved to the left most place instead of the right most one...
Can somebody help make sense of this thing?
Thanks in advance...