Components and supplies
Breadboard (generic)
Pushbutton switch 12mm
Resistor 10k ohm
USB-A to B Cable
Arduino UNO
Jumper wires (generic)
Apps and platforms
Arduino IDE
Project description
Code
Fun Stopwatch
c_cpp
Seven segment display stopwatch
1//STOPWATCH by Arduino Makers 2int nummin=0; 3int numsec=0; 4int numdigit[3] = { 0,0,0 }; 5int digit = 0; 6int tick = 0; 7int stopwatch = 0; // 0:stop(reset), 1:go, 2:pause 8int stateflag = 0; 9int btn = LOW; 10int btntick = 0; 11int startflag = 1; 12 13void setup() { 14 15 pinMode(2,OUTPUT); 16 pinMode(3,OUTPUT); 17 pinMode(4,OUTPUT); 18 pinMode(5,OUTPUT); 19 pinMode(6,OUTPUT); 20 pinMode(7,OUTPUT); 21 pinMode(8,OUTPUT); 22 pinMode(9,OUTPUT); 23 pinMode(10,INPUT); 24 pinMode(11,OUTPUT); 25 pinMode(12,OUTPUT); 26 pinMode(13,OUTPUT); 27 digitalWrite(11,LOW); 28 digitalWrite(12,LOW); 29 digitalWrite(13,LOW); 30 31} 32 33void displayStart() 34{ 35 int data[7][2] = { {10,0},{10,1},{10,2},{3,2},{2,1},{1,0},{10,1} }; 36 37 for(int i=0;i<7;i++){ 38 displayNumber(data[i][0],data[i][1]); 39 delay(500); 40 } 41 42 startflag=0; 43} 44 45void loop() 46{ 47 // put your main code here, to run repeatedly: 48 49 if(startflag==1){ 50 displayStart(); 51 } 52 53 if(btntick==0){ 54 btn = digitalRead(10); 55 if(btn==HIGH){ 56 displayNumber(10,1); 57 delay(1000); 58 if(stopwatch==0) stopwatch=1; 59 else if(stopwatch==1) stopwatch=2; 60 else if(stopwatch==2) stopwatch=0; 61 } 62 } 63 btntick++; 64 if(btntick>=20){ 65 btntick=0; 66 } 67 68 displayNumber(numdigit[digit],digit); 69 delay(5); 70 71 digit++; 72 if(digit>2) digit=0; 73 74 if(stopwatch==1){ 75 tick++; 76 if(tick>=200){ 77 tick=0; 78 numsec++; 79 if(numsec>=60){ 80 numsec=0; 81 nummin++; 82 if(nummin>9){ 83 nummin=0; 84 numsec=0; 85 stopwatch=0; 86 } 87 } 88 divideTimeTo3Digit(nummin,numsec); 89 } 90 }else if(stopwatch==2){ 91 stateflag=1; 92 }else if(stopwatch==0){ 93 if(stateflag==1){ 94 nummin=0; 95 numsec=0; 96 divideTimeTo3Digit(nummin,numsec); 97 stateflag=0; 98 } 99 } 100} 101 102void divideTimeTo3Digit(int m,int s) 103{ 104 numdigit[2] = s%10; 105 numdigit[1] = s/10; 106 numdigit[0] = m; 107} 108 109void divideNumberTo3Digit(int n) 110{ 111 numdigit[2] = n % 10; 112 numdigit[1] = (n / 10) % 10 ; 113 numdigit[0] = n / 100; 114} 115 116void displayNumber(int n,int idx) 117{ 118 turnOnDigit(idx); 119 displayCharacter(n); 120} 121 122void turnOnDigit(int idx) 123{ 124 switch(idx){ 125 case 0 : digitalWrite(11,LOW); 126 digitalWrite(12,HIGH); 127 digitalWrite(13,HIGH); 128 break; 129 case 1 : digitalWrite(11,HIGH); 130 digitalWrite(12,LOW); 131 digitalWrite(13,HIGH); 132 break; 133 case 2 : digitalWrite(11,HIGH); 134 digitalWrite(12,HIGH); 135 digitalWrite(13,LOW); 136 break; 137 } 138} 139 140void displayCharacter(int n) 141{ 142 if((n<0)||(n>10)) return; 143 144 int chartable[11][7] = { { HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,LOW }, //0 145 { LOW ,HIGH,HIGH,LOW ,LOW ,LOW ,LOW }, //1 146 { HIGH,HIGH,LOW ,HIGH,HIGH,LOW ,HIGH }, //2 147 { HIGH,HIGH,HIGH,HIGH,LOW ,LOW ,HIGH }, //3 148 { LOW ,HIGH,HIGH,LOW ,LOW ,HIGH,HIGH }, //4 149 { HIGH,LOW ,HIGH,HIGH,LOW ,HIGH,HIGH }, //5 150 { HIGH,LOW ,HIGH,HIGH,HIGH,HIGH,HIGH }, //6 151 { HIGH,HIGH,HIGH,LOW ,LOW ,LOW ,LOW }, //7 152 { HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH }, //8 153 { HIGH,HIGH,HIGH,HIGH,LOW ,HIGH,HIGH }, //9 154 { LOW ,LOW, LOW, LOW, LOW ,LOW ,HIGH } }; //- 155 156 for(int i=0;i<7;i++){ 157 digitalWrite(2+i,chartable[n][i]); 158 } 159} 160 161 162
Downloadable files
20160802_145930.jpg
20160802_145930.jpg
20160802_145930.jpg
20160802_145930.jpg
Comments
Only logged in users can leave comments
Anonymous user
6 years ago
Nice project!