Bluetooth queston

Arduino Uno
HC05 bluetooth
8 channel relay board

I'm trying to implement bluetooth into my code. Everything works using the buttons, but I don't get anything from bluetooth and I think the problem may be in my app. Can someone tell me if the code looks correct. I did verify the tx/rx wiring is correct, not hooked together tx/tx. The first "if" statement is the one for bluetooth.

/*
Char data = 0;
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
}

void loop()
{
if(Serial.available()>0)
{
data = Serial.read();
Serial.print(data);
Serial.print(“\n”);
if(data == '1')
digitalWrite (13, HIGH);
else if (data == '0')
digitalWrite (13, LOW);
}
}
*/

int button1 = 14;
int button2 = 15;
int limitOpen = 2;
int limitDeploy = 3;
int limitDown = 4;
int limitUp = 5;
int limitStow = 6;
int limitClose = 7;
int Open = 8;
int Close = 9;
int deploy = 10;
int stow = 11;
int down = 12;
int up = 13;
char data = 0;


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode (button1, INPUT_PULLUP);
pinMode (button2, INPUT_PULLUP);
pinMode (limitOpen, INPUT_PULLUP);
pinMode (limitDeploy, INPUT_PULLUP);
pinMode (limitDown, INPUT_PULLUP);
pinMode (limitUp, INPUT_PULLUP);
pinMode (limitStow, INPUT_PULLUP);
pinMode (limitClose, INPUT_PULLUP);
pinMode (Open, OUTPUT);
pinMode (Close, OUTPUT);
pinMode (deploy, OUTPUT);
pinMode (stow, OUTPUT);
pinMode (down, OUTPUT);
pinMode (up, OUTPUT);
}

void loop() {

  // put your main code here, to run repeatedly:

if(Serial.available()>0)
{
data = Serial.read();
Serial.print(data);

if(data == '0')
{
Down();
}
else if (data == '1')
{ 
Up();
}
//else
//{
 //for (int i = 8; i <= 13; i++){
  // all outputs (8-13) off (relay board is active low)
//   digitalWrite(i, HIGH); 
//}
//}
}

else if (digitalRead(button1) == HIGH)
{
Down();
}
else if (digitalRead(button2) == HIGH)
{ 
Up();
}
else
{
 for (int i = 8; i <= 13; i++){
  // all outputs (8-13) off (relay board is active low)
   digitalWrite(i, HIGH); 
}
}
}
  
void Down()  {
if (digitalRead(limitOpen)==LOW && digitalRead(limitDeploy)==LOW && digitalRead(limitDown)==LOW)
 {
 digitalWrite (Open, LOW);
 } 
 else if (digitalRead(limitOpen)==HIGH && digitalRead(limitDeploy)==LOW && digitalRead(limitDown) == LOW)
  {
  digitalWrite (Open, HIGH);
  digitalWrite (deploy, LOW);
  }
  else if (digitalRead(limitOpen)==HIGH && digitalRead(limitDeploy)==HIGH && digitalRead(limitDown) == LOW)
  {
  digitalWrite (deploy, HIGH);
  digitalWrite (down, LOW);
  }
  else if (digitalRead(limitOpen)==HIGH && digitalRead(limitDeploy)==HIGH && digitalRead(limitDown) == LOW)
  {
  digitalWrite (deploy, HIGH);
  digitalWrite (down, LOW);
  }
}

void Up() {
if (digitalRead(limitUp)==LOW && digitalRead(limitStow)==LOW && digitalRead(limitClose) == LOW)
  {
  digitalWrite (up, LOW);
  }
  else if (digitalRead(limitUp)==HIGH && digitalRead(limitStow)==LOW && digitalRead(limitClose) == LOW)
 {
  digitalWrite (up, HIGH);
  digitalWrite (stow, LOW);
 }
  else if (digitalRead(limitUp)==HIGH && digitalRead(limitStow) == HIGH && digitalRead(limitClose) == LOW)
  {
  digitalWrite (stow, HIGH);
  digitalWrite (Close, LOW);
  }
}

Can't see anything obvious but here's my first code for testing Bluetooth.

