How do I control a motor ?J?

Hi, (i'm not native so excuse if I make somes mistakes)
For a school project, I need to control a motor with a button, I want that when the button is pressed :

The motor turn in one side for 5 seconds
Stop for 5 seconds
Turn in the other side for 5 seconds
Stop

And when the button is pressed again, the motor turns in one side and the other again

So, with my basic knowledge and with the class I had, I made this :

const int buttonPin = 2;  // pin of the button
const int motorPin1 = 9;  // motor pin1 (IN1 on the L298N)
const int motorPin2 = 10; // motor pin2 (IN2 on the L298N)

// States button variable
int buttonState = 0;

void setup() {
  // Initiate pins
  pinMode(buttonPin, INPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);

  // Begin with pin motor low
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
}

void loop() {
  // read button state
  buttonState = digitalRead(buttonPin);

  // if button pressed
  if (buttonState == HIGH) {
    // turn the motor
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    delay(5000); // wait 5 sec

    // stop the motor for 5 sec
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    delay(5000);

    // turn the motor in the other side
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    delay(5000); // wait 5 sec

    // stop the motor for 5 sec
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    delay(5000);
  }
}

I tried and it didn't work, my teacher isn't helpful at all so i'm here to ask if you have any idea where i made a mistake

ty

Welcome to the forum

What did it do ?

More details please

ty

What did it do ?

Nothing, the motor didnt turned, i tried different circuit, it didnt worked either i tried to change the motor, the cables, the button every thing, but nothing worked so i guess the mistake is from the programm

What voltage is the motor?

Start with a simpler sketch that simply runs the motor in one direction at a fixed speed forever

How is the motor powered ?

are you driving the motor directly from the Arduino I/O pins?
or using some driver chip/board (e.g. L293)?

FKT06QMICRXPLI6

I first tried to see how the motor works and i copy pasted a program that would make the motor turn in one direction then stop then turn in the same direction forever and it worked well

I tried to power the 12V motor with the usb cable of my PC but i didnt worked, so i tried to use a battery but it didn't worked either

my teacher gave me a Arduino MEGA with a Motor Shield, there is a "L298P" so ye
edit: tho its on the board, not on the breadboard

Was it a 12V battery?

it was 4 battery of 3V so i yea i think, i also tried to connect it to a generator of 12V

Please post a schematic of the circuit that you used with the sketch that you posted. We need to see how the button is wired, for instance

it kinda looks like this (please ignore the 1.5v battery or the resistor not in the right pin, it's all good irl)((pwease dont insult me i suck at arduino :')))

Follow this tutorial:
https://docs.arduino.cc/tutorials/motor-shield-rev3/msr3-controlling-dc-motor/

Your code generates two five second pulses on pins 9 and 10, as I expected:

Does your motor shield have an ENABLE pin, that needs to be taken high for the motor to work?

I have no idea ;-;

See post #13

I'm looking on it rn, i'll tell you later if it's working
but the few i looked on it, it's always ON, is there a way to to make it that it's only when you push the button that the program execute ?

Why don't you make that code work first, then we will worry about the pushbutton later

2 Likes

It worked ! ty for this tutorial, i'll try to add the push button in my next class tomorrow, i'll work on it tonight.

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