I'm trying to control three relays with the Arduino Uno rev3 and a HC-05 bluetooth module from Elecfreaks.
I've wrote this code:
// the comments are the part of the code that I use to replace the Serial by default to be able to use the HC-05 module
//#include <SoftwareSerial.h>
int relay1 = 2;
int relay2 = 3;
int relay3 = 4;
int relay1estatus = 0;
int relay2estatus = 0;
int relay3estatus = 0;
char command;
//SoftwareSerial bluetooth(5,6);
void setup() {
Serial.begin(9600); //bluetooth.begin(9600);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
}
void loop() {
while(Serial.available() > 0){ //bluetooth.available()
command = (byte)Serial.read(); //(byte)bluetooth.read();
Serial.println(command); //bluetooth.println(command)
if (command == 'a'){
if (relay1estatus == 0){
digitalWrite(relay1, HIGH);
relay1estatus = 1;
}else{
digitalWrite(relay1, LOW);
relay1estatus = 0;
}
}
if (command == 'b'){
if (relay2estatus == 0){
digitalWrite(relay2, HIGH);
relay2estatus = 1;
}else{
digitalWrite(relay2, LOW);
relay2estatus = 0;
}
}
if (command == 'c'){
if (relay3estatus == 0){
digitalWrite(relay3, HIGH);
relay3estatus = 1;
}else{
digitalWrite(relay3, LOW);
relay3estatus = 0;
}
}
}
}
The problem is that this code works perfectly using the Arduino IDE serial monitor, but when I try it using instead of the default serial methods, the SoftwareSerial ones it doesn't work.
Doesn't work in a very strange way because I can switch one by one the relays between on or off but, when I have two of them activated seems like freezes, I can't switch the third one on or off and neither I can switch the other two again.
To send the 'commands' through bluetooth I use the Bluetooth Terminal app available in the PlayStore. (I've just posted the same question in stackoverflow but now I think here's the best place to ask it)