Hi
I'm using an arduino nano with a 2 digit seven segment display (http://www.ebay.co.uk/itm/121149434914?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649)
I'm using a shift register (74hc595) to limit the amount of pins used by the arduino however I'm having trouble displaying more than one digit. My code is below, I adapted it from one that counts 1 to 10, A to F, up on a single digit display. I'm getting it to display 2 different numbers from the the array however some numbers like 1 and 2 i.e. 12 comes up completely wrong whilst others such as 3 and 6 i.e 36 come up fine.
I've spent all day on this an would like any solution really, I just need it to display two numbers without any problems. I think maybe using 2 transistors instead of the shift register maybe better...
any help appreciated.
o forgot to mention my end goal is to make a 2 digit voltmeter so if anyone has already had success with one then please let me know
const int latchPin = 5; // Pin connected to Pin 12 of 74HC595 (Latch)
const int dataPin = 6; // Pin connected to Pin 14 of 74HC595 (Data)
const int clockPin = 7; // Pin connected to Pin 11 of 74HC595 (Clock)
const int d1 = 4; // turn on seg one
const int d2 = 3; // turn on seg two
unsigned long t1;
unsigned long t2;
int i = 0;
int number = 0b11110010;
// Describe each digit in terms of display segments
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
const byte numbers[16] = {
0b11111100,
0b01100000,
0b11011010,
0b11110010,
0b01100110,
0b10110110,
0b10111110,
0b11100000,
0b11111110,
0b11100110,
0b11101110,
0b00111110,
0b10011100,
0b01111010,
0b10011110,
0b10001110
};
void setup()
{
// initialisation time
t1 = millis();
//set pins to output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(d1, OUTPUT);
pinMode(d2, OUTPUT);
}
void loop()
{
digitalWrite(d1, HIGH);
digitalWrite(d2, HIGH);
digitalWrite(d1, LOW);
show(numbers[1]);
digitalWrite(d1, HIGH);
delay(10);
digitalWrite(d2, LOW);
show(numbers[2]);
digitalWrite(d2, HIGH);
delay(10);
}
void show( byte number)
{
// Use a loop and a bitwise AND to move over each bit that makes up
// the seven segment display (from left to right, A => G), and check
// to see if it should be on or not
for(int j = 0; j <= 7; j++)
{
byte toWrite = number & (0b10000000 >> j);
// If all bits are 0 then no point writing it to the shift register,
// so break out and move on to next segment.
if(!toWrite) { continue; }
// Otherwise shift it into the register
shiftIt(toWrite);
}
}
void shiftIt (byte data)
{
// Set latchPin LOW while clocking these 8 bits in to the register
digitalWrite(latchPin, LOW);
for (int k=0; k <= 7; k++)
{
// clockPin LOW prior to sending a bit
digitalWrite(clockPin, LOW);
if ( data & (1 << k) )
{
digitalWrite(dataPin, HIGH); // turn “On”
}
else
{
digitalWrite(dataPin, LOW); // turn “Off”
}
// and clock the bit in
digitalWrite(clockPin, HIGH);
}
//stop shifting out data
digitalWrite(clockPin, LOW);
//set latchPin to high to lock and send data
digitalWrite(latchPin, HIGH);
}