Timer
Timer
name: Stopwatch
function: You can see the number increases by one per second on the 4-digit 7-
segment display.
***********************************/
// Email: [email protected]
// Website: www.sunfounder.com
/**************************************/
#include <TimerOne.h>
// The pins of the 4-digit 7-segment display attach to pins 2-13 respectively
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int p = 9;
int d4 = 10;
int d3 = 11;
int d2 = 12;
int d1 = 13;
long n = 0; // n represents the value displayed on the LED display. Maximum value
is 9999.
int x = 100;
int del = 5; // Set del as 5; this is the degree of fine-tuning for the clock
int count = 0; // Set count=0. This count increases by 1 every 0.1 second
void setup() {
// Set all the pins of the LED display as output
pinMode(d1, OUTPUT);
pinMode(d2, OUTPUT);
pinMode(d3, OUTPUT);
pinMode(d4, OUTPUT);
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
pinMode(p, OUTPUT);
void loop() {
clearLEDs(); // Clear the 7-segment display screen
pickDigit(0); // Light up 7-segment display d1
pickNumber((n / 1000)); // Get the value of thousand
delay(del); // Delay for 'del' milliseconds
switch (x) {
case 0:
digitalWrite(d1, LOW); // Light d1 up
break;
case 1:
digitalWrite(d2, LOW); // Light d2 up
break;
case 2:
digitalWrite(d3, LOW); // Light d3 up
break;
default:
digitalWrite(d4, LOW); // Light d4 up
break;
}
}
// The function to control the 7-segment LED display to show numbers (0-9)
void pickNumber(int x) {
switch (x) {
default: zero(); break;
case 1: one(); break;
case 2: two(); break;
case 3: three(); break;
case 4: four(); break;
case 5: five(); break;
case 6: six(); break;
case 7: seven(); break;
case 8: eight(); break;
case 9: nine(); break;
}
}
/*******************************************/
void add() {
count++;
if (count ==10) {
count =0;
n++;
if (n ==10000) {
n=9999;
}
}
}