Adding Bluetooth Serial Code To My Infared/Relay Build

Hello

I am playing around trying to get an infrared sensor to turn on a relay, like a light using 2 Arduino Unos. There are some great tutorials to do this, but the goal is to make them talk through Bluetooth.

Was able to program the HC-05 Modules to master and slave. They are talking to each other.

Goal is:

Slave Code Send

IR sensor detects (1) ==> Open serial.port 38400 and send (1) via HC-05 Bluetooth Slave

Master Code Receive

Serial.port 38400 message received (1) by HC-05 Bluetooth Master ==> Open Relay for 5 minutes

The fire code works, the relay code works, but where in the world do I insert my bluetooth serial lines?

Your problem is unclear, and why you should have one is even less clear, but, if you have blueteeth talking to each other, you have done all the hard stuff. You say your code works, which may be true but I think it is still suss. You should have the transmitter serial port open continuously, i.e. in setup, and simply print through it when needed in the loop. If it works anyway, all you need do is disconnect the wires and put blueteeth in their place. The thing you have to know is that there is no such thing as bluetooth serial code, they use the same code as any other serial. Essentially bluetooth is "serial without wires."

After posting, I realize it was not clear enough. Here is my slave telling the master to turn on a relay after the IR sensor is triggered. Thoughts? Was convinced I had it!

// Slave Code

#define firesensor 8

int firesensorState = 0;

void setup() {
pinMode(firesensor, INPUT);
Serial.begin(38400); // Bluetooth Communication Rate
}

void loop() {
// firesensor on/off
firesensorState = digitalRead(firesensor);
if (firesensorState == HIGH) {
Serial.write('1');
}
else {
Serial.write('0');
}
}

======================================================

// Master Code

#define relayPin 8

int state = 0;

void setup() {
pinMode (relayPin, OUTPUT);
digitalWrite (relayPin, LOW);
Serial.begin (38400); // Bluetooth Communication Rate
}

void loop(){
if(Serial.available() > 0){ // Checks if Data is available
state = Serial.read(); // Reads Data if available

}
// controlling the relay
if (state == '1') {
digitalWrite (relayPin, HIGH); //relay ON
}
else

if (state == '0') {
digitalWrite (state, LOW); //relay OFF
state = 0;
}

}

Is your bluetooth set to 38400? It's a funny speed to use with hardware serial.
Since you are using hardware serial,

  1. you can have serial monitor connected to see what is going on

  2. you can test the code by connecting the serial ports by wire, thereby having bluetooth out of the game.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

And there is this clever idea if you just want to check for single characters.

...R

Nick_Pyner:
Is your bluetooth set to 38400? It's a funny speed to use with hardware serial.
Since you are using hardware serial,

  1. you can have serial monitor connected to see what is going on

  2. you can test the code by connecting the serial ports by wire, thereby having bluetooth out of the game.

I never thought about connecting them by wire to see if it is working. This would be be via tx/rx, of both boards, together?

I have programmed the Hc-05's to 38400. Maybe the term "Serial code" is not correct, but I am still trying to know why my code isn't sending a simple on/off via Bluetooth with a delay to keep the light on.

The code looks good and checks out. When I hook it up the relay is stuck on and the slave is only showing Rx lights flashing.

I noticed that I do not have a digitalWrite in the slave setting the low value for the firesensor. Is this causing there to be no distinction between on/off=high/low?

Cjohns44:
I never thought about connecting them by wire to see if it is working. This would be be via tx/rx, of both boards, together?

Correct. By using hardware serial, you can prove your code is kosher before you even connect bluetooth, which can do wonders for your confidence. Disconnect Blueteeth and connect Arduinos Rx>Tx and Tx>Rx. Arduino neither knows nor cares what it is connected to, it is just talking to the serial port - hence my comment about bluetooth serial code.

I'm afraid I don't know anything about relays and LEDs but you said the individual codes worked OK, so now is the time to suspect the wiring. Having said that, I would have guessed a pin set high stays high until set low.