I used an int and a switch state where you have used a char and an if(state== .....

I can tell you this code works and turns a LED on and off as well as sending a comment back to the android terminal I was using it with. Try it and you will prove your BT connection etc.

#define     GREEN  2                 // lcd GREEN on pin 2
#include    "pins_arduino.h"
int state = 0;

void setup() 
{
    pinMode(GREEN, OUTPUT);
    digitalWrite(GREEN, HIGH);
    Serial.begin(9600); // Default connection rate for my BT module
}

void loop() {
    //if some data is sent, read it and save it in the state variable
    if(Serial.available() > 0)
    {
        state = Serial.read();

        switch (state)
        {
    
            case '0':
                Serial.println("Jeez, a zero! thanks buddy"); 
                digitalWrite(GREEN, LOW);
                break;
        
            case '1': 
                Serial.println("ah, a 1, as in want 1? well OK then, I'll have a pint of guiness");
                digitalWrite(GREEN, HIGH);
            break;

                   
        }
         
   }
}

Good luck, Steve.

You have 2 void setup and 2 void loop. Don't do that.

The code in the Original Post seems to have two setup() functions - how can that work?

What pins is the Bluetooth module connected to?

It will be easier to debug your program if you use SoftwareSerial to create an extra serial port for the HC05 and leave the HardwareSerial port for messages to the Serial Monitor (and for uploading programs).

Have a look at the examples in Serial Input Basics

...R

Robin2:
The code in the Original Post seems to have two setup() functions

Oops, how did I miss that? I was concentrating on the lines of code not the function names!

Well spotted both of you.

The first void () and setup () functions are between /**/ so they are not read. I used this example to pull code from and should have erased it when I was done. I did test the original code with just an led and it worked through bluetooth.

The code is setup to use a momentary switch so the "Up" "Down" functions are only called while that button is pressed. It works with the buttons. It may be that my app is only sending one byte and not acting as a momentary. In this case the code could be resetting to zero faster than the led can blink?

As @Robin suggested you can use Software Serial and bluetooth module can connect to it.

Then write whatever you receive from Software serial to Hardware Serial. This way you can monitor what data is coming from Bluetooth module via Serial monitor.

Also before posting, remove unwanted commented codes to avoid any confusions.

Thank you everyone! I was able to confirm that bluetooth is working by using another app in my phone. Since the app only sends the command once I had to remove the "else" code that reset everything when no button was pressed in order to verify. Now I just have to figure out how to make an app that behaves as a momentary switch. I will try the software serial method once I get it working as is.

Less confusing code posted below,

int button1 = 14;
int button2 = 15;
int limitOpen = 2;
int limitDeploy = 3;
int limitDown = 4;
int limitUp = 5;
int limitStow = 6;
int limitClose = 7;
int Open = 8;
int Close = 9;
int deploy = 10;
int stow = 11;
int down = 12;
int up = 13;
char data = 0;


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode (button1, INPUT_PULLUP);
pinMode (button2, INPUT_PULLUP);
pinMode (limitOpen, INPUT_PULLUP);
pinMode (limitDeploy, INPUT_PULLUP);
pinMode (limitDown, INPUT_PULLUP);
pinMode (limitUp, INPUT_PULLUP);
pinMode (limitStow, INPUT_PULLUP);
pinMode (limitClose, INPUT_PULLUP);
pinMode (Open, OUTPUT);
pinMode (Close, OUTPUT);
pinMode (deploy, OUTPUT);
pinMode (stow, OUTPUT);
pinMode (down, OUTPUT);
pinMode (up, OUTPUT);
}

void loop() {

  // put your main code here, to run repeatedly:

if(Serial.available()>0)
{
data = Serial.read();

if(data == '0')
{
digitalWrite (Open, HIGH);
//Down();
}
else if (data == '1')
{ 
digitalWrite (deploy, HIGH);
//Up();
}

}

else if (digitalRead(button1) == HIGH)
{
Down();
}
else if (digitalRead(button2) == HIGH)
{ 
Up();
}

}
  
void Down()  {
if (digitalRead(limitOpen)==LOW && digitalRead(limitDeploy)==LOW && digitalRead(limitDown)==LOW)
 {
 digitalWrite (Open, LOW);
 } 
 else if (digitalRead(limitOpen)==HIGH && digitalRead(limitDeploy)==LOW && digitalRead(limitDown) == LOW)
  {
  digitalWrite (Open, HIGH);
  digitalWrite (deploy, LOW);
  }
  else if (digitalRead(limitOpen)==HIGH && digitalRead(limitDeploy)==HIGH && digitalRead(limitDown) == LOW)
  {
  digitalWrite (deploy, HIGH);
  digitalWrite (down, LOW);
  }
  else if (digitalRead(limitOpen)==HIGH && digitalRead(limitDeploy)==HIGH && digitalRead(limitDown) == LOW)
  {
  digitalWrite (deploy, HIGH);
  digitalWrite (down, LOW);
  }
}

void Up() {
if (digitalRead(limitUp)==LOW && digitalRead(limitStow)==LOW && digitalRead(limitClose) == LOW)
  {
  digitalWrite (up, LOW);
  }
  else if (digitalRead(limitUp)==HIGH && digitalRead(limitStow)==LOW && digitalRead(limitClose) == LOW)
 {
  digitalWrite (up, HIGH);
  digitalWrite (stow, LOW);
 }
  else if (digitalRead(limitUp)==HIGH && digitalRead(limitStow) == HIGH && digitalRead(limitClose) == LOW)
  {
  digitalWrite (stow, HIGH);
  digitalWrite (Close, LOW);
  }
}