Hello everyone, I hope that y'all are doing great.
As you may know, i tried to do a servo actuation program with a color sensor, it worked, but the detection range was poor, and an ultrasonic sensor could do the job as well.
I now have my ultrasonic sensor, my code done, and my wiring done, it works BUT ...
I have 3 cases in my program, and one need to be time driven, and I don't know how to do it, lemme explain:
If we detect an object in a distance =< 10 cms
wait c secs
if the object is still =< 10 cms
actuate the servo to go to pos a
else
actuate the servo to go to pos b
else
actuate the servo to go to pos b
The program:
// Breadboard +5 to Arduino Vin
// Breadboard GND to Arduino GND
// Breadboard +5 to DS3225 Red
// Breadboard GND to DS3225 Black
// Breadboard +5 to HY-SRF05 Vcc
// Breadboard GND to HY-SRF05 GND
// Arduino D8 to HY-SRF05 Echo
// Arduino D9 to HY-SRF05 trigger
// Arduino D10 to DS3225 Orange
//-----------------------------------------------------------------------------------------------------
float duration = 0; // Intitialize the echo duration at 0 (ms)
float distance = 0; // Initialize the distance at 0 (cms)
//-----------------------------------------------------------------------------------------------------
#include <Servo.h> // Include the Servo library
//-----------------------------------------------------------------------------------------------------
#define trigger 9 // Define the HY-SRF05 trigger pin on the Arduino pin 9
#define echo 8 // Define the HY-SRF05 trigger pin on the Arduino pin 8
//-----------------------------------------------------------------------------------------------------
Servo Servo1; // Assign the name Servo1 to the servo
//-----------------------------------------------------------------------------------------------------
void setup() // Initialization of the code
{
pinMode(trigger, OUTPUT); // Define the trigger pin as an Output
pinMode(echo, INPUT); // Define the echo pin as an Input
Serial.begin(9600); // Initialize the Serial monitor at 9600 Bauds
Servo1.attach(10); // Attach the Servo1 to the pin 10
}
//-----------------------------------------------------------------------------------------------------
void loop() // Code run repetadly
{
digitalWrite(trigger, LOW); // Set the trigger pin as a low state
delayMicroseconds(2); // For 2 seconds to ensure that the trigger is always at low at each loop
digitalWrite(trigger, HIGH); // Set the trigger pin as an high state
delayMicroseconds(10); // For 10 mandatory seconds to initialize the mesurement by sending a wave
digitalWrite(trigger, LOW); // Set the trigger pin as a low state to stop the wave emission
duration = pulseIn(echo, HIGH); // Read the high state pulse on the echo pin
distance = duration * 0.034 / 2; // Calculates the distance from the sensor using the celerity of a wave
if ((distance > 3) && (distance < 10))) //If the distance object-sensor is between 3 and 10 cms
{
while ((distance > 3) && (distance < 10) && ("time condition not met"))
{
Servo1.write(0); // Write the position value on the variable pos
delayMicroseconds(5); // Give Servo1 10ms to actuate itself
Serial.print("Distance: "); // Print on the serial monitor the word Distance :
Serial.println(distance); // Print on the serial monitor the distance sensor-object
}
if (("time condition met"))
{
Servo1.write(90); // Write the position value on the variable pos
delayMicroseconds(5); // Give Servo1 5ms to actuate itself
Serial.print("Distance: "); // Print on the serial monitor the word Distance :
Serial.println(distance); // Print on the serial monitor the distance sensor-object
}
}
else
{
if (distance > 10) //If the distance object-sensor is superior to 10 cms
{
Servo1.write(180); // Write the position value on the variable pos
delayMicroseconds(5); // Give Servo1 5ms to actuate itself
Serial.print("Distance: "); // Print on the serial monitor the word Distance :
Serial.println(distance); // Print on the serial monitor the distance sensor-object
}
}
}
Thanks for y'all answers