Bluetooth Relay Disconnect Problem

Hi

I have the following code working on a Arduino Mega communicating with HC-06 bluetooth and can control my 8 relay board exactly as I would like to. In order to doe this I am using the RoboRemo app on an android device and have effectivley made the butons momentary. The only problem I have which is a major one for me is when I disconnect the bluetooth the relay board puts Relays 1,3,5,7 on when I am infact looking to have all relays off on exit. Does anyone have any suggestions please?.

Thanks
Andy

#include <Arduino.h>

int state;

void setup() {

Serial.begin(9600);
Serial.flush();

/////////////////////////////////////Set Digital Pins as Outputs //////////////////////////////////////////

#define RELAY1 2
#define RELAY2 3
#define RELAY3 4
#define RELAY4 5
#define RELAY5 6
#define RELAY6 7
#define RELAY7 8
#define RELAY8 9

{
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
pinMode(RELAY5, OUTPUT);
pinMode(RELAY6, OUTPUT);
pinMode(RELAY7, OUTPUT);
pinMode(RELAY8, OUTPUT);
}
/////////////////////////////////////Set State of Pins all to LOW (1)////////////////////////////////////

digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
digitalWrite(9, 1);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
{
{
if (Serial.available())
state = Serial.read();
}
////////////////////////////////// Downhill Putt UP Direction ///////////////////////////////////////////

if (state == 'A') {
digitalWrite(RELAY1, 0);
digitalWrite(RELAY2, 1);
digitalWrite(RELAY3, 0);
digitalWrite(RELAY4, 1);
}
////////////////////////////////////////////// STOP //////////////////////////////////////////////////////

else if (state == 'a') {
digitalWrite(RELAY1, 1);
digitalWrite(RELAY2, 1);
digitalWrite(RELAY3, 1);
digitalWrite(RELAY4, 1);
}
////////////////////////////////// Downhill Putt DOWN Direction //////////////////////////////////////////

if (state == 'B') {

digitalWrite(RELAY1, 1);
digitalWrite(RELAY2, 0);
digitalWrite(RELAY3, 1);
digitalWrite(RELAY4, 0);
}
////////////////////////////////////////////// STOP //////////////////////////////////////////////////////

else if (state == 'b') {

digitalWrite(RELAY1, 1);
digitalWrite(RELAY2, 1);
digitalWrite(RELAY3, 1);
digitalWrite(RELAY4, 1);
}
////////////////////////////////// Uphill Putt UP Direction //////////////////////////////////////////////

if (state == 'C') {

digitalWrite(RELAY5, 0);
digitalWrite(RELAY6, 1);
digitalWrite(RELAY7, 0);
digitalWrite(RELAY8, 1);
}
////////////////////////////////////////////// STOP //////////////////////////////////////////////////////

else if (state == 'c') {

digitalWrite(RELAY5, 1);
digitalWrite(RELAY6, 1);
digitalWrite(RELAY7, 1);
digitalWrite(RELAY8, 1);
}
///////////////////////////////// Uphill Putt DOWN Direction ////////////////////////////////////////////

if (state == 'D') {

digitalWrite(RELAY5, 1);
digitalWrite(RELAY6, 0);
digitalWrite(RELAY7, 1);
digitalWrite(RELAY8, 0);

}
/////////////////////////////////////////////// STOP /////////////////////////////////////////////////////

else if (state == 'd') {
digitalWrite(RELAY5, 1);
digitalWrite(RELAY6, 1);
digitalWrite(RELAY7, 1);
digitalWrite(RELAY8, 1);
}
////////////////////////////////// Left To Right Putt UP Direction /////////////////////////////////////////

if (state == 'E') {
digitalWrite(RELAY1, 0);
digitalWrite(RELAY2, 1);
digitalWrite(RELAY7, 0);
digitalWrite(RELAY8, 1);
}
////////////////////////////////////////////// STOP ///////////////////////////////////////////////////////

else if (state == 'e') {
digitalWrite(RELAY1, 1);
digitalWrite(RELAY2, 1);
digitalWrite(RELAY7, 1);
digitalWrite(RELAY8, 1);
}
////////////////////////////////// Left To Right Putt DOWN Direction //////////////////////////////////////

if (state == 'F') {
digitalWrite(RELAY1, 1);
digitalWrite(RELAY2, 0);
digitalWrite(RELAY7, 1);
digitalWrite(RELAY8, 0);
}
////////////////////////////////////////////// STOP //////////////////////////////////////////////////////

else if (state == 'f') {
digitalWrite(RELAY1, 1);
digitalWrite(RELAY2, 1);
digitalWrite(RELAY7, 1);
digitalWrite(RELAY8, 1);
}
////////////////////////////////// Right To Left Putt UP Direction ///////////////////////////////////////

if (state == 'G') {
digitalWrite(RELAY3, 0);
digitalWrite(RELAY4, 1);
digitalWrite(RELAY5, 0);
digitalWrite(RELAY6, 1);
}
////////////////////////////////////////////// STOP //////////////////////////////////////////////////////

else if (state == 'g') {
digitalWrite(RELAY3, 1);
digitalWrite(RELAY4, 1);
digitalWrite(RELAY5, 1);
digitalWrite(RELAY6, 1);
}
////////////////////////////////// Right To Left Putt DOWN Direction /////////////////////////////////////

if (state == 'H') {
digitalWrite(RELAY3, 1);
digitalWrite(RELAY4, 0);
digitalWrite(RELAY5, 1);
digitalWrite(RELAY6, 0);
}
/////////////////////////////////////////////// STOP /////////////////////////////////////////////////////

else if (state == 'h') {
digitalWrite(RELAY3, 1);
digitalWrite(RELAY4, 1);
digitalWrite(RELAY5, 1);
digitalWrite(RELAY6, 1);
}
////////////////////////////////// Double Break Right To Left Putt UP Direction //////////////////////////

if (state == 'I') {
digitalWrite(RELAY3, 0);
digitalWrite(RELAY4, 1);
digitalWrite(RELAY7, 0);
digitalWrite(RELAY8, 1);
}
////////////////////////////////////////////// STOP //////////////////////////////////////////////////////

else if (state == 'i') {
digitalWrite(RELAY3, 1);
digitalWrite(RELAY4, 1);
digitalWrite(RELAY7, 1);
digitalWrite(RELAY8, 1);
}
////////////////////////////////// Double Break Right To Left Putt DOWN Direction ////////////////////////

if (state == 'J') {
digitalWrite(RELAY3, 1);
digitalWrite(RELAY4, 0);
digitalWrite(RELAY7, 1);
digitalWrite(RELAY8, 0);
}
/////////////////////////////////////////////// STOP /////////////////////////////////////////////////////

else if (state == 'j') {
digitalWrite(RELAY3, 1);
digitalWrite(RELAY4, 1);
digitalWrite(RELAY7, 1);
digitalWrite(RELAY8, 1);
}
////////////////////////////////// Double Break Left To Right UP Direction ///////////////////////////////

if (state == 'K') {
digitalWrite(RELAY1, 0);
digitalWrite(RELAY2, 1);
digitalWrite(RELAY5, 0);
digitalWrite(RELAY6, 1);
}
////////////////////////////////////////////// STOP //////////////////////////////////////////////////////

else if (state == 'k') {
digitalWrite(RELAY1, 1);
digitalWrite(RELAY2, 1);
digitalWrite(RELAY5, 1);
digitalWrite(RELAY6, 1);
}
////////////////////////////////// Double Break Left To Right UP Direction ///////////////////////////////

if (state == 'L') {
digitalWrite(RELAY1, 1);
digitalWrite(RELAY2, 0);
digitalWrite(RELAY5, 1);
digitalWrite(RELAY6, 0);
}
/////////////////////////////////////////////// STOP /////////////////////////////////////////////////////

else if (state == 'l') {
digitalWrite(RELAY1, 1);
digitalWrite(RELAY2, 1);
digitalWrite(RELAY5, 1);
digitalWrite(RELAY6, 1);
}

delay(100);
}
}

