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
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
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
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 :')))
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 ?