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