Components and supplies
Alphanumeric LCD, 16 x 2
Servo Module (Generic)
Elegoo Ultrasonic Sensor
Solderless Breadboard Full Size
Switch Sealing Boot, Button Operators
Arduino Mega 2560
Apps and platforms
Arduino IDE
Project description
Code
StopwatchSensor.ino
arduino
1#include <LiquidCrystal.h> 2#include <Servo.h> 3 4LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 5Servo releaseGate; 6 7const int distancePingPin = 9; 8const int distanceEchoPin = 10; 9const int buttonPin = 6; 10const int servoPin = 7; 11const int detectionDistance = 10; 12unsigned long startTime; 13unsigned long currentTime; 14bool timerStop = false; 15int closedAngle = 70; 16int openAngle = 170; 17 18void setup() { 19 Serial.begin(9600); 20 21 releaseGate.attach(servoPin); 22 23 pinMode(buttonPin, INPUT); 24 25 lcd.begin(16, 2); 26 startTime = 0; 27 StartClock(); 28} 29 30void loop() 31{ 32 currentTime = millis(); 33 34 int distance = DistanceToObstacle(); 35 36 if (distance <= detectionDistance) 37 { 38 if (timerStop == false) 39 { 40 lcd.clear(); 41 delay(100); 42 float currentT = currentTime; 43 float startT = startTime; 44 if ((currentT - startT) /1000 > 60) 45 { 46 lcd.print(((currentT - startT) /1000) / 60); 47 lcd.print(" Minutes"); 48 } 49 else 50 { 51 lcd.print((currentT - startT) /1000); 52 lcd.print(" Seconds"); 53 } 54 timerStop = true; 55 } 56 } 57 58 if(digitalRead(buttonPin) == HIGH) 59 { 60 StartClock(); 61 } 62} 63 64void StartClock() 65{ 66 releaseGate.write(openAngle); 67 timerStop = false; 68 startTime = millis(); 69 lcd.clear(); 70 delay(100); 71 lcd.print("Clock Started"); 72 delay(500); 73 releaseGate.write(closedAngle); 74} 75 76float DistanceToObstacle() 77{ 78 delay(100); 79 long duration, inches, cm; 80 pinMode(distancePingPin, OUTPUT); 81 digitalWrite(distancePingPin, LOW); 82 delayMicroseconds(2); 83 digitalWrite(distancePingPin, HIGH); 84 delayMicroseconds(10); 85 digitalWrite(distancePingPin, LOW); 86 pinMode(distanceEchoPin, INPUT); 87 duration = pulseIn(distanceEchoPin, HIGH); 88 cm = MicrosecondsToCentimeters(duration); 89 return cm; 90} 91 92long MicrosecondsToCentimeters(long microseconds) { 93 return microseconds / 29 / 2; 94} 95
StopwatchSensor.ino
arduino
1#include <LiquidCrystal.h> 2#include <Servo.h> 3 4LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 5Servo releaseGate; 6 7const int distancePingPin = 9; 8const int distanceEchoPin = 10; 9const int buttonPin = 6; 10const int servoPin = 7; 11const int detectionDistance = 10; 12unsigned long startTime; 13unsigned long currentTime; 14bool timerStop = false; 15int closedAngle = 70; 16int openAngle = 170; 17 18void setup() { 19 Serial.begin(9600); 20 21 releaseGate.attach(servoPin); 22 23 pinMode(buttonPin, INPUT); 24 25 lcd.begin(16, 2); 26 startTime = 0; 27 StartClock(); 28} 29 30void loop() 31{ 32 currentTime = millis(); 33 34 int distance = DistanceToObstacle(); 35 36 if (distance <= detectionDistance) 37 { 38 if (timerStop == false) 39 { 40 lcd.clear(); 41 delay(100); 42 float currentT = currentTime; 43 float startT = startTime; 44 if ((currentT - startT) /1000 > 60) 45 { 46 lcd.print(((currentT - startT) /1000) / 60); 47 lcd.print(" Minutes"); 48 } 49 else 50 { 51 lcd.print((currentT - startT) /1000); 52 lcd.print(" Seconds"); 53 } 54 timerStop = true; 55 } 56 } 57 58 if(digitalRead(buttonPin) == HIGH) 59 { 60 StartClock(); 61 } 62} 63 64void StartClock() 65{ 66 releaseGate.write(openAngle); 67 timerStop = false; 68 startTime = millis(); 69 lcd.clear(); 70 delay(100); 71 lcd.print("Clock Started"); 72 delay(500); 73 releaseGate.write(closedAngle); 74} 75 76float DistanceToObstacle() 77{ 78 delay(100); 79 long duration, inches, cm; 80 pinMode(distancePingPin, OUTPUT); 81 digitalWrite(distancePingPin, LOW); 82 delayMicroseconds(2); 83 digitalWrite(distancePingPin, HIGH); 84 delayMicroseconds(10); 85 digitalWrite(distancePingPin, LOW); 86 pinMode(distanceEchoPin, INPUT); 87 duration = pulseIn(distanceEchoPin, HIGH); 88 cm = MicrosecondsToCentimeters(duration); 89 return cm; 90} 91 92long MicrosecondsToCentimeters(long microseconds) { 93 return microseconds / 29 / 2; 94} 95
Downloadable files
Schematic
Schematic
Breadboard
Breadboard
Schematic
Schematic
Comments
Only logged in users can leave comments