Hi. I started a project, where I want to control a DC Motor direction.
What I want to happen:
- Power on circuit --> Motor starts turning automatically left.
- touch sensor 1 --> turn right
- touch sensor 2 --> turn left
- continue like that in loop.
I am using the button library from this site for button control.
So far this is the Code:
#include <Button.h>
Button button1 = Button(12,PULLUP); //button to turn right
Button button2 = Button(13,PULLUP); //button to turn left
int in1 = 4; // input1 on L298
int in2 = 3; // input2 on L298
int enb = 2; // enable on L298
void setup (){
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(4,OUTPUT);
pinMode(3,OUTPUT);
pinMode(2,OUTPUT);
}
void loop(){
if(button1.isPressed()){
digitalWrite(4,HIGH);
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
}
if(button2.isPressed()){
digitalWrite(3,HIGH);
digitalWrite(2,HIGH);
digitalWrite(4,LOW);
}
}
This works, just that I have to press one of the buttons to initiate the rotation. I would like that to start automatically.
I have no programming experience. I am happy I go this far. I tried adding a else loop after the two IFs but that only makes the motor turn one direction non stop. I have to HOLD the button for it to turn the other way.
Any suggestions/help ? Thanks!