Arduino Bluetooth controled car code issue

When I press f my Arduino car works perfectly and both motors follow the instructions and go forward, however whenever I press b to go backward Arduino doesn't follow the code at all and moves one motor forward and the other stays still.

if(inputString == "f")

{

digitalWrite(3, HIGH);

digitalWrite(2, LOW);

digitalWrite(4, HIGH);

digitalWrite(5, LOW);

}

else if(inputString == "b")

{

digitalWrite(3, LOW);

digitalWrite(2, HIGH);

digitalWrite(4, LOW);

digitalWrite(5, HIGH);

}

else if(inputString == "r")

{

  digitalWrite(3, HIGH);  

digitalWrite(2, LOW);

digitalWrite(4, LOW);

digitalWrite(5, LOW);

}

else if(inputString == "l")

{

digitalWrite(3, LOW);

digitalWrite(2, LOW);

digitalWrite(4, HIGH);

digitalWrite(5, LOW);

}

inputString = "";

}

Hello
I think that is magic :slight_smile:
Show your complete sketch and schematic.

Heres the full code :rofl:

char junk;

String inputString="";

void setup()

{

Serial.begin(9600);

pinMode(2, OUTPUT);

pinMode(3,OUTPUT);

pinMode(4,OUTPUT);

pinMode(5,OUTPUT);

}

void loop()

{

if(Serial.available()){

while(Serial.available())

{

  char inChar = (char)Serial.read(); 

  inputString += inChar;       

}

Serial.println(inputString);

while (Serial.available() > 0)  

{

junk = Serial.read() ;

}

if(inputString == "f")

{

digitalWrite(3, HIGH);

digitalWrite(2, LOW);

digitalWrite(4, HIGH);

digitalWrite(5, LOW);

}

else if(inputString == "b")

{

digitalWrite(3, LOW);

digitalWrite(2, HIGH);

digitalWrite(4, LOW);

digitalWrite(5, HIGH);

}

else if(inputString == "r")

{

  digitalWrite(3, HIGH);  

digitalWrite(2, LOW);

digitalWrite(4, LOW);

digitalWrite(5, LOW);

}

else if(inputString == "l")

{

digitalWrite(3, LOW);

digitalWrite(2, LOW);

digitalWrite(4, HIGH);

digitalWrite(5, LOW);

}

inputString = "";

}

}

Hello
the best way to see what happens inside the sketch inject some Serial.println() locared at POIs.
I avoid the usage of while() instructions as far as possible :slight_smile:

Does your Serial.println(inputString); show what you are expecting? Why are you using a String for single character commands? What app is sending the commands?

You've said what f and b do. How about r and l?

Steve

I'm sending it commands via the Serial Bluetooth terminal app. L and r don't do anything, the car just doesn't respond.

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