flashing led in bluetooth car

I want to connect a flashing rgb led in my Bluetooth controlled Arduino car which constantly flash as red-blue-red-blue.... I have made a code and it flashes led as required but when I connect Bluetooth module(HC05), led starts working abnormally. I think its a coding problem please have a look on my code and help me out of this, as I am new in this... maybe its a bad programming

char junk;
String inputString="";
const int lf=9;// motor
const int rf=3;// motor
const int lb=10;// motor
const int rb=11;// motor
const int light=5;
const int r=8; // red led
const int b=7; // blueled
unsigned long tm=0;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(lf, OUTPUT);  
pinMode(rf, OUTPUT);  
pinMode(lb, OUTPUT);  
pinMode(rb, OUTPUT);
pinMode(light, OUTPUT);
pinMode(r, OUTPUT);  
pinMode(b, OUTPUT);  
digitalWrite(r,LOW);// low due to common cathode rgb led
}

void loop() 
{
  rgb(); // function to flash rgb led
  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
    }
    Serial.println(inputString);
    while (Serial.available() > 0)  
    { junk = Serial.read() ; }      // clear the serial buffer
    
    if(inputString == "f")
    { 
      forward();
    }
    else if(inputString == "b")
    { 
      backward();
    }
    else if(inputString == "l")
    { 
      left();
    }
    else if(inputString == "r")
    { 
      right();
    }
    else if(inputString == "z")
    { 
      forleft();
    }
    else if(inputString == "x")
    { 
      forright();
    }
    else if(inputString == "v")
    { 
      backleft();
    }
    else if(inputString == "c")
    { 
      backright();
    }
    else if(inputString == "s")
    { 
      stopp();
    }
    else if(inputString == "n")
    { 
      digitalWrite(light,HIGH);
    }
    else if(inputString == "m")
    { 
      digitalWrite(light,LOW);
    }
    inputString = "";
}
}
void forward()
{
  digitalWrite(lf,HIGH);
  digitalWrite(rf,HIGH);
}
void backward()
{
  digitalWrite(lb,HIGH);
  digitalWrite(rb,HIGH);
}
void left()
{
  digitalWrite(rf,HIGH);
}
void right()
{
  digitalWrite(lf,HIGH);
}
void forleft()
{
  analogWrite(lf,150);
  analogWrite(rf,255);
}
void forright()
{
  analogWrite(rf,150);
  analogWrite(lf,255);
}
void backleft()
{
  analogWrite(lb,150);
  analogWrite(rb,255);
}
void backright()
{
  analogWrite(rb,150);
  analogWrite(lb,255);
}
void stopp()
{
  digitalWrite(lf,LOW);
  digitalWrite(lb,LOW);
  digitalWrite(rf,LOW);
  digitalWrite(rb,LOW);
}
void rgb() // red-blue-red-blue....
{
  if(millis()-tm>500)
  {
    if(digitalRead(r)==LOW)
    {
      digitalWrite(r,HIGH);
      digitalWrite(b,LOW);
      tm=millis();
    }
    else if(digitalRead(b)==LOW)
    {
      digitalWrite(b,HIGH);
      digitalWrite(r,LOW);
      tm=millis();
    }
  }
}

the function for flashing is in the last which is called in the starting of void loop. Also I have used digitalWrite(r,LOW); to turn on the red led because it is a common cathode led, same as for blue.

Be wise when you iterate over the Serial Port. let the loop do it's magic, handle one character at a time, don't build massive strings when you are really interested only in 1 character and don't use the String class (with a capital S). You want to loop fast and handle things through the loop basically.

I restructured your program to do this - I think it's much simpler like this. of course it's untested but I let you do that :slight_smile:

I don't "clean up" what you used to call junk on the Serial line, when the loop() gets to read those, if it's not a recognized character when I read it, then it won't do anything with it anyway. This way I make sure the loop() comes back to the top quickly to ensure I blink that RGB LED every half second and don't end up stuck in a Serial time-out or something.

I also created a few functions so that the code looks more readable

of course you have the right current limiting resistor for your R and B pins, right? (they don't take the same V in usually, Red has a smaller max V

I would suggest as well to define as constant 150 and 255 you use here and there in the code so that it's easier for you to change those in one place rather than throughout the code if needed. Same would apply for the 1/2 second blinking period make a const unsigned int (or long) blinkRate=500; and use that in the rgb() function - again will be more readable and easy to change if all is at the beginning of the code

give it a try.

const int lf = 9; // motor
const int rf = 3; // motor
const int lb = 10; // motor
const int rb = 11; // motor
const int light = 5;
const int r = 8; // red led
const int b = 7; // blueled
unsigned long tm = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(lf, OUTPUT);
  pinMode(rf, OUTPUT);
  pinMode(lb, OUTPUT);
  pinMode(rb, OUTPUT);
  pinMode(light, OUTPUT);
  pinMode(r, OUTPUT);
  pinMode(b, OUTPUT);
  digitalWrite(r, LOW); // low due to common cathode rgb led
}

void loop()
{
  rgb(); // function to flash rgb led
  checkSerial();
}

void checkSerial()
{
  char inChar;

  if (Serial.available()) {
    inChar = Serial.read(); //read the input
    switch (inChar) {
      case 'f': forward();   break;
      case 'b': backward();  break;
      case 'l': left();      break;
      case 'r': right();     break;
      case 'z': forleft();   break;
      case 'x': forright();  break;
      case 'v': backleft();  break;
      case 'c': backright(); break;
      case 's': stopp();     break;
      case 'n': lightOn();   break;
      case 'm': lightOff();  break;
    }
  }
}

void lightOn()
{
  digitalWrite(light, HIGH);
}

void lightOff()
{
  digitalWrite(light, LOW);
}

void forward()
{
  digitalWrite(lf, HIGH);
  digitalWrite(rf, HIGH);
}
void backward()
{
  digitalWrite(lb, HIGH);
  digitalWrite(rb, HIGH);
}
void left()
{
  digitalWrite(rf, HIGH);
}
void right()
{
  digitalWrite(lf, HIGH);
}
void forleft()
{
  analogWrite(lf, 150);
  analogWrite(rf, 255);
}
void forright()
{
  analogWrite(rf, 150);
  analogWrite(lf, 255);
}
void backleft()
{
  analogWrite(lb, 150);
  analogWrite(rb, 255);
}
void backright()
{
  analogWrite(rb, 150);
  analogWrite(lb, 255);
}
void stopp()
{
  digitalWrite(lf, LOW);
  digitalWrite(lb, LOW);
  digitalWrite(rf, LOW);
  digitalWrite(rb, LOW);
}
void rgb() // red-blue-red-blue....
{
  if (millis() - tm > 500)
  {
    if (digitalRead(r) == LOW)
    {
      digitalWrite(r, HIGH);
      digitalWrite(b, LOW);
      tm = millis();
    }
    else if (digitalRead(b) == LOW)
    {
      digitalWrite(b, HIGH);
      digitalWrite(r, LOW);
      tm = millis();
    }
  }
}

Instead of while(Serial.available()), since you are only reading one character at a time just read one character in your loop and let the next iteration of loop() do the next one.

thanks for reply. I have tried this code and still problem is there... the led starts work abnormally as I start controlling motors. the red led stays on and some times after 10-11 seconds blue got lighten up. but thanks for modifying the code this looks much better than mine's.