could you test the state of the Serial connection with if(Serial)

  {
    pinMode(RELAY1, OUTPUT);
    pinMode(RELAY2, OUTPUT);
    pinMode(RELAY3, OUTPUT);
    pinMode(RELAY4, OUTPUT);
    pinMode(RELAY5, OUTPUT);
    pinMode(RELAY6, OUTPUT);
    pinMode(RELAY7, OUTPUT);
    pinMode(RELAY8, OUTPUT);
  }

Useless curly braces annoy me no end. Why do you have them?

        state = Serial.read();

The read() method returns an int so that it can tell you that there was nothing to read, of that is the case. Since you properly call this function only after checking that there is something to read, the value that you get will be in the range 0 to 255. Why do you need an int to store that?

Why do you diddle with the state of the pins when there is no new serial data to read? The pins do NOT need to be told repeatedly to stay in some position.

What does "disconnect the bluetooth" mean? You aren't physically unplugging hardware while the Arduino is running, are you?

If you are doing something in the app to "disconnect the bluetooth", then perhaps that app is sending data you are not expecting it to send.

Hi PaulS

Thank you for your reply as I am new to coding Arduino I will try and reply to the best of my limited knowdlege so please be gentle!!!.

The code I used was modified from an example on the Roboremo website. If I take out the int state; the code does not work(I think you are telling me I should just use serial read i.e if (Serial.read == 'A')???.

Having looked at the code again following your comments I have indeed taken some code out which set the pins to low(although as I now understand these were already set to low).

What I meant by disconnect the bluetooth is by closing the bluetooth on the app or when I close the app the bluetooth connection is lost and this sets relays 1,3,5 & 7 to high. But I want all relays to be set to low on disconnect.

Unless I am missing something simple then perhaps you are right in thinking that the app is sending some form of data on exit.

My understanding from the Roboremo app is that each button command is followed by" \n" therefore I thought if I wrote the Arduino code as below this would solve it, but this did not work.

if (state == 'disconnect') {

digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
digitalWrite(9, 1);

{

Many Thanks
Andy

when the connection is lost could the board keep reseting and executing the setup() function?
would if(Serial) detect this and you can set the outputs low?

If I take out the int state; the code does not work(I think you are telling me I should just use serial read i.e if (Serial.read == 'A')???.

No. I am saying that the type of state should be a char or a byte.

What I meant by disconnect the bluetooth is by closing the bluetooth on the app or when I close the app the bluetooth connection is lost and this sets relays 1,3,5 & 7 to high. But I want all relays to be set to low on disconnect.

So. it is likely that closing the app, or the bluetooth connection, sends some data that you are not expecting. "Good night; sweet dreams" could cause you all kinds of grief.

You should get your bluetooth device off of the hardware serial pins, and use an instance of SoftwareSerial to read from/write to it. Then, you can use the hardware serial pins, and Serial, to talk to the PC, telling it what you received from the bluetooth device. Perhaps a clue will present itself.

Hi All

Thank you for your help I have now resolved the issue and everything working great.

Andy