Help with a 2 digit seven segment display

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


}

..take a really good look at the "shiftOut()" command
It can replace a lot of your code.

Chain the two registers. Shift out 16 bits (2xshiftOut)
Then latch and show both registers.

This example is for one digit. U just chain the two .. and shift out two bytes

/*
Exampelprogram counts 0.. 9 - repeated
connect to 74HC595 shiftregister Qa-A, Qb-B ..> t0 Qh-decimalpoint
dataPin to SI(pin14)l
latchpin to RCK(pin12)
clockpin to SCK(pin11)
pin8 and pin13 to 0V
pin 16 og pin10 to 5V (shiftregister clear)
*/
int dataPin  = 2; //Defines pins for ShiftRegister-control
int latchPin = 3;
int clockPin = 4;

byte siff[10] ={63,6,91,79,102,109,125,7,127,111};  //digits 0..9

void setup()
{
    pinMode(dataPin, OUTPUT);  //3 out-pins
    pinMode(latchPin, OUTPUT);
    pinMode(clockPin, OUTPUT);
}

void loop()
{
    for (int i = 0; i <=9 ; i++)  //counter for array where patterns are  stored
    {
      digitalWrite(latchPin, LOW); //Steng utgangslatch. Åpne for data inn
      shiftOut(dataPin, clockPin, MSBFIRST, 255-siff[i]);  //Transfer one  byte
      digitalWrite(latchPin, HIGH); //move data til outputs
      delay(500);
      // several Shiftregister can be chained 
    }
}

Hi and thanks for your reply, what do you mean by chain registers? I would rather use one...
Is chaining when you use 2 registers in series?

..OK. I understand you want to use just one.. My setup was two shiftregister in series.
It is of course possible to use your setup.
Send A's data - latch it - disable B - display A.
Sned B's data - latch - disable A - display B.
.........
This way the CPU gets a higher load than using two registers..

Hi m600,

I'm surprised even one digit displays with that code! You are multiplexing the 2 digits with a single shift register, but some parts of the code read like they came from another sketch where multiplexing was not used. So I can see why Knut_ny was confused, but was right to say get rid of the write() and shiftIt() functions in favour of the standard shiftOut() function. Also you should move your delay() functions to be before you set d1 and d2 back to HIGH.

There is a danger of damaging your Nano or the shift register here. How many series resistors are you using and what value? Are you using transistors as you mentioned before?

As for reading voltages, you will probably need a voltage divider and maybe use the internal voltage reference, which is 1.1V on a Nano I think. What range of voltages to you need to measure, and how will the Nano be powered?

Paul