int latchPin =35; // Latch pin of 74HC595 is connected to Digital pin 5
int clockPin = 36; // Clock pin of 74HC595 is connected to Digital pin 6
int dataPin = 34;
int digitSelect2Pin = 37;//7
int digitSelect1Pin = 12;
int digitSelect3Pin = 11;
int digitSelect4Pin = 10;
int h=0;
int g=0;
int q=0;
// Data pin of 74HC595 is connected to Digital pin 4
int t = 0;
int s = 0;
int x = 0;
int f = 0;
byte leds1 = 0;
byte leds2 = 0;
byte leds3 = 0;
byte leds4 = 0;
int analogpin = A0;
int speed = 0;
int servoPin=3;
int pulse=9;
int count = 0;
int all =0;
int a=0;
int b=0;
int c=0;
int d=0;
int n1=0;
int n2=0;
int n3=0;
int n4=0;
int blank=0;
// Higher = slower count
// Variable to hold the pattern of which LEDs are currently turned on or off
int num1[10][7] = { { 1, 1, 1, 1, 1, 1, 0 }, // 0
{ 0, 1, 1, 0, 0, 0, 0 }, // 1
{ 1, 1, 0, 1, 1, 0, 1 }, // 2
{ 1, 1, 1, 1, 0, 0, 1 }, // 3
{ 1, 1, 1, 0, 0, 1, 0 }, // 4
{ 1, 0, 1, 1, 0, 1, 1 }, // 5
{ 1, 0, 1, 1, 1, 1, 1 }, // 6
{ 1, 1, 0, 0, 0, 1 }, // 7
{ 1, 1, 1, 1, 1, 1, 1 }, // 8
{ 1, 1, 1, 0, 0, 1, 1 } }; // 9
int num[16][1] = {
{ 63 }, // 0 Add 128 to turn decimal point
{ 48 }, // 1
{ 109 }, // 2
{ 121 }, // 3
{ 114 }, // 4
{ 91 }, // 5
{ 95 }, // 6
{ 49 }, // 7
{ 127 }, // 8
{ 115 }, // 9
{ 119 }, //A
{ 94 }, //b
{ 15 }, //C
{ 124 }, //D
{ 79 }, //E
{ 71 }, //F
};
int test[5][5]{ 1, 2, 3, 4, 5, 67, 8, 9, 10 };
int l=0;
int m=0;
/*
* setup() - this function runs once when you turn your Arduino on
* We initialize the serial connection with the computer
*/
void setup() {
Serial.begin(9600);
// Set all the pins of 74HC595 as OUTPUT
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(digitSelect1Pin, OUTPUT);
pinMode(digitSelect2Pin, OUTPUT);
pinMode(digitSelect3Pin, OUTPUT);
pinMode(digitSelect4Pin, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(47,INPUT_PULLUP);//this is the input pin for the external pulse on counter 5
TCCR5A = 0; // Init Timer1A
TCCR5B = 0; // Init Timer1B
TCCR5B |= B00000111; // External Clock on T1 Pin (RISING)
TCNT5 = 0; //timer 5 as a counter for the optical sensor
TCCR4B = 0;
TCCR4A = 0;
TCCR4B = 0b10000101;
ICR4 = 310; //310
OCR4A = 8;
TCCR4A |= 1<<WGM41; //00000010
TCCR4B |= (1<<WGM42| (1<<WGM43)) ;
TCCR4A |= (1<<COM4A1); //10000010
TIMSK4 |= (1<<OCIE4A); //counter 4 drives the servo motor
//Serial.print("TIMSK4 1 = ");
//Serial.println(TIMSK4,BIN);
sei();
digitalWrite(digitSelect1Pin, 0);
digitalWrite(digitSelect2Pin, 1);
digitalWrite(digitSelect3Pin, 1);
digitalWrite(digitSelect4Pin, 1);
}
void loop() {
//d1();
convert();// this funtion breaks down any number given to into it's one, tens, hundreds...
a=TCNT5/22;// this is simulating the opticl interrupter signal to the counter
if (a<1000) //these if's blank the leading zeros on the display
leds4=blank;
else
leds4 = num[n4][0];
if (a<100)
leds3=blank;
else
leds3 = num[n3][0];
if (a<10)
leds2=blank;
else
leds2 = num[n2][0];
//leds1 = num[n1][0];//no need to blank ones
display(); //This function writes the converted number to the l.e.d. disply
if(a>9999){ //reset the counter to zero when all 4 digits are full 9999
TCNT5=0;
}//end if 9999
//a=TCNT5/30;
}//end loop
void d1() { // d1 through d4 functions are never called. they are for the leds counting with out a input fron thhe timer
for (x = 0; x < 10; x++) {
leds4 = num[0][0];
if (f < 9)
leds4 = 0;
if (x == 0)
leds4 = 0;
d2();
}
}
void d2() {
for (t = 0; t < 10; t++) {
leds3 = num[2][0];
if (f < 9)
leds3 = 0;
if (t == 0 && x == 0) {
leds3 = 0;
}
d3();
}
}
void d3() {
for (s = 0; s < 10; s++) {
leds2 = num[3][0];
if (f < 9)
leds2 = 0;
if (t == 0 && x == 0 && s == 0) {
leds2 = 0;
}
d4();
}
}
void d4() {
for (f = 0; f < 10; f++) {
leds1 = num[4][0];
//leds1=63;
speed = analogRead(A0);
if (speed == 0)
speed = 1;
}
}
void display(){
for (int w = 0; w <1; w++) { //Dislplays the same output "speed" times Higher= slower
digitalWrite(digitSelect1Pin, 0);
updateShiftRegister1(); // this togles the digit on and off very fast so it looks like
digitalWrite(digitSelect1Pin, 1); //it is on all the time.
digitalWrite(digitSelect2Pin, 0);
updateShiftRegister2();
digitalWrite(digitSelect2Pin, 1);
digitalWrite(digitSelect3Pin, 0);
updateShiftRegister3();
digitalWrite(digitSelect3Pin, 1);
digitalWrite(digitSelect4Pin, 0);
updateShiftRegister4();
digitalWrite(digitSelect4Pin, 1);
} //w for loop end
}//display end
/*
* updateShiftRegister() - This function sets the latchPin to low, then calls the Arduino function
'shiftOut' to shift out contents of variable 'ledsx' in the shift register before putting the
'latchPin' high again.
*/
void updateShiftRegister1() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST,leds1);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 0);
digitalWrite(latchPin, HIGH);
}
void updateShiftRegister2() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds2);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 0);
digitalWrite(latchPin, HIGH);
}
void updateShiftRegister3() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds3);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 0);
digitalWrite(latchPin, HIGH);
}
void updateShiftRegister4() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds4);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 0);
digitalWrite(latchPin, HIGH);
}
ISR(TIMER4_COMPA_vect) { //timer 4 interrupt servo motor driver
ICR4=190; //frequency 790
h=h+1;
g=g+1;
if(h>20){ //pulse with 1
OCR4A =25;//38
h=0;
}//end if h
if(g>40){ //pulse with2
OCR4A =20;//8
//OCR5A = 12;
g=0;
h=0;
}//end ifg
}//end isr
void convert()
{
//brakes down the TCNT5 counter value into its ones ten huudres and thousands parts
// to sent to the shift register for displaying.
int t=0;
t=a;
d=t*.001;
//c=b*.1;
n4=d;
Serial.print(n4);
//Serial.println(a);
t=t-d*1000;
c=t*.01;
n3=c;
Serial.print(n3);
t=t-c*100;
b=t*.1;
n2=b;
Serial.print(n2);
t=t-b*10;
n1=t;
Serial.println(n1);
}//end convert