RC car not moving when leds are on

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);

}

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

inputString = "";

After the LEDs are turned on, the inputString variable is cleared so the next time through loop() the command is empty (not f, b, l, r, s) so everything is turned off until new command comes in from serial.

I would separate the motor and LED functions into different code sections as they are not really related.

As you can see I struck out my previous comment as it was not the right answer. I have, I think, found the problem.

Here is the schematic of the AFMotor motor shield. You see that the PWM0A and PWM0B are connected to pins 5 and 6.

Here is another schematic showing the pins used by the shield. You can use the analog inputs (A0-A5) as digital outputs for the LEDs.

groundFungus:
As you can see I struck out my previous comment as it was not the right answer. I have, I think, found the problem.

Here is the schematic of the AFMotor motor shield. You see that the PWM0A and PWM0B are connected to pins 5 and 6.

Here is another schematic showing the pins used by the shield. You can use the analog inputs (A0-A5) as digital outputs for the LEDs.

I am so sorry for the late reply!
First of all thanks for informing me that I need to format my code, I will do it next post!
Now here, do you mean I need to connect my LEDs to A0-A5 on the shield?

I mean that you could connect the LEDs to A0-A5 because most of the digital pins are used by the shield. The analog inputs are really digital pins with analog input as a special function. Otherwise pins 2 and 13 are open.

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