Components and supplies
Buzzer
Pushbutton Switch, Pushbutton
5 mm LED: Green
Arduino UNO
Alphanumeric LCD, 16 x 2
Resistor 330 ohm
5 mm LED: Red
Jumper wires (generic)
Single Turn Potentiometer- 10k ohms
Apps and platforms
Arduino IDE
Project description
Code
Code
arduino
1#include <LiquidCrystal.h> //Libraries 2LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Arduino pins to lcd 3 4#define bt_start A0 5#define bt_stop A1 6#define bt_reset A2 7 8#define G_led 8 9#define R_led 9 10 11#define buzzer 13 12 13int hh=0, mm=0, ss=0, ms=0; 14bool timerStart = false; 15 16void setup() { // put your setup code here, to run once 17 18pinMode(bt_start, INPUT_PULLUP); 19pinMode(bt_stop, INPUT_PULLUP); 20pinMode(bt_reset, INPUT_PULLUP); 21 22pinMode(R_led,OUTPUT); // declare Red LED as output 23pinMode(G_led,OUTPUT); // declare Green LED as output 24pinMode(buzzer,OUTPUT); // declare Buzzer as output 25 26lcd.begin(16, 2); // Configura lcd numero columnas y filas 27lcd.clear(); 28lcd.setCursor (0,0); 29lcd.print(" Welcome To "); 30lcd.setCursor (0,1); 31lcd.print(" Stopwatch "); 32delay(2000); 33lcd.clear(); 34 35 36 noInterrupts(); // disable all interrupts 37 TCCR1A = 0; // set entire TCCR1A register to 0 //set timer1 interrupt at 1kHz // 1 ms 38 TCCR1B = 0; // same for TCCR1B 39 TCNT1 = 0; // set timer count for 1khz increments 40 OCR1A = 1999; // = (16*10^6) / (1000*8) - 1 41 //had to use 16 bit timer1 for this bc 1999>255, but could switch to timers 0 or 2 with larger prescaler 42 // turn on CTC mode 43 TCCR1B |= (1 << WGM12); // Set CS11 bit for 8 prescaler 44 TCCR1B |= (1 << CS11); // enable timer compare interrupt 45 TIMSK1 |= (1 << OCIE1A); 46 interrupts(); // enable 47 48} 49 50void loop() { 51 52if(digitalRead (bt_start) == 0){ 53digitalWrite(buzzer, HIGH); 54timerStart = true; // Start stopwatch 55delay(100); 56} 57 58if(digitalRead (bt_stop) == 0){ 59digitalWrite(buzzer, HIGH); 60timerStart = false; // Stop stopwatch 61delay(100); 62} 63 64if(digitalRead (bt_reset) == 0){ 65digitalWrite(buzzer, HIGH); 66ms=0, ss=0, mm=0, hh=0; // Reset stopwatch 67delay(100); 68} 69 70lcd.setCursor (0,0); 71lcd.print(" Stopwatch "); 72 73lcd.setCursor (2,1); 74lcd.print((hh/10)%10); 75lcd.print(hh%10); 76lcd.print(":"); 77lcd.print((mm/10)%10); 78lcd.print(mm%10); 79lcd.print(":"); 80lcd.print((ss/10)%10); 81lcd.print(ss%10); 82lcd.print(":"); 83lcd.print((ms/100)%10); 84lcd.print((ms/10)%10); 85lcd.print(ms%10); 86 87if(timerStart==true){ 88digitalWrite(G_led, HIGH); // Turn LED on. 89digitalWrite(R_led, LOW); // Turn LED off. 90}else{ 91digitalWrite(G_led, LOW); // Turn LED off. 92digitalWrite(R_led, HIGH); // Turn LED on. 93} 94 95digitalWrite(buzzer, LOW); 96} 97 98 99ISR(TIMER1_COMPA_vect){ 100if(timerStart == true){ms=ms+1; 101if(ms>999){ms=0;ss=ss+1; 102if(ss>59){ss=0; mm=mm+1;} 103if(mm>59){mm=0; hh=hh+1;} 104 } 105 } 106} 107
Code
arduino
1#include <LiquidCrystal.h> //Libraries 2LiquidCrystal lcd(2, 3, 4, 5, 3 6, 7); //Arduino pins to lcd 4 5#define bt_start A0 6#define bt_stop A1 7#define 8 bt_reset A2 9 10#define G_led 8 11#define R_led 9 12 13#define buzzer 13 14 15int 16 hh=0, mm=0, ss=0, ms=0; 17bool timerStart = false; 18 19void setup() { // put 20 your setup code here, to run once 21 22pinMode(bt_start, INPUT_PULLUP); 23pinMode(bt_stop, 24 INPUT_PULLUP); 25pinMode(bt_reset, INPUT_PULLUP); 26 27pinMode(R_led,OUTPUT); 28 // declare Red LED as output 29pinMode(G_led,OUTPUT); // declare Green LED as output 30pinMode(buzzer,OUTPUT); 31 // declare Buzzer as output 32 33lcd.begin(16, 2); // Configura lcd numero columnas 34 y filas 35lcd.clear(); 36lcd.setCursor (0,0); 37lcd.print(" Welcome To "); 38lcd.setCursor 39 (0,1); 40lcd.print(" Stopwatch "); 41delay(2000); 42lcd.clear(); 43 44 45 46 noInterrupts(); // disable all interrupts 47 TCCR1A = 0; // 48 set entire TCCR1A register to 0 //set timer1 interrupt at 1kHz // 1 ms 49 TCCR1B 50 = 0; // same for TCCR1B 51 TCNT1 = 0; // set timer count 52 for 1khz increments 53 OCR1A = 1999; // = (16*10^6) / (1000*8) - 1 54 55 //had to use 16 bit timer1 for this bc 1999>255, but could switch to timers 0 or 56 2 with larger prescaler 57 // turn on CTC mode 58 TCCR1B |= (1 << WGM12); // Set 59 CS11 bit for 8 prescaler 60 TCCR1B |= (1 << CS11); // enable timer compare interrupt 61 62 TIMSK1 |= (1 << OCIE1A); 63 interrupts(); // enable 64 65} 66 67void 68 loop() { 69 70if(digitalRead (bt_start) == 0){ 71digitalWrite(buzzer, HIGH); 72 73timerStart = true; // Start stopwatch 74delay(100); 75} 76 77if(digitalRead 78 (bt_stop) == 0){ 79digitalWrite(buzzer, HIGH); 80timerStart = false; // Stop 81 stopwatch 82delay(100); 83} 84 85if(digitalRead (bt_reset) == 0){ 86digitalWrite(buzzer, 87 HIGH); 88ms=0, ss=0, mm=0, hh=0; // Reset stopwatch 89delay(100); 90} 91 92lcd.setCursor 93 (0,0); 94lcd.print(" Stopwatch "); 95 96lcd.setCursor (2,1); 97lcd.print((hh/10)%10); 98lcd.print(hh%10); 99lcd.print(":"); 100lcd.print((mm/10)%10); 101lcd.print(mm%10); 102lcd.print(":"); 103lcd.print((ss/10)%10); 104lcd.print(ss%10); 105lcd.print(":"); 106lcd.print((ms/100)%10); 107lcd.print((ms/10)%10); 108lcd.print(ms%10); 109 110if(timerStart==true){ 111digitalWrite(G_led, 112 HIGH); // Turn LED on. 113digitalWrite(R_led, LOW); // Turn LED off. 114}else{ 115digitalWrite(G_led, 116 LOW); // Turn LED off. 117digitalWrite(R_led, HIGH); // Turn LED on. 118} 119 120 121digitalWrite(buzzer, LOW); 122} 123 124 125ISR(TIMER1_COMPA_vect){ 126if(timerStart 127 == true){ms=ms+1; 128if(ms>999){ms=0;ss=ss+1; 129if(ss>59){ss=0; mm=mm+1;} 130if(mm>59){mm=0; 131 hh=hh+1;} 132 } 133 } 134} 135
Downloadable files
Circuit Diagram
Circuit Diagram
Comments
Only logged in users can leave comments
deathbytube
2 years ago
This is awesome and works great thank you. but i did have a question for you What would I have to change in the code so that i could use Timer 0 or 2. You mention using a larger prescaler but I have not figured out how to do it yet below is what I've been using but the clock is to fast. Thank you in advance noInterrupts(); // disable all interrupts TCCR2A = 0; // set entire TCCR1A register to 0 //set timer1 interrupt at 1kHz // 1 ms TCCR2B = 0; // same for TCCR1B TCNT2 = 0; // set timer count for 1khz increments OCR2A = 512; // = (16*10^6) / (1000*8) - 1 //had to use 16 bit timer1 for this bc 1999>255, but could switch to timers 0 or 2 with larger prescaler // turn on CTC mode TCCR2B |= (1 << WGM21); // Set CS11 bit for 8 prescaler TCCR2B |= (1 << CS21); // enable timer compare interrupt TIMSK2 |= (1 << OCIE1A); interrupts(); // enable