My Arduino nano writes bad data.

Hello! I am using a board with the bootloader compatible for nano, with ATMega168 and a HC-05 bluetooth module.

I am trying to make a RGB LED change color according to 3 numbers I give for R, G, B from an Android app.
The problem is that sometimes when I choose the color too fast the color changes wrong. So I decided to test it with a bluetooth serial monitor app on the phone.
I tried to test the HC-05 module first but it received and returned the values correctly.
I sent "255 0 0" from the serial monitor and the color changed to red, as it should. After 1 second I sent this "255 0 0" again and the HC-05 returned "0 255 0" and after another 1 second i sent "255 0 0" and HC-05 returned "0 0 255".
I tried changing the baud rate of the HC-05 to 38400 but it didin't work, I can't find the problem here.

I did the connections correctly: Tx-Rx, Rx-Tx, Vcc-5V, Gnd-Gnd

The code:
#include <SoftwareSerial.h>

SoftwareSerial BLU(0,1);

#define redPin 9
#define greenPin 6
#define bluePin 5

void setup()
{
//Serial setup
Serial.begin(38400);
Serial.println("-= HC-05 Bluetooth RGB LED =-");

BLU.begin(38400);
BLU.println("-= HC-05 Bluetooth RGB LED =-");

pinMode(4, OUTPUT);

digitalWrite(4,HIGH);

pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

void loop()
{
while (BLU.available() > 0)
{
int redInt = BLU.parseInt();
int greenInt = BLU.parseInt();
int blueInt = BLU.parseInt();

redInt = constrain(redInt, 0, 255);
greenInt = constrain(greenInt, 0, 255);
blueInt = constrain(blueInt, 0, 255);

if (BLU.available() > 0)
{
setColor(redInt, greenInt, blueInt);

Serial.print("Red: ");
Serial.print(redInt);
Serial.print(" Green: ");
Serial.print(greenInt);
Serial.print(" Blue: ");
Serial.print(blueInt);
Serial.println();

BLU.flush();
}
}
}

void setColor(int red, int green, int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

You are using software serial on the hardware serial pins 0,1 - a sure-fire recipe for disaster. Either use hardware serial on pins 0,1, or software serial somewhere else. If the former, disconnect Bluetooth while uploading your programme.

Nick_Pyner:
You are using software serial on the hardware serial pins 0,1 - a sure-fire recipe for disaster. Either use hardware serial on pins 0,1, or software serial somewhere else. If the former, disconnect Bluetooth while uploading your programme.

I am not sure what you mean by hardware serial, I also tried software serial on other pins but the result is the same.

mihaimoraru:
I am not sure what you mean by hardware serial, I also tried software serial on other pins but the result is the same.

HardwareSerial is what happens on pins 0 and 1 using, for example, Serial.read() and Serial.print()

Post the version of your program with SoftwareSerial on the other pins.

...R

I just changed the parameters in SoftwareSerial BLU()

#include <SoftwareSerial.h>

SoftwareSerial BLU(2,3);

#define redPin 9
#define greenPin 6
#define bluePin 5

void setup()
{
//Serial setup
Serial.begin(38400);
Serial.println("-= HC-05 Bluetooth RGB LED =-");

BLU.begin(38400);
BLU.println("-= HC-05 Bluetooth RGB LED =-");

pinMode(4, OUTPUT);

digitalWrite(4,HIGH);

pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}

void loop()
{
while (BLU.available() > 0)
{
int redInt = BLU.parseInt();
int greenInt = BLU.parseInt();
int blueInt = BLU.parseInt();

redInt = constrain(redInt, 0, 255);
greenInt = constrain(greenInt, 0, 255);
blueInt = constrain(blueInt, 0, 255);

if (BLU.available() > 0)
{
setColor(redInt, greenInt, blueInt);

Serial.print("Red: ");
Serial.print(redInt);
Serial.print(" Green: ");
Serial.print(greenInt);
Serial.print(" Blue: ");
Serial.print(blueInt);
Serial.println();

BLU.flush();
}
}
}

void setColor(int red, int green, int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

Robin2:
HardwareSerial is what happens on pins 0 and 1 using, for example, Serial.read() and Serial.print()

Post the version of your program with SoftwareSerial on the other pins.

...R

Please edit your posts to add code tags.

jremington:
Please edit your posts to add code tags.

And get rid of all that insulting white space while you are at it.

I guess you also need to use Bluetooth at 9600. Bluetooth will only communicate at 38400 if you specifically configure it to do so, and I bet you haven't.

  BLU.begin(9600);

Nick_Pyner:
and I bet you haven't.

  BLU.begin(9600);

I actually did, with AT commands.
I think the problem is the parseInt() function. It returns 0 if no valid digit is read when setTimeout() occurs. And the interval is 1s.

mihaimoraru:
I think the problem is the parseInt() function.

I have no idea of what you are doing, and what you say may be true but, if you want to use software serial, you may find 38400 a bit marginal and there is nothing to suggest you need it, so reverting to 9600 might be a good idea.

Nick_Pyner:
I have no idea of what you are doing

I want to send 3 integer values from the phone representing R G B color values. Then I want to control an RGB LED with arduino using those values with PWM.

While I know nothing about LEDs, that sounds reasonable but there is still nothing there to suggest you need 38400. My comment still stands.