How do I create a timer to time how long the wheels are moving forward before they are stopped by the ultrasonic distance sensor? Then, I would like to store that amount of time in a variable to be used later.
const int trigPin = A3; //Distance Sensor
const int echoPin = A2;
const int drillPos = 4; //Drill Motor
const int drillNeg = 5;
const int rightFrontPos = 12; //Right Wheels
const int rightFrontNeg = 13;
const int rightBackPos = 10;
const int rightBackNeg = 11;
const int leftBackPos = 8; //Left Wheels
const int leftBackNeg = 9;
const int leftFrontPos = 6;
const int leftFrontNeg = 7;
const int sensorPin = A0; //Temperature Sensor
const float baselineTemp = 25.0;
const int redColor = 3; //Red color
const int greenColor = 2; //Green color
float duration;
float distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(drillPos, OUTPUT);
pinMode(drillNeg, OUTPUT);
pinMode(rightFrontPos, OUTPUT);
pinMode(rightFrontNeg, OUTPUT);
pinMode(rightBackPos, OUTPUT);
pinMode(rightBackNeg, OUTPUT);
pinMode(leftFrontPos, OUTPUT);
pinMode(leftFrontNeg, OUTPUT);
pinMode(leftBackPos, OUTPUT);
pinMode(leftBackNeg, OUTPUT);
pinMode(redColor, OUTPUT);
pinMode(greenColor, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 74;
Serial.print("Distance in inches: ");
Serial.println(distance);
if (distance > 100) {
analogWrite(greenColor, 255); //Set light to green
analogWrite(redColor, 0);
digitalWrite(leftFrontPos, HIGH); //Wheels Spin Forward
digitalWrite(leftFrontNeg, LOW);
digitalWrite(leftBackPos, HIGH);
digitalWrite(leftBackNeg, LOW);
digitalWrite(rightFrontPos, HIGH);
digitalWrite(rightFrontNeg, LOW);
digitalWrite(rightBackPos, HIGH);
digitalWrite(rightBackNeg, LOW);
} else {
//Stop all motors
for (int motorPin = 6; motorPin <=13; motorPin++) {
digitalWrite(motorPin, LOW);
}
delay(1000); //Delay for one second (adjust as needed)
//Start the drill motor
digitalWrite(drillPos, HIGH);
digitalWrite(drillNeg, LOW);
int sensorVal = analogRead(sensorPin);
Serial.print("Sensor Value: ");
Serial.print(sensorVal);
//Convert the ADC Reading to voltage
float voltage = (sensorVal / 1024.0) * 5.0;
Serial.print(", Volts: ");
Serial.print(voltage);
Serial.print(", degrees C: ");
//Convert the voltage to temperature in degrees Celcius
float temperature = (voltage - 0.5) * 100;
Serial.println(temperature);
if (temperature <= baselineTemp) {
//Continue spinning the drill motor
digitalWrite(drillPos, HIGH);
digitalWrite(drillNeg, LOW);
}
else {
//Stop the drill motor
digitalWrite(drillPos, LOW);
digitalWrite(drillNeg, LOW);
//Set RGB Light to Red
analogWrite(greenColor, 0);
analogWrite(redColor, 255);
//Reverse the direction of all wheels
digitalWrite(rightFrontPos, LOW);
digitalWrite(rightFrontNeg, HIGH);
digitalWrite(rightBackPos, LOW);
digitalWrite(rightBackNeg, HIGH);
digitalWrite(leftFrontPos, LOW);
digitalWrite(leftFrontNeg, HIGH);
digitalWrite(leftBackPos, LOW);
digitalWrite(leftBackNeg, HIGH);
delay(3000);
//Stop all motors
for (int motorPin = 6; motorPin <= 13; motorPin++) {
digitalWrite(motorPin, LOW);
}
//Stop the wheels in place for 10 minutes
delay(600000);
}
}
}