Search code examples
arduinoesp32

ESP32-WROOM-32 PWD with millis


Beginner in Arduino and ESP-32 needs help.

Hello together, I’m using the Pololu - VNH5019 Motor Driver Carrier to control a 12v motor with an ESP32. In the following sketch i can speed up and speed down the ramp with delay();. I tried to archiv the same result with millis(), but until now i could not make it. What i am missing in my code.

Thanks in advance.

#define MOTOR_IN1         27
#define MOTOR_IN2         16
#define PWMPIN            14
#define frequency         40000
#define resolutionbit     8

const unsigned long eventInterval = 30;
unsigned long previousTime = 0;

void setup() {
  pinMode(MOTOR_IN1, OUTPUT);
  pinMode(MOTOR_IN2, OUTPUT);
  ledcAttachPin(PWMPIN, 0);  // assign the speed control PWM pin to a channel
  ledcSetup(0, frequency, resolutionbit);
}

void loop() {
  //with_delay();
  with_millis();
}
//------------------------------------------
void with_delay() {
  // set direction
  digitalWrite(MOTOR_IN1, HIGH);
  digitalWrite(MOTOR_IN2, LOW);

  // ramp speed up
  for (int i = 0; i <= 255; i++) {
    ledcWrite(0, i);
    delay(30);
  }
  // ramp speed down
  for (int i = 255; i >= 0; i--) {
    ledcWrite(0, i);
    delay(30);
  }
}
//-------------------------------------------
void with_millis() {
  unsigned long currentTime = millis();

  if (currentTime - previousTime >= eventInterval) {
    digitalWrite(MOTOR_IN1, HIGH);
    digitalWrite(MOTOR_IN2, LOW);
    for (int i = 0; i <= 255; i++) {
      ledcWrite(0, i);
      previousTime = currentTime;
    }
  }
  if (currentTime - previousTime >= eventInterval) {
    digitalWrite(MOTOR_IN1, HIGH);
    digitalWrite(MOTOR_IN2, LOW);
    for (int i = 255; i >= 0; i--) {
      ledcWrite(0, i);
      previousTime = currentTime;
    }
  }
}


Solution

  • Your problem is that the program gets stuck in the for loop.

    • You need to also create direction variable so the program that knows which if statement to execute.
    • You need to create some other logic that will increase the i variable without stopping the whole program.

    The code:

    //Initialize the i variable globaly:
    int i = 0;
    bool direction = 0;
    
    
    //Your function:
    void with_millis() {
      unsigned long currentTime = millis();
    
      if ((currentTime - previousTime >= eventInterval) && direction == true) {
        digitalWrite(MOTOR_IN1, HIGH);
        digitalWrite(MOTOR_IN2, LOW);
        i++;
        if (i <= 255) {
          ledcWrite(0, i);
          previousTime = currentTime;
        } elif (i > 255) {
          i = 0;
          direction = false;
        }
      }
    
      if ((currentTime - previousTime >= eventInterval) && direction == false) {
        digitalWrite(MOTOR_IN1, LOW);
        digitalWrite(MOTOR_IN2, HIGH);
        i++;
        if (i <= 255) {
          ledcWrite(0, i);
          previousTime = currentTime;
        } elif (i > 255) {
          i = 0;
          direction = true;
        }
      }
    }