/*
  Sieben_Segment_x2
  
 counts and displays from 0-99 and backwards 
 
 The circuit:
 * 2x 74HC_HCT4094 shift register
 * 2x 7-segment display
 
 created 2012
 by hennisoft <http://www.hennisoft.de>
 modified 17 Feb 2013 by
 Henning Buchholz
 
 Based on:
 CC by-sa 3.0
 http://tronixstuff.wordpress.com
 
 */

int latchpin = 8; //  --> Pin 1 (STR)
int clockpin = 12;//  --> Pin 3 (CP)
int datapin = 11; //  --> Pin 2 (D)

float b = 0;
int c = 0;
float d = 0;
int e = 0;
int speed = 150; // used to control speed of counting

// digits 0,1,2,3,...,9,dot
int segdisp[11] = {192,249,164,176,153,146,130,248,128,144,127};

void setup()
{
  pinMode(latchpin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  pinMode(datapin, OUTPUT);
}
void loop()
{ 
  
  //  Count up
  for (int z=0; z<100; z++)
  {
    digitalWrite(latchpin, LOW);
    shiftOut(datapin, clockpin, MSBFIRST, 255); // clears the right display
    shiftOut(datapin, clockpin, MSBFIRST, 255); // clears the left display
    digitalWrite(latchpin, HIGH);

    if (z<10)
    {
      digitalWrite(latchpin, LOW);
      shiftOut(datapin, clockpin, MSBFIRST, segdisp[z]); // sends the digit down the serial path
      shiftOut(datapin, clockpin, MSBFIRST, 255); // sends a blank down the serial path to push the digit to the right
      digitalWrite(latchpin, HIGH);
    }
    else if (z>=10)
    {
      d=z%10; // find the remainder of dividing z by 10, this will be the right-hand digit
      c=int(d); // make it an integer, c is the right hand digit
      b=z/10; // divide z by 10 - the whole number value will be the left-hand digit
      e = int(b); // e is the left hand digit
      digitalWrite(latchpin, LOW); // send the digits down to the shift registers!
      shiftOut(datapin, clockpin, MSBFIRST, segdisp[c]); 
      shiftOut(datapin, clockpin, MSBFIRST, segdisp[e]); 
      digitalWrite(latchpin, HIGH);
    }
    delay(speed);
  }
 // delay(2000);
  
  for(int i=0; i < 3; i++){
    digitalWrite(latchpin, LOW);
    shiftOut(datapin, clockpin, MSBFIRST, 255); // clears the right display
    shiftOut(datapin, clockpin, MSBFIRST, 255); // clears the left display
    digitalWrite(latchpin, HIGH);
    delay(200);
    digitalWrite(latchpin, LOW);
    shiftOut(datapin, clockpin, MSBFIRST, segdisp[10]); // dot to the right display
    shiftOut(datapin, clockpin, MSBFIRST, segdisp[10]); // dot to the left display
    digitalWrite(latchpin, HIGH);
    delay(200);
  }
  
  //  Count down
  for (int z=99; z>=0; z--)
  {
    digitalWrite(latchpin, LOW);
    shiftOut(datapin, clockpin, MSBFIRST, 255); // clears the right display
    shiftOut(datapin, clockpin, MSBFIRST, 255); // clears the left display
    digitalWrite(latchpin, HIGH);

    if (z<10)
    {
      digitalWrite(latchpin, LOW);
      shiftOut(datapin, clockpin, MSBFIRST, segdisp[z]); // sends the digit down the serial path
      shiftOut(datapin, clockpin, MSBFIRST, 255); // sends a blank down the serial path to push the digit to the right
      digitalWrite(latchpin, HIGH);
    }
    else if (z>=10)
    {
      d=z%10; // find the remainder of dividing z by 10, this will be the right-hand digit
      c=int(d); // make it an integer, c is the right hand digit
      b=z/10; // divide z by 10 - the whole number value will be the left-hand digit
      e = int(b); // e is the left hand digit
      digitalWrite(latchpin, LOW); // send the digits down to the shift registers!
      shiftOut(datapin, clockpin, MSBFIRST, segdisp[c]); 
      shiftOut(datapin, clockpin, MSBFIRST, segdisp[e]); 
      digitalWrite(latchpin, HIGH);
    }
    delay(speed);
  }
  delay(2000);  
}