I recently made an rc car and I wanted to add some lights to it. So I grabbed some leds plugged them into the pins and started coding. When I was done I found out that every time I turn on the leds using my phone (the car works using Bluetooth), I can't move forwards, backwards, left or right. when I close the leds it starts moving again. I tried it while the car is moving. I made the car move then I opened the led and as excepted the car stopped and when I closed back the led the car started working again. Any help?!
Code:
#include <AFMotor.h>
AF_DCMotor motor1(1); //motor1 is the left motor
AF_DCMotor motor2(2); //motor2 is the right motor
char junk;
String inputString="";
int led1 = 7; // Trigger 6 Pin of Ultrasonic Sensor
int led2 = 6;// echo 6
int ledState = LOW;
void setup()
{
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
motor1.setSpeed(255); //motor speed is set
motor2.setSpeed(255);
Stop();
}
void loop() {
if(Serial.available()){
while(Serial.available())
{
char inChar = (char)Serial.read(); //read the input
inputString += inChar; //make a string of the characters coming on serial
}
while (Serial.available() > 0)
{
junk = Serial.read() ;
} // clear the serial buffer
if(inputString == "f") //when the bluetooth module recieves 1 the car moves forward
{
forward();
}
else if(inputString == "b") //when the bluetooth module recieves 2 the car moves backward
{
backward();
}
else if(inputString == "l") //when the bluetooth module recieves 3 the car moves left
{
left();
}
else if(inputString == "r") //when the bluetooth module recieves 4 the car moves right
{
right();
}
else if(inputString == "s") //when the bluetooth module recieves 5 the car stops
{
Stop();
}
else if(inputString == "a") //when the bluetooth module recieves 6 the car opens leds
{
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(led1, ledState);
digitalWrite(led2, ledState);
}
inputString = "";
}
}
void forward()
{
motor1.run(FORWARD);
motor2.run(FORWARD);
}
void backward()
{
motor1.run(BACKWARD);
motor2.run(BACKWARD);
}
void left()
{
motor1.run(BACKWARD);
motor2.run(FORWARD);
}
void right()
{
motor1.run(FORWARD);
motor2.run(BACKWARD);
}
void Stop()
{
motor1.run(RELEASE);
motor2.run(RELEASE);
}