Hackster is hosting Impact Spotlights: Industrial Automation. Watch the stream live on Thursday!Hackster is hosting Impact Spotlights: Industrial Automation. Stream on Thursday!
wombatlord_
Published

Motion Sensor Stopwatch

Measure the speed of an object with the motion sensor stopwatch.

IntermediateShowcase (no instructions)2,280
Motion Sensor Stopwatch

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×1
Elegoo Ultrasonic Sensor
×1
Switch Sealing Boot, Button Operators
Switch Sealing Boot, Button Operators
×1
Alphanumeric LCD, 16 x 2
Alphanumeric LCD, 16 x 2
×1
Servo Module (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Breadboard

Schematic

Code

StopwatchSensor.ino

Arduino
#include <LiquidCrystal.h>
#include <Servo.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Servo releaseGate;

const int distancePingPin = 9;
const int distanceEchoPin = 10;
const int buttonPin = 6;  
const int servoPin = 7;  
const int detectionDistance = 10;
unsigned long startTime;
unsigned long currentTime;
bool timerStop = false;
int closedAngle = 70;
int openAngle = 170;

void setup() {
  Serial.begin(9600);

  releaseGate.attach(servoPin);

  pinMode(buttonPin, INPUT);
  
  lcd.begin(16, 2);
  startTime = 0;
  StartClock();
}

void loop() 
{
  currentTime = millis();
  
  int distance = DistanceToObstacle();

  if (distance <= detectionDistance)
  {
    if (timerStop == false)
    {
    lcd.clear();
    delay(100);
    float currentT = currentTime;
    float startT = startTime;
    if ((currentT - startT) /1000 > 60)
    {
          lcd.print(((currentT - startT) /1000) / 60);
          lcd.print(" Minutes");
    }
    else
    {
          lcd.print((currentT - startT) /1000);
          lcd.print(" Seconds");
    }
    timerStop = true;
    }
  }

  if(digitalRead(buttonPin) == HIGH)
  {
    StartClock();
  }
}

void StartClock()
{
    releaseGate.write(openAngle);
    timerStop = false;
    startTime = millis();
    lcd.clear();
    delay(100);
    lcd.print("Clock Started");
    delay(500); 
    releaseGate.write(closedAngle);
}

float DistanceToObstacle()
{
   delay(100);
   long duration, inches, cm;
   pinMode(distancePingPin, OUTPUT);
   digitalWrite(distancePingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(distancePingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(distancePingPin, LOW);
   pinMode(distanceEchoPin, INPUT);
   duration = pulseIn(distanceEchoPin, HIGH);
   cm = MicrosecondsToCentimeters(duration);
   return cm;
}

long MicrosecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;
}

Credits

wombatlord_
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.