Single motor and timer

Hello, I want to run a single motor with the L298N motor controller and make both speed and direction adjustments, and I want it to work in the interval I want, I made the necessary codes and circuit for speed and direction adjustment, but I could not do it. Time interval. That's what I did for now.


#define enA 9
#define in1 6
#define in2 7
#define button 4

int rotDirection = 0;
int pressed = false;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(button, INPUT);
  // Set initial rotation direction
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
}

void loop() {
  int potValue = analogRead(A0); // Read potentiometer value
  int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer value from 0 to 255
  analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin

  // Read button - Debounce
  if (digitalRead(button) == true) {
    pressed = !pressed;
  }
  while (digitalRead(button) == true);
  delay(20);

  // If button is pressed - change rotation direction
  if (pressed == true  & rotDirection == 0) {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    rotDirection = 1;
    delay(20);
  }
  // If button is pressed - change rotation direction
  if (pressed == false & rotDirection == 1) {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    rotDirection = 0;
    delay(20);
  }
}

More information is needed.

Please post a picture of a hand-drawn wiring diagram, with pins, connections and parts clearly labeled. Post a link to the motor product page or data sheet.

Also, please describe what should happen when you run the program, and what happens instead.

don't understand what you mean by interval

you button logic is confusing. buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

look this over

# define enA 9
# define in1 6
# define in2 7
# define button 4

byte butLst;

void setup () {
    pinMode (enA, OUTPUT);
    pinMode (in1, OUTPUT);
    pinMode (in2, OUTPUT);
    pinMode (button, INPUT_PULLUP);

    digitalWrite (in1, LOW);
    digitalWrite (in2, HIGH);
}

void loop () {
    int potValue = analogRead (A0);
    int pwmOutput = map (potValue, 0, 1023, 0 , 255);
    analogWrite (enA, pwmOutput);

    byte but = digitalRead (button);
    if (butLst != but) {
        butLst = but;
        delay (20);

        if (LOW == but)  {          // pressed
            if (LOW == digitalRead (in1)) {
                digitalWrite (in1, HIGH);
                digitalWrite (in2, LOW);
            }
            else {
                digitalWrite (in1, LOW);
                digitalWrite (in2, HIGH);
            }
        }
    }
}

"I want" you to post an annotated schematic showing exactly how you have wired it. Be sure all grounds, interconnects, and power sources are shown. Post links to each of the hardware items that gives technical details, links to market places usually do not have that information. The L298N is about the worse choice you can make. You lose about 3 volts to your motor when using it. Is it rated to drive your motor? Without the information "jremington" asked for your odds of getting this answered is slim and none but you could get lucky.

Be smart, before purchasing parts be sure they will do what you want.

Time interval = a segment of time. Per Google: What Is Time Interval? The amount of time between two given times is known as time interval. In other words, it is the amount of time that has passed between the beginning and end of the event."

is a schematic really necessary?

I used the schema here and did the coding I specified. I'm not the type to do this job, so I don't know much. I want to put a time slot in my coding so that when I run the circuit the motor will run and stop for the time I specify.

how exactly is that switch wired? presumably the resistor is connected to both the pin and the switch and the switch pulls the pin HIGH, correct?

Yes exactly as you said

You are missing most of the information I asked for. Your frizzy does not have that information.

Where is A0 defined?

Try this code:
(Not tested by me.)

#define enA 9
#define in1 6
#define in2 7
#define button 4
unsigned long myTime = 2000;   // each 1000 = 1 second
unsigned long lastMillis = 0;  //

int rotDirection = 0;
int pressed = false;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(button, INPUT);
  // Set initial rotation direction
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA, 0);
}

void loop() {
  int potValue = analogRead(A0); // Read potentiometer value
  int pwmOutput = map(potValue, 0, 1023, 0, 255);  // Map the potentiometer value from 0 to 255
  //analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin

  // Read button - Debounce
  if (digitalRead(button) == HIGH) {
    while (digitalRead(button) == HIGH) { delay(20);}
    pressed = !pressed;
    lastMillis = millis();
   
  }
  // while (digitalRead(button) == true);  //  why ;
  // delay(20);

  // If button is pressed - change rotation direction
  if (pressed == true  & rotDirection == 0) {
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    rotDirection = 1;
    analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin
  }
  // If button is pressed - change rotation direction
  if (pressed == false & rotDirection == 1) {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    rotDirection = 0;
    analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin
  }
  if ( millis() - lastMillis > myTime)
  {
    analogWrite(enA, 0);  // Stop motor
  }
}

I tried the code you sent, but it didn't work exactly as I wanted. When I run the circuit I want to do, for example, after waiting for 5 minutes, it will send a signal to the motor, after working for 1 minute, it will wait for 5 minutes and start again.

Turn on the arduino;
wait a few seconds;
pushes the button;
motor turns to one side for 1 minute and stops.
It only allows turning on and turning in the opposite direction when pressing the button if 5 minutes have passed.
And so? If not,
write your flow..

What I want to do:
Step 1: After turning on the Arduino.
Step 2: The motor will not run for the specified time (example 5 minutes).
Step 3: After the specified time has elapsed, the motor will run for the specified time (example 1 minute).
Step 4: The engine stops.
And it continues to run in this cycle.

OK
then try this: (not tested).

#define enA 9
#define in1 6
#define in2 7
unsigned long stopTime = 300000;   // each 1000 = 1 second 5 min = 3000000
unsigned long runTime = 60000;     // each 1000 = 1 second 1 min =   60000
unsigned long lastMillis = 0;      //
int potValue ;
int pwmOutput ;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);

  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA, 0); // Motor stoped
  lastMillis = millis();
}

void loop() {
  potValue = analogRead(A0);                    // Read potentiometer value
  pwmOutput = map(potValue, 0, 1023, 0, 255);   // Map the potentiometer value from 0 to 255
  if (millis() - lastMillis <= stopTime)
  {
    analogWrite(enA, 0);      // Stop motor
  }
  if (millis() - lastMillis > stopTime  && millis() - lastMillis <= stopTime + runTime )
  {
    analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin
  }
  if ( millis() - lastMillis > stopTime + runTime)  {
    analogWrite(enA, 0);      // Stop motor
    lastMillis = millis();    // Reset millis
  }
}

thank you for your help this solved my coding problem <3

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