Arduino Project 1

After connecting the display directly to my Arduino, which I wouldn’t recommend unless you’re really careful, I connected it up in a much more efficient way using a 74HC595 shift register and several transistors. The code I used to make it countdown is provided below.

/*
7 seg display countdown timer using shift register
*/

//Pin connected to ST_CP of 74HC595
const int latchPin = 11;
//Pin connected to SH_CP of 74HC595
const int clockPin = 10;
//Pin connected to DS of 74HC595
const int dataPin = 12;
//Pins connected to anodes
const int digit1 = 9;
const int digit2 = 8;
const int digit3 = 6;
const int digit4 = 5;
const int digits[4] = {digit1, digit2, digit3, digit4}; //for ease of resetting etc
const int colon = 7;

signed long timer = 10;//1980; //3:30
unsigned long startTime;
int doBlink = 0; //flash off first
byte numbers[11] =
{ //A-Gcolon
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110, // 9
B00000010  // -
};

void setup() {
//Shift register pins
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
//Anodes

for(int i=0;i<4;i++)   {     pinMode(digits[i], OUTPUT);     digitalWrite(digits[i], LOW);   }   //Colon   pinMode(colon, OUTPUT);   digitalWrite(colon,HIGH);      //convert to milliseconds   timer = (timer*1000);    startTime = millis(); //program start time  } void loop() {   unsigned long timeElapsed = millis()-startTime; //time elapsed since program start   signed long timerMilli = timer;                 //countdown time in ms   timerMilli = timerMilli-timeElapsed;            //current countdown time in ms   if(timerMilli>0) //show mm:ss
{
for (unsigned long elapsed=0; elapsed < 100; elapsed = millis() - startTime - timeElapsed) {
lightDigit(digit1,((timerMilli/1000)/60)/10,1);
delay(5);
lightDigit(digit2,((timerMilli/1000)/60)%10,1);
delay(5);
lightDigit(digit3,((timerMilli/1000)%60)/10,1);
delay(5);
lightDigit(digit4,((timerMilli/1000)%60)%10,1);
delay(5);
}
}
else // blink 00:00
{
if(doBlink)
{
for (unsigned long elapsed=0; elapsed < 800; elapsed = millis() - startTime - timeElapsed) {
digitalWrite(colon, HIGH);
lightDigit(digit1,0,1);
delay(5);
lightDigit(digit2,0,1);
delay(5);
lightDigit(digit3,0,1);
delay(5);
lightDigit(digit4,0,1);
delay(5);
}
doBlink=0;
}
else
{
for (unsigned long elapsed=0; elapsed < 200; elapsed = millis() - startTime - timeElapsed) {
digitalWrite(colon, LOW);
digitalWrite(digit1, LOW);
digitalWrite(digit2, LOW);
digitalWrite(digit3, LOW);
digitalWrite(digit4, LOW);
}
doBlink=1;
}
}
}

void lightDigit(int digit, int number, int colon) {
for(int i=0;i<4;i++)
{
digitalWrite(digits[i], LOW);
}
byte bitsToSend = numbers[number];
if(colon)
{
bitsToSend = bitsToSend ^ B11111110;
}
else
{
bitsToSend = bitsToSend ^ B11111111;
}
// take the latchPin low so
// the LEDs don't change while you're sending in bits:
digitalWrite(latchPin, LOW);
// shift out the bits:
shiftOut(dataPin, clockPin, LSBFIRST, bitsToSend);
//take the latch pin high so
digitalWrite(latchPin, HIGH);
digitalWrite(digit, HIGH);
}
posted in Arduino