// When ever I push the button, the motors change direction but only after several seconds of delay
const int m1a = 8;
const int m1b = 9;
const int m2a = 10;
const int m2b = 11;
const int forward = 2;
const int left = 3;
const int back = 4;
const int right = 5;
void setup() {
Serial.begin(9600);
pinMode(forward, INPUT);
pinMode(left, INPUT);
pinMode(back, INPUT);
pinMode(right, INPUT);
pinMode(m1a, OUTPUT);
pinMode(m1b, OUTPUT);
pinMode(m2a, OUTPUT);
pinMode(m2b, OUTPUT);
}
void loop() {
if (digitalRead(forward) == HIGH)
{
digitalWrite(m1a, HIGH);
digitalWrite(m2a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2b, LOW);
}
if (digitalRead(left) == HIGH)
{
digitalWrite(m1a, LOW);
digitalWrite(m2a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2b, LOW);
}
if ((digitalRead(right) == LOW) && (digitalRead(left) == LOW) && (digitalRead(forward) == LOW) && (digitalRead(back) == LOW))
{
digitalWrite(m1a, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2b, LOW);
}
if (digitalRead(back) == HIGH)
{
digitalWrite(m1a, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m1b, HIGH);
digitalWrite(m2b, HIGH);
}
if (digitalRead(right) == HIGH)
{
digitalWrite(m1a, HIGH);
digitalWrite(m2a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2b, LOW);
}
}
Show your schematic and think about detecting when the button was pushed rather than is pushed, debouncing and state change detection. Lots of examples of each to look up
Thanks for telling. Do You have any problem and a question?
Read this link: How to get the best out of this forum - Using Arduino / Project Guidance - Arduino Forum
Thanks, I fixed it
Thank you for helping me fix me
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.