L298n Motor Driver Code

I have code to use an L298N motor driver. I have 1 DC motor connected and am only interested in moving it in the forwards direction. How would I be able to slim down this code, so that it is only activated for the desired amount of time, when called upon?
Thanks

// Motor A
int motor1Pin1 = 27; 
int motor1Pin2 = 26; 
int enable1Pin = 14; 

// Setting PWM properties
const int freq = 30000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 200;

void setup() {
  // sets the pins as outputs:
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);
  
  // configure LED PWM functionalitites
  ledcSetup(pwmChannel, freq, resolution);
  
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(enable1Pin, pwmChannel);

  Serial.begin(115200);

  // testing
  Serial.print("Testing DC Motor...");
}

void loop() {
  // Move the DC motor forward at maximum speed
  Serial.println("Moving Forward");
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH); 
  delay(2000);


  // Move DC motor forward with increasing speed
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  while (dutyCycle <= 255){
    ledcWrite(pwmChannel, dutyCycle);   
    Serial.print("Forward with duty cycle: ");
    Serial.println(dutyCycle);
    dutyCycle = dutyCycle + 5;
    delay(500);
  }
  dutyCycle = 200;
}

use one of the PWM pins

use analogWrite() to set the pin to a non-zero value to enable the motor

recognize a button or when to enable the motor and capture a timestamp.

in loop() check if the time since the timestamp has expired and disable the motor

Non-blocking timing tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

// example. 

// Motor A 
#define motor1Pin1  7  //<<<<<<<<<<<<<<<<<<<<<
#define motor1Pin2  6  //<<<<<<<<<<<<<<<<<<<<<
#define enable1Pin  14

const byte dutyCycle = 200;

void setup() {
  Serial.begin(115200);
  Serial.print("Testing DC Motor...");
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(enable1Pin, LOW); /// active HIGH ?
}

void loop() {
  if (something == happening)Forward(2000);
}

void Forward(unsigned long howLong) {
  Serial.println("Moving Forward");
  unsigned long startTime = millis();
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(enable1Pin, HIGH);/// active HIGH ?
  for (byte f = dutyCycle; f > 5; f -= 5) {
    Serial.print("Forward with duty cycle: ");
    Serial.println(f);
    analogWrite(motor1Pin1, f);
    delay(100);
  }
  digitalWrite(motor1Pin1, LOW);
  while (millis() - startTime < howLong)delay(10);
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(enable1Pin, LOW);
}

Okay. Thanks for the replies. I have tried a couple of things, but feel like I may be overcomplicating things for myself. How would I be able to move the motor in the forward direction for 20 seconds?

if (..........) Forward(20000);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.