Components and supplies
Pushbutton switch 12mm
Jumper wires (generic)
Arduino Nano R3
Breadboard (generic)
Resistor 10k ohm
Resistor 221 ohm
Standard LCD - 16x2 White on Blue
Project description
Code
Code
arduino
This is the code for my stopwatch. Just copy it in Arduino IDE and upload it on your Arduino.
1/*Simple LCD stopwatch program with stop, start, reset and lap buttons.*/ 2 3//including liblary for LCD 4#include <LiquidCrystal.h> 5 6//setting up LCD INPUT pins 7LiquidCrystal lcd(12,11,5,4,3,2); 8 9//setting hours, minutes, secound and miliseconds to 0 10int h=0; 11int m=0; 12int s=0; 13int ms=0; 14 15//defines pin for all buttons 16const int start_pin = 8; 17const int stop1_pin = 9; 18const int reset_pin = 10; 19 20//defines starting points (in my case 0) 21int start=0; 22int stop1=0; 23int reset=0; 24 25 26int brightness_pin = 6; //defines pin for setting brightness 27int brightness=100; //you can change this number to change brightness 28 29void setup() 30{ 31 32 analogWrite(brightness_pin ,brightness); //this sets brightness on pin 6 33 lcd.begin(16 ,2); //starting LCD 34 35 //defining pins if they are INPUT or OUTPUT pins 36 pinMode(start_pin, INPUT); 37 pinMode(stop1_pin, INPUT); 38 pinMode(reset_pin, INPUT); 39 pinMode(brightness_pin, OUTPUT); 40} 41void loop() 42{ 43 lcd.setCursor(0,1); 44 lcd.print("STOPWATCH"); 45 lcd.setCursor(0,0); 46 lcd.print("TIME:"); 47 lcd.print(h); 48 lcd.print(":"); 49 lcd.print(m); 50 lcd.print(":"); 51 lcd.print(s); 52 53 start = digitalRead(start_pin); //reading buton state 54 if(start == HIGH) 55 { 56 stopwatch(); //goes to sub program stopwatch 57 } 58 59} 60 61 62 63//--------------------SUB PROGRAMS------------------------- 64 65 66 67void stopwatch() 68{ 69 lcd.setCursor(0,0); //setting start point on lcd 70 lcd.print("TIME:"); //writting TIME 71 lcd.print(h); //writing hours 72 lcd.print(":"); 73 lcd.print(m); //writing minutes 74 lcd.print(":"); 75 lcd.print(s); //writing seconds 76 ms=ms+10; 77 delay(10); 78 79 if(ms==590) 80 { 81 lcd.clear(); //clears LCD 82 } 83 84 if(ms==590) //if state for counting up seconds 85 { 86 ms=0; 87 s=s+1; 88 } 89 90 if(s==60) //if state for counting up minutes 91 { 92 s=0; 93 m=m+1; 94 } 95 96 if(m==60) //if state for counting up hours 97 { 98 m=00; 99 h=h+01; 100 } 101 102 lcd.setCursor(0,1); 103 lcd.print("STOPWATCH"); 104 105 stop1 = digitalRead(stop1_pin); //reading buton state 106 if(stop1 == HIGH) //checking if button is pressed 107 { 108 stopwatch_stop(); //going to sub program 109 } 110 else 111 { 112 stopwatch(); //going to sub program 113 } 114} 115 116void stopwatch_stop() 117{ 118 lcd.setCursor(0,0); 119 lcd.print("TIME:"); 120 lcd.print(h); 121 lcd.print(":"); 122 lcd.print(m); 123 lcd.print(":"); 124 lcd.print(s); 125 126 lcd.setCursor(0,1); 127 lcd.print("STOPWATCH"); 128 129 start = digitalRead(start_pin); //reading buton state 130 if(start == HIGH) 131 { 132 stopwatch(); //going to sub program 133 } 134 135 reset = digitalRead(reset_pin); //reading buton state 136 if(reset == HIGH) 137 { 138 stopwatch_reset(); //going to sub program 139 loop(); 140 } 141 if(reset == LOW) 142 { 143 stopwatch_stop(); //going to sub program 144 } 145} 146 147void stopwatch_reset() 148{ 149 lcd.clear(); 150 lcd.setCursor(0,1); 151 lcd.print("STOPWATCH"); 152 h=00; //seting hours to 0 153 m=00; //seting minutes to 0 154 s=00; //seting seconds to 0 155 return; //exiting the program and returning to the point where entered the program 156}
Library
This is the library that you need for LCD.
Code
arduino
This is the code for my stopwatch. Just copy it in Arduino IDE and upload it on your Arduino.
1/*Simple LCD stopwatch program with stop, start, reset and lap buttons.*/ 2 3//including liblary for LCD 4#include <LiquidCrystal.h> 5 6//setting up LCD INPUT pins 7LiquidCrystal lcd(12,11,5,4,3,2); 8 9//setting hours, minutes, secound and miliseconds to 0 10int h=0; 11int m=0; 12int s=0; 13int ms=0; 14 15//defines pin for all buttons 16const int start_pin = 8; 17const int stop1_pin = 9; 18const int reset_pin = 10; 19 20//defines starting points (in my case 0) 21int start=0; 22int stop1=0; 23int reset=0; 24 25 26int brightness_pin = 6; //defines pin for setting brightness 27int brightness=100; //you can change this number to change brightness 28 29void setup() 30{ 31 32 analogWrite(brightness_pin ,brightness); //this sets brightness on pin 6 33 lcd.begin(16 ,2); //starting LCD 34 35 //defining pins if they are INPUT or OUTPUT pins 36 pinMode(start_pin, INPUT); 37 pinMode(stop1_pin, INPUT); 38 pinMode(reset_pin, INPUT); 39 pinMode(brightness_pin, OUTPUT); 40} 41void loop() 42{ 43 lcd.setCursor(0,1); 44 lcd.print("STOPWATCH"); 45 lcd.setCursor(0,0); 46 lcd.print("TIME:"); 47 lcd.print(h); 48 lcd.print(":"); 49 lcd.print(m); 50 lcd.print(":"); 51 lcd.print(s); 52 53 start = digitalRead(start_pin); //reading buton state 54 if(start == HIGH) 55 { 56 stopwatch(); //goes to sub program stopwatch 57 } 58 59} 60 61 62 63//--------------------SUB PROGRAMS------------------------- 64 65 66 67void stopwatch() 68{ 69 lcd.setCursor(0,0); //setting start point on lcd 70 lcd.print("TIME:"); //writting TIME 71 lcd.print(h); //writing hours 72 lcd.print(":"); 73 lcd.print(m); //writing minutes 74 lcd.print(":"); 75 lcd.print(s); //writing seconds 76 ms=ms+10; 77 delay(10); 78 79 if(ms==590) 80 { 81 lcd.clear(); //clears LCD 82 } 83 84 if(ms==590) //if state for counting up seconds 85 { 86 ms=0; 87 s=s+1; 88 } 89 90 if(s==60) //if state for counting up minutes 91 { 92 s=0; 93 m=m+1; 94 } 95 96 if(m==60) //if state for counting up hours 97 { 98 m=00; 99 h=h+01; 100 } 101 102 lcd.setCursor(0,1); 103 lcd.print("STOPWATCH"); 104 105 stop1 = digitalRead(stop1_pin); //reading buton state 106 if(stop1 == HIGH) //checking if button is pressed 107 { 108 stopwatch_stop(); //going to sub program 109 } 110 else 111 { 112 stopwatch(); //going to sub program 113 } 114} 115 116void stopwatch_stop() 117{ 118 lcd.setCursor(0,0); 119 lcd.print("TIME:"); 120 lcd.print(h); 121 lcd.print(":"); 122 lcd.print(m); 123 lcd.print(":"); 124 lcd.print(s); 125 126 lcd.setCursor(0,1); 127 lcd.print("STOPWATCH"); 128 129 start = digitalRead(start_pin); //reading buton state 130 if(start == HIGH) 131 { 132 stopwatch(); //going to sub program 133 } 134 135 reset = digitalRead(reset_pin); //reading buton state 136 if(reset == HIGH) 137 { 138 stopwatch_reset(); //going to sub program 139 loop(); 140 } 141 if(reset == LOW) 142 { 143 stopwatch_stop(); //going to sub program 144 } 145} 146 147void stopwatch_reset() 148{ 149 lcd.clear(); 150 lcd.setCursor(0,1); 151 lcd.print("STOPWATCH"); 152 h=00; //seting hours to 0 153 m=00; //seting minutes to 0 154 s=00; //seting seconds to 0 155 return; //exiting the program and returning to the point where entered the program 156}
Library
This is the library that you need for LCD.
Downloadable files
Schematics
This is a Schematics of my circuit.
Schematics
Comments
Only logged in users can leave comments
TechWithZan
0 Followers
•0 Projects
Table of contents
Intro
19
0