1

I'm running a dc motor using esp32 and mosfet by taking readings of IR.

Program works fine when used delay() but same program doesn't work when uploaded using millis().

//This uses fixed delay()

// Pin definitions
const int irSensorPin = 34;  // IR sensor connected to GPIO 2
const int motorPin = 32;     // DC motor connected to GPIO 4

// Variables
bool isObjectDetected = false;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Set pin modes
  pinMode(irSensorPin, INPUT);
  pinMode(motorPin, OUTPUT);
}

void loop() {
  // Check if object is detected
  if (digitalRead(irSensorPin) == LOW) {
    if (!isObjectDetected) {
      // Object detected for the first time
      isObjectDetected = true;
      Serial.println("Object detected. Stopping motor.");
      digitalWrite(motorPin, LOW);  // Stop the motor
      delay(500);                   // Wait for 500ms
      Serial.println("Resuming motor.");
      digitalWrite(motorPin, HIGH); // Start the motor
    }
  } else {
    if (isObjectDetected) {
      // Object is no longer detected
      isObjectDetected = false;
    }
  }
}



//This uses millis()

// Pin definitions
const int irSensorPin = 34;  // IR sensor connected to GPIO 34
const int motorPin = 32;     // DC motor connected to GPIO 32

// Variables
bool isObjectDetected = false;
unsigned long motorStopTime = 0;
const unsigned long motorDelayTime = 500;  // Delay time in milliseconds

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Set pin modes
  pinMode(irSensorPin, INPUT);
  pinMode(motorPin, OUTPUT);

  // Initialize motor state
  digitalWrite(motorPin, HIGH);  // Start the motor
}

void loop() {
  // Check if object is detected
  if (digitalRead(irSensorPin) == HIGH) {
    if (!isObjectDetected) {
      // Object detected for the first time
      isObjectDetected = true;
      Serial.println("Object detected. Stopping motor.");
      digitalWrite(motorPin, LOW);  // Stop the motor
      motorStopTime = millis();     // Record the stop time
    }
  } else {
    if (isObjectDetected) {
      // Object is no longer detected
      isObjectDetected = false;
      unsigned long currentTime = millis();
      if (currentTime - motorStopTime >= motorDelayTime) {
        Serial.println("Resuming motor.");
        digitalWrite(motorPin, HIGH); // Start the motor
      }
    }
  }
}

6
  • doesn't work” is not a useful description of the problem. What is the expected behavior? What is the actual behavior? May 11 at 13:59
  • @EdgarBonet Sorry for my bad clarification. I have made a program where IR sensor detects object and the dc motor has to turn off for 1 sec or 500ms. In first of my program it works fine. motor starts normally, ir detects object and tell motor to wait for 1 sec and then motor starts again after 1 second even if ir sensor is detecting object ahead. In my second program, I'm using millis() with 500ms delay but that program doesn't start the motor normally. May 11 at 14:16
  • I posted a code example to a similar question at arduino.stackexchange.com/questions/93115/… ... it includes a link to a simulation
    – jsotola
    May 11 at 15:59
  • @jsotola switch? May 11 at 16:36
  • What print statements are you seeing? Could be that the input in "chattering" since you are not debouncing it at all, and so motorStopTime is continuing to get reset to the current time.
    – Chad G
    May 11 at 18:24

1 Answer 1

1

I am not sure I quite understand what the requirements are for this program. However, they do not do the same thing. I tried to represent their behavior as state diagrams, and here is what I got:

Version using delay():

state diagram

Version using millis():

state diagram

In the above diagrams, states with a gray background are those where the motor is off. As can be seen here, the first version never keeps the motor off for more than 0.5 s, where the second one can keep the motor indefinitely off if the sensor remains HIGH.

I suggest you start by drawing the state diagram corresponding to the behavior you actually want. Then, use the technique outlined in this tutorial on finite state machines to turn the diagram into code. Note that finite state machines work well with millis()-based timing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.