Good evening. Before I start, I would like to ask you to understand that I am still new in programming and I'm not really good in english. In fact, my field is not deep in programming. Only used for certain simple projects. Still, I am in my final year and am completing a year-end project.
I want to control a DC motor using an Ultrasonic sensor (HC-SR04). I want to make it move clockwise and counterclockwise and stop after a certain time. Like this:
*Move clockwise when ultrasonic sensor distance is < 70 cm --> pause ---> Move counterclockwise (backward) when ultrasonic sensor distance <70 cm -----> stop
Hence, I used a delay function. I'm trying to understand millis() but since I am not good at this (+ I never learn about it before in my course) so it's hard for me to understand it. I tried it, it's not working.
Before that, I told you a little bit about my project. I made an IoT based temperature sensing system
First, I created a system to " * scan body temperature *"
Second, the system to move the temperature up and down when the ultrasonic detects human presence. I put the ultrasonic on the top..and the DC motor next to it
Like this:
So, when the ultrasonic detects a distance of less than 70cm, ie there is a human presence, it will activate the DC motor (clockwise). (The rope will be used to bring the temperature system box down)
Then, the motor will "pause" to forehead level. I want to use this system for wheelchair users.
I have been able to make it move clockwise (distance <70cm) and pause for a moment (delay 10 sec).
(1) The first problem is, the motor will move again after the delay. I want to stop while there are still people there.
Then, I can move it counterclockwise (ie back to actual position) if no one (distance> 70cm)
(2) The second problem, is that the motor still does not stop after returning to the top. I want him to STOP while no one is around.
If I put a delay (). The problem is the same as clockwise. It will move again after delay ()
This is my coding. What do I need to modify? I used Arduino Uno, Ultrasonic sensor, L293D motor driver, and DC motor.
Many thanks in advance!
// defines pins numbers L293D
#define en1 2
#define M1 7
#define M2 6
// defines pins numbers ultrasonic sensor.
const int trigpin = 11;
const int echopin = 12;
// defines variables
long duration;
int distance;
void setup() {
// Set all the motor control pins to outputs
pinMode(en1, OUTPUT);
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(trigpin,OUTPUT); // Sets the trigPin as an Output
pinMode(echopin,INPUT); // Sets the echoPin as an Input
Serial.begin(9600);
}
void loop() {
digitalWrite(trigpin,LOW); //clear ultrasonic trigpin
delayMicroseconds(2);
digitalWrite(trigpin,HIGH); // Sets the trigPin on HIGH state for 10 micro seconds
delayMicroseconds(10);
digitalWrite(trigpin,LOW);
duration= pulseIn(echopin, HIGH); // Read the echoPin, returns the sound wave travel time in microseconds
distance= (duration / 2) / 28.5; // Calculate the distance in CM
Serial.print(distance); // Prints the distance on the Serial Monitor
if(distance<70){
Serial.print(distance);
digitalWrite(M1,HIGH); // if distance is less than 70cm, the motor will move clockwise
digitalWrite(M2,LOW);
analogWrite(en1,255);
Serial.print("cm MOTOR CLOCKWISE");
delay(5000);
// Pause the motor
digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
delay(10000); // I want to make it stop for at least 10 sec or while ultrasonic distance still <70cm so please help me on this :(
}
else{
Serial.print(distance);
digitalWrite(M1,LOW); //Motor will move backward after ultrasonic distance is >70cm
digitalWrite(M2,HIGH);
analogWrite(en1,255);
Serial.print("cm MOTOR COUNTERCLOCKWISE");
delay(5000);
// I want to make the dc motor stop after a certain time and start to move again when ultrasonic distance is less than 70 cm (<70)
}
}
