Simple timer

updated: photos below (Apr. 3, 2011)

I started making a simple timer using the JeonLab mini, 2 shift registers (maybe one will be enough) and 4 digit 7-segment LED as shown below. There are buttons and buzzer are not placed and no wires connected yet. I like the size

4 digit 7-segment LED with JeonLab mini

updated photos (Apr 3, 2011)
Simple timer running

Simple timer with FTDI breakout brd

JeonLab mini right angle header for FTDI

Sketch (Simple_timer.pde)

/*
 Simple timer with JeonLab mini v1.0

 This is a simple timer with a button to start, a pot resistor
 to adjust the time to count down up to 10 minutes, and a buzzer
 to alarm using the JeonLab mini and a shift resistor 74HC585 and
 4 digit 7-segment LED.

 Author: Jinseok Jeon (http://jeonlab.wordpress.com/)
 Date: Mar 29, 2011

*/

byte dataPin = 9;  // connect to the data pin of 74HC595
byte latchPin = 10;  // connect to the latch pin of 74HC595
byte clockPin = 11;  // connect to the clock pin of 74HC595
byte LEDpin = 13;  // LED to turn on with alarm
byte triggerPin = 12;  // button to start
byte alarmPin = 4;  // connect to the buzzer
byte potPin = 5;  // pot resistor input
byte buttonState = 0;  // check if the button is pressed

byte digit[4] = {5,6,7,8};  // cathode pins of each digit of 7-segment
byte ms[4];  // number to show on each digit
byte numberArray[11] = {215,20,205,93,30,91,219,21,223,31,2};
 // 0-9 decimal data of 7-segment
unsigned long time;  // variable to check 1 second to count down

void setup() {
 pinMode(dataPin, OUTPUT);
 pinMode(latchPin, OUTPUT);
 pinMode(clockPin, OUTPUT);
 for (int i=0; i<=3; i++) {
 pinMode(digit[i], OUTPUT);
 digitalWrite(digit[i], HIGH);
 }
 pinMode(12, INPUT);  // Start button switch input
 pinMode(alarmPin, OUTPUT);  // buzzer
 setTime();
}

void loop() {
// keep showing current count for 1 second
 do {
 time = millis();
 LEDshow();
 } while (millis()/1000 <= time/1000);

// calculate each digit every second
 if (ms[3] == 0) {
 ms[3] = 9;
 if (ms[2] == 0) {
 ms[2] = 5;
 ms[0]--;
 }
 else {
 ms[2]--;
 }
 }
 else {
 ms[3]--;
 }

// alarm goes off with pin #13 LED on for 1 second
 if (ms[0] == 0 && ms[2] == 0 && ms[3] == 0) {
 digitalWrite(alarmPin, HIGH); // buzzer on
 digitalWrite(LEDpin, HIGH);
 delay(1000);
 digitalWrite(alarmPin, LOW); // buzzer off with the button pressed
 digitalWrite(LEDpin, LOW);
 setTime();
 }
}

// multiplexing 4 digits of 7-segment LED
void LEDshow() {
 for (int i = 0; i <= 3; i++) {
 digitalWrite(latchPin, LOW);
 shiftOut(dataPin, clockPin, MSBFIRST, numberArray[ms[i]]);
 digitalWrite(latchPin, HIGH);
 digitalWrite(digit[i], LOW);
 delay(2);
 digitalWrite(digit[i], HIGH);
 }
}

// setting timer with a pot resistor
//    in order for accurate setting, using 2 digital pins will
//    be better for up/down control
void setTime() {
 buttonState = 0;
 float potRead;
 do {
 if (digitalRead(triggerPin) == HIGH) buttonState++;
 potRead = analogRead(potPin)/1024.0 * 10.0;
 ms[0] = int(potRead);  //minute, 0 to 9
 ms[1]=10;
 ms[2] = int((potRead-ms[0]) * 6);  //ten second
 ms[3] = int((potRead-ms[0]) * 60) - ms[2]*10;  //second
 LEDshow();
 } while (!(buttonState > 0 && digitalRead(triggerPin) == LOW)
 && (ms[0] == 0 && ms[2] == 0 && ms[3] == 0)); // press button to set time
}
About these ads
About

Opto-mechatronic Engineering scientist.

Tagged with:
Posted in JeonLab mini

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

March 2011
S M T W T F S
« Feb   Apr »
 12345
6789101112
13141516171819
20212223242526
2728293031  
Archives

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 8 other followers

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: