Components and supplies
Breadboard (generic)
Tactile Switch, Top Actuated
Arduino UNO
Rotary potentiometer (generic)
Alphanumeric LCD, 16 x 2
Jumper wires (generic)
Project description
Code
Code
arduino
1#include <LiquidCrystal.h> 2//PIN 5 R/W to Ground, for writing 3//LCD Mode : 4 data pin 4//Functions: Start, stop, saving up to 4 partial time in memory. 5//10 millis = 1 hundredth of a second. The chronometer measures hour, minutes, seconds, and hundredths of a second. 6 7//Buttons with internal Pullup, so if they get pressed we get LOW 8const int start = 8; //Start the stopwatch 9const int pausa = 9; //Pausa 10const int partial = 10; //Save a partial 11const int scroll_partial = 11; //If paused, check the 4 last saved partial 12int x = 0; //Variable to manage the loop 13 14//LCD 15int lcdclear = 0; //this to variables are needed for managing 16int Display = 0; //the display, to clear and print 17 18//chronometer 19int cents = 0; 20int seconds = 0; 21int minutes = 0; 22int hours = 0; 23const int interval = 10; //Every 10 milliseconds i increase 1 cent 24unsigned long previousMillis = 0; 25int c1, c2, s1, s2, m1, m2, h; //Variables used to put in the form 26 //h:m2m1:s2s1:c2c1 27 28//Partial: I save 4 partial, that can be seen if stopwatch is stopped 29int partial2[7]; //penultimate partial (The one that stays in Old). The last partial stays in New: 30int partial3[7]; 31int partial4[7]; 32int scrolling = 0; //Used to scroll the saved partial times 33 34 35LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // RS-Enable-D4-D5-D6-D7 in that digitalPin 36 37void setup() { 38 pinMode(start, INPUT_PULLUP); //In the schematic from right to left 39 pinMode(pausa, INPUT_PULLUP);//there are start-pausa-partial-scroll_partial 40 pinMode(partial, INPUT_PULLUP); 41 pinMode(scroll_partial, INPUT_PULLUP); 42 lcd.begin(16,2); 43 lcd.print("Press start"); 44 45} 46 47void loop() { 48 if (x == 0) { //Initially is 0 49 while(digitalRead(start) == HIGH) {}; //Until i press the button, the chronometer doesn't start 50 x++; //When i press the button, i go out from this cycle, x++ and i cannot return here anymore 51 } 52 if (lcdclear == 0){ //Condition to clear the display, used in the various functions 53 lcd.clear(); 54 lcdclear++; 55 } 56 if (Display == 0){ //Also this is used to clear display 57 lcd.home(); 58 lcd.print("Now: "); //With this condition, i can print "Now: " one single time, otherwise the chronometer wouldn't be precise 59 Display++; 60 scrolling = 0; //When i exit from the partial menu, then if i go in the partial menu again i always get in PAR1 and PAR2 61 } 62 chronometer(); //At the end of every increase, i control if stop is pressed. And also if the partial function is pressed 63 pause(); 64 f_partial(); 65} 66 67void chronometer(void){ //This function print: "New: Actual time" 68 unsigned long currentMillis = millis(); //If for the updating. If it is true, it means 1 cent of a second had passed. Update cents, minutes, seconds, hours and then i write on the lcd 69 if (currentMillis - previousMillis >= interval) { 70 previousMillis = currentMillis; 71 cents++; 72 if (cents == 100){ 73 cents = 0; 74 seconds++; 75 if (seconds == 60) { 76 seconds = 0; 77 minutes++; 78 if (minutes == 60){ 79 minutes = 0; 80 hours++; 81 if (hours == 24) 82 hours = 0; 83 } 84 } 85 } 86 int cent = cents; 87 int sec = seconds; 88 int minu = minutes; //Taking the digits separeted 89 h = hours; //For the other funcionts, so i can put hours = 0 and h still is the last value 90 c1 = cent%10; 91 cent /= 10; 92 c2 = cent%10; 93 s1 = sec%10; 94 sec /= 10; 95 s2 = sec%10; 96 m1 = minu%10; 97 minu /= 10; 98 m2 = minu%10; 99 lcd.setCursor(6, 0); 100 lcd.print(h); 101 lcd.print(':'); 102 lcd.print(m2); 103 lcd.print(m1); 104 lcd.print(':'); 105 lcd.print(s2); 106 lcd.print(s1); 107 lcd.print(':'); 108 lcd.print(c2); 109 lcd.print(c1); 110 } 111} 112 113void scrollPartial(void){ 114 while(digitalRead(scroll_partial) == LOW) {}; //Debounce, as long as i press the button the real function doesn't start 115 if (scrolling == 0) { //Visualize the last 2 partials 116 lcd.clear(); 117 lcd.home(); 118 lcd.print("PAR1:"); 119 lcd.setCursor(6, 0); 120 lcd.print(h); 121 lcd.print(':'); 122 lcd.print(m2); 123 lcd.print(m1); 124 lcd.print(':'); 125 lcd.print(s2); 126 lcd.print(s1); 127 lcd.print(':'); 128 lcd.print(c2); 129 lcd.print(c1); 130 lcd.setCursor(0, 1); 131 lcd.print("PAR2:"); 132 lcd.setCursor(6, 1); 133 lcd.print(partial2[0]); 134 lcd.print(':'); 135 lcd.print(partial2[1]); 136 lcd.print(partial2[2]); 137 lcd.print(':'); 138 lcd.print(partial2[3]); 139 lcd.print(partial2[4]); 140 lcd.print(':'); 141 lcd.print(partial2[5]); 142 lcd.print(partial2[6]); 143 Display = 0; //When i press start the display must be cleared 144 lcdclear = 0; //When i press start the display must be cleared 145 cents = seconds = minutes = hours = 0; 146 scrolling++; 147 } 148 else if (scrolling == 1){ //Visualize 3th and 4th partial 149 lcd.clear(); 150 lcd.home(); 151 lcd.print("PAR3:"); 152 lcd.setCursor(6, 0); 153 lcd.print(partial3[0]); 154 lcd.print(':'); 155 lcd.print(partial3[1]); 156 lcd.print(partial3[2]); 157 lcd.print(':'); 158 lcd.print(partial3[3]); 159 lcd.print(partial3[4]); 160 lcd.print(':'); 161 lcd.print(partial3[5]); 162 lcd.print(partial3[6]); 163 lcd.setCursor(0, 1); 164 lcd.print("PAR4:"); 165 lcd.setCursor(6, 1); 166 lcd.print(partial4[0]); 167 lcd.print(':'); 168 lcd.print(partial4[1]); 169 lcd.print(partial4[2]); 170 lcd.print(':'); 171 lcd.print(partial4[3]); 172 lcd.print(partial4[4]); 173 lcd.print(':'); 174 lcd.print(partial4[5]); 175 lcd.print(partial4[6]); 176 Display = 0; //When i press start the display must be cleared 177 lcdclear = 0; //When i press start the display must be cleared 178 cents = seconds = minutes = hours = 0; 179 scrolling = 0; 180 181 } 182 183} 184 185 186void pause(void){ //If pause is pressed, i stop in this function until start doesn't get pressed again 187 if (digitalRead(pausa) == HIGH) 188 return; 189 else if (digitalRead(pausa) == LOW){ //Stuck in this cycle until i press start 190 while(digitalRead(start) == HIGH) { 191 if (digitalRead(scroll_partial) == LOW) //If i press the button for seeing the partial, i enter in that function 192 scrollPartial(); //When scrollPartial() ends, i'm still in this function, so if i press start the chronometer starts back normal 193 } 194 } 195} 196 197void f_partial(void){ //If this button is pressed, i put the current value of New in Old, and a new crhonometer starts 198 if (digitalRead(partial) == HIGH) 199 return; 200 else if (digitalRead(partial) == LOW ){ 201 lcd.clear(); 202 lcd.setCursor(0, 1); //The values calculated in the function chronometer can be used, h,m,s,c 203 lcd.print("Old: "); 204 lcd.setCursor(6, 1); 205 lcd.print(h); 206 lcd.print(':'); 207 lcd.print(m2); 208 lcd.print(m1); 209 lcd.print(':'); 210 lcd.print(s2); 211 lcd.print(s1); 212 lcd.print(':'); 213 lcd.print(c2); 214 lcd.print(c1); //When i come here, i've got the old values for h,m,s,c, i save it in the partial array 215 Display = 0; //The new is written again 216 cents = 0; 217 seconds = 0; 218 minutes = 0; 219 hours = 0; 220 221 partial4[0] = partial3[0]; //Partial4[] is updated with the old partial3[] 222 partial4[1] = partial3[1]; 223 partial4[2] = partial3[2]; 224 partial4[3] = partial3[3]; 225 partial4[4] = partial3[4]; 226 partial4[5] = partial3[5]; 227 partial4[6] = partial3[6]; 228 229 230 partial3[0] = partial2[0]; //Partial3[] is updated with the old partial2[] 231 partial3[1] = partial2[1]; 232 partial3[2] = partial2[2]; 233 partial3[3] = partial2[3]; 234 partial3[4] = partial2[4]; 235 partial3[5] = partial2[5]; 236 partial3[6] = partial2[6]; 237 238 partial2[0] = h; //Update partial2 with OLD 239 partial2[1] = m2; 240 partial2[2] = m1; 241 partial2[3] = s2; 242 partial2[4] = s1; 243 partial2[5] = c2; 244 partial2[6] = c1; 245 246 while(digitalRead(partial) == LOW) {}; //Debounce, until i press the button i stay here 247 } 248}
Downloadable files
Breadboard schematic
Breadboard schematic
Breadboard schematic
Breadboard schematic
Comments
Only logged in users can leave comments