hi, i have a problem with my code. i want to turn a dc motor clockwise upon button press and turn counterclockwise when the same button is pressed again. so it should be: button pressed, turn left, button pressed again, turn right. i tried like this and thought it should work, but it doesnt rest after being pressed once, but it turns right back.
int pin1 = 13;
int pin2 = 12;
int inputpin = 2;
int val = 0;
void setup(){
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(inputpin, INPUT);
}
void loop(){
val = digitalRead(inputpin);
if (val == HIGH){
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
} else {
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
delay(1500);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
}
if (val == HIGH){
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
} else {
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
delay(1500);
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
}
}
i tried it with the command for the turning back inside and outside the first if(){...
and outside. tried everything, but i guess im missing something here.
i hope this is not a too stupid question and somebody can help me here,
thanks