Help guys!! (Arduino codes combine)

hello
guys i’m doing home automation with arduino, using relay board (4 channels) controlled by bluetooth and just one of those channels should be controlled with PIR motion sensor.

i’ve got two different codes each of them worked fine but the problem that i tried to combine’em, but that doesn’t work :confused:

Notice: 1 st code to control relay board with bluetooth (Worked)
2 nd code to switch ON/OFF the first channel of the relay board using PIR Motion sensor (Worked)

First code : Bluetooth

#include <SoftwareSerial.h>

SoftwareSerial BT(10, 11); //TX, RX respetively
String device;

void setup() {
BT.begin(9600);
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);

}
//-----------------------------------------------------------------------//
void loop() {
while (BT.available()){ //Check if there is an available byte to read
delay(10); //Delay added to make thing stable
char c = BT.read(); //Conduct a serial read
device += c; //build the string.
}
if (device.length() > 0) {
Serial.println(device);

if(device == “doneon”)
{
digitalWrite(3, HIGH);
}

else if(device == “doneoff”)
{
digitalWrite(3, LOW);
}

else if (device == “dtwoon”)
{
digitalWrite (4,HIGH);
}

else if ( device == “dtwooff”)
{
digitalWrite (4, LOW);
}

else if (device == “dthreeon”)
{
digitalWrite (5, HIGH);

}

else if (device == “dthreeoff”)
{digitalWrite (5, LOW);}

else if (device == “dforeon”)
{
digitalWrite (6, HIGH);

}

else if (device == “dforeoff”)
{digitalWrite (6, LOW);}
device="";}} //Reset the variable

Second code: PIR motion sensor

int irmotionPin = 4; // Pin of IR Motion Sensor
int relayPin = 8; // Pin of Relay Module
void setup(){
Serial.begin(9600);
pinMode(relayPin, OUTPUT); // Set Pin connected to Relay as an OUTPUT
digitalWrite(relayPin, LOW); // Set Pin to LOW to turn Relay OFF
}
void loop(){
while (digitalRead(irmotionPin) == HIGH) { // If Motion detected
digitalWrite(relayPin, HIGH); // Turn Relay ON
Serial.println(“Relay is ON”);
delay(500);
}
digitalWrite(relayPin, LOW); // Turn Relay OFF
Serial.println(“Relay is OFF”);
delay(500);
}

I attached both codes guys

please guys i need help!
sorry for the language.

Relays_Controlled_by_bluetooth__Worked__created_app_.ino (1.04 KB)

relay_with_sensor_and_light.ino (532 Bytes)

It would be more helpful to see your combined program and have a description of any problems.
Seeing delay()s makes me wonder if they caused your problems.

Unfortunately i didn’t save the combined code :confused:
i really don’t know :confused:

Amin1991:
Unfortunately i didn't save the combined code :confused:
i really don't know :confused:

Then combine them again.

aarg:
Then combine them again.

And then post the combined code, but this time use code tags.

yeah i’m working on it bro, i’ll post the combined code when i finish.
sorry what do you mean by code tags ?

Amin1991:
yeah i'm working on it bro, i'll post the combined code when i finish.
sorry what do you mean by code tags ?

Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question ...

This is the combined code guys
same issue the three relay chaneels which controlled by bluetooth worked fine but the on controlled by PIR motion sensor doen’t work!

#include <SoftwareSerial.h>
int irmotionPin = 9;  // Pin of IR Motion Sensor
int relayPin = 6;  // Pin of Relay Module
SoftwareSerial BT(10, 11); //TX, RX respetively
String device;

void setup() {
BT.begin(9600);
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);

pinMode(relayPin, OUTPUT);  // Set Pin connected to Relay as an OUTPUT
digitalWrite(relayPin, LOW);  // Set Pin to LOW to turn Relay OFF


}
//-----------------------------------------------------------------------//
void loop() {
while (BT.available()){ //Check if there is an available byte to read
delay(10); //Delay added to make thing stable
char c = BT.read(); //Conduct a serial read
device += c; //build the string.
}
if (device.length() > 0) {
Serial.println(device);

if(device == "doneon")
{
digitalWrite(3, HIGH);
}

else if(device == "doneoff")
{
digitalWrite(3, LOW);
}

else if (device == "dtwoon")
{
digitalWrite (4,HIGH);
}

else if ( device == "dtwooff")
{
digitalWrite (4, LOW);
}

else if (device == "dthreeon")
{
digitalWrite (5, HIGH);

}

else if (device == "dthreeoff")
{digitalWrite (5, LOW);
}
else if (digitalRead(irmotionPin) == HIGH) {
digitalWrite(relayPin, HIGH);  // Turn Relay ON
Serial.println("Relay is ON");
delay(500);
}
digitalWrite(relayPin, LOW);  // Turn Relay OFF
Serial.println("Relay is OFF");
delay(500);
}

device="";} //Reset the variable

Sorry for not using code tags in the previous post
thx for aarg for post those "How to… "

Amin1991:
Sorry for not using code tags in the previous post
thx for aarg for post those "How to... "

No problem. They are also always available as one of the first three posts in the forum.

any one hv solution for this guys? still trying but nothing changed :confused:

Can you do us, and yourself, a favour, and use the auto-format took before posting code?

ok bro i'll do it
thx

The if statement that checks the PIR pin is nested inside the bluetooth receive block. So it never gets checked unless bluetooth data is coming in.

Also:
There are many other things that programmers do to make their code understandable. Please do them, as a courtesy to the members who volunteer their time to help you here. One is to use a standard indentation to clearly show the code blocks. Never put more than one statement per line. Place any brackets by themselves on a separate line. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Another is to give things descriptive names. You can name numerical constants, pin numbers, variables and many other things in this way. For example, you can refer to a pin and an output level by number, like digitalWrite(3,0). But such a statement doesn't reveal anything about the purpose. digitalWrite(hornRelayPin, LOW) does. You can do that by declaring const byte hornRelayPin = 3; before setup() in your program. Many such names are already defined for you by the compiler and the Arduino IDE. Here are some:

#define HIGH 0x1
#define LOW  0x0
#define PI 3.1415926535897932384626433832795

Use them. There are many more. Use compiler math to compute values so you can see where they came from (or at least document them). For example, if you see the number 73, you would be hard put to explain the significance of it. But if you see "daysPerYear/5", it is obvious. One more thing. When you work on program continuously, you become familiar with it. So many things seem obvious even if they are not spelled out explicitly. But try looking at your own code six months later. It will be as if a stranger wrote it. So write for strangers, not yourself.