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
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
}



