Help me "How to "pause" the DC Motor and "stop" after a certain time

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)
    }
}

Hello
I recommend that you study the IPO model to get a better start on system design.
Based on the study, you should draw a programme structure diagram to identify the required software functions and relationships. By programming and testing the software functions step by step, you will get the desired functionality.
Have a nice day and enjoy programming in C++.

It sounds like your program can be in a number of discrete states and, as such, would lend itself to being implemented as a state machine. Using switch/case with the cases being system states allows you to control which sections of the program are run, their order and where applicable how long they should run for

Your program does not monitor the current position of the motor and therefore does not work properly. I can think of 2 options. Your program should monitor the motor whether it is in the upper or lower position and move it or not according to the data from the ultrasonic sensor and the current position. I would prefer the other option and that is to use limit switches with a rectifier diodes for the motor. Thus, the final positions will always be guaranteed and will be able to change if necessary without reprogramming the Arduino and the control program itself seems to me to be simpler.

You have no position feedback for the DC motor so you can't move the temperature sensor to any specific position (like eye level). I think you will have to either add a shaft encoder to provide position feedback or switch to a stepper motor. In either case, you will need to have a limit switch at the top so you can home the temperature sensor when the power comes on.

What I think you want your machine to do is:

When the ultrasonic sensor detects an object that may be a human head, move the temperature sensor to approximately eye level and pause there to take a reading.

Yes! Yes! that's correct. That's what I intend to do. Wow I have never thought about limit switch. Thank you for your advice :slight_smile: