HC05 and KEY

hi
in my project i want to turn on the two relay at a time
once when the key is pressed
secondly when ever i send the data code from blue tooth

i use this code which each one work perfect but i copied together just the BT section is working
i want the program check the connection to BT and key and any of them is working fine do the needed action below is my used code

 #include <IRLibAll.h>

#define MY_PROTOCOL NEC
#define LAMP_ON   0xFF30CF  //Move several clockwise
#define LEFT_ARROW    0xfd10ef //Move servo counterclockwise
#define SELECT_BUTTON 0xfd906f //Center the servo
#define UP_ARROW      0xfda05f //Increased number of degrees servo moves
#define DOWN_ARROW    0xfdb04f //Decrease number of degrees servo moves
#define BUTTON_0 0xFFB04F  //Pushing buttons 0-9 moves to fixed positions
#define BUTTON_1 0xFF30CF  // each 20 degrees greater
#define BUTTON_2 0xFF18E7
#define BUTTON_3 0xFF7A85
#define BUTTON_4 0xFF10EF
#define BUTTON_5 0xFF38C7 

IRrecv myReceiver(7); //pin number for the receiver
IRdecode myDecoder;
uint32_t Previous;//handles NEC repeat codes


//*************************
const int buttonPin = 7;    // the number of the pushbutton pin
const int relay_1 = 3;      // the number of the LED pin
const int relay_2 = 4;      //User2
const int ledpin3=11; // led on D13 will show  Touch is Run

// Variables will change:
int RState = LOW;         // the current state of the output pin
int L3State = LOW;         // the current state of the output pin
int LState = LOW;         // the current state of the output pin

#define TouchSensor 7 // Pin for capactitive touch sensor

   // boolean currentState = LOW;
boolean currentState = HIGH;
    
    boolean lastState = LOW;
    boolean RelayState = LOW;
  //************************************************
const int ledpin1=13; // led on D13 will show Alarm
const int ledpin2=12; // led on D13 will show BT is going ON
//const int ledpin3=11; // led on D13 will show  Touch is Run
const int ledpin4=10; // led on D13 will show  User1
const int ledpin5=9; // led on D13 will show User2
//************************************************
 int vv;// state of voltage 
 int R;// state of user1
 int L;// state of User2
//************************************************
    //int relay = 13; 
  //  int relay_1 = 3; // User 1
  //  int relay_2 = 4; //User2
//************************************************
char data = 0;            //Variable for storing received data


void setup()
{
  pinMode(buttonPin, INPUT);
  //***************************
 pinMode(ledpin1,OUTPUT);
  pinMode(ledpin2,OUTPUT);
 pinMode(ledpin3,OUTPUT);
 pinMode(ledpin4,OUTPUT);
 pinMode(ledpin5,OUTPUT);
//***************************
  pinMode(relay_1, OUTPUT);
  pinMode(relay_2, OUTPUT);
   pinMode(TouchSensor, INPUT);
 //**************************************
    Serial.begin(9600);   //Sets the baud for serial data transmission                               
   
 //**************************************
 
 
   digitalWrite(ledpin1, LOW);
   digitalWrite(ledpin2, LOW);
   digitalWrite(ledpin3, LOW);
   digitalWrite(ledpin4, LOW);
   digitalWrite(ledpin5, LOW);


   
   digitalWrite(relay_1, LOW);
  digitalWrite(relay_2, LOW);
  //***************************************
   digitalWrite(relay_1, RState);
   digitalWrite(relay_2, LState);
    digitalWrite(ledpin3, L3State);
    
      myReceiver.enableIRIn(); // Start the receiver
}
void loop()
{
   String str = "~";
    String str88 = " #";
    String strv = ",";  
    String str0 = "V=";
    String str1 = "R=";
    String str2 = "L=";    
// Read values from the sensor
//    Serial.print(  +strv+round(sht1x.readHumidity())+strv+analogRead(A0)+strv+str); 
  //***********Voltage**************
    int voltagesam = analogRead(A0);
    if (voltagesam >200){
    digitalWrite(ledpin1, LOW); 
       }
      else {
        digitalWrite(ledpin1, HIGH); 
        } 
 
 //***************IR KEY *****************************       
while (myReceiver.getResults()) {
   
       myDecoder.decode();
       if(myDecoder.protocolNum==MY_PROTOCOL) {
         if(myDecoder.value==0xFFFFFFFF)
           myDecoder.value=Previous;
         switch(myDecoder.value) {
          
            case BUTTON_0: 
                          digitalWrite(ledpin3, LOW);
                          digitalWrite(relay_1, LOW);
                          digitalWrite(relay_2, LOW);
                          break;
            case BUTTON_1:
                          digitalWrite(ledpin3, HIGH);
                          digitalWrite(relay_1, HIGH);
                          digitalWrite(relay_2, HIGH);
                          break;
           
         } }
       myReceiver.enableIRIn();
    }
 
    if (Serial.available() < 1) return; // if serial empty, return to loop().

   
//*****************************************
  if (Serial.available() > 0 )      // Send data only when you receive data:
//(Serial.available() > 0 )      // Send data only when you receive data:
   {
     Serial.print("BT");
        data = Serial.read();        //Read the incoming data & store into data

     digitalWrite(ledpin2, HIGH);
     //   Serial.print(data);          //Print Value inside data in Serial monitor
          
      if(data == '1')              // Checks whether value of data is equal to 1
        { 
      digitalWrite( relay_1, HIGH);
      RState =HIGH;
          R=1;
          } 
      else if(data == '2')         //  Checks whether value of data is equal to 0
       { 
          digitalWrite( relay_1, LOW);
             RState =LOW;
        R=0;
         digitalWrite(ledpin2, LOW);

        }       
 
 if(data == '3')              // Checks whether value of data is equal to 1
        { 
         digitalWrite( relay_2, HIGH); L=1; 
         LState =HIGH;
            } 
      else if(data == '4')         //  Checks whether value of data is equal to 0
       { 
          digitalWrite(relay_2, LOW); L=0; 
        digitalWrite(ledpin2, LOW);
        LState =LOW;
        }  
     //Serial.println("R:"+R);Serial.println("L:"+L); 

  else if(data == '5')         //  Checks whether value of data is equal to 0
       {
  
//*********************status reading *********************** 
    int v = digitalRead(ledpin1);
     int r1 = digitalRead(relay_1);
     int r2 = digitalRead(relay_2);
    Serial.println(str0+v+strv+str1+r1+strv+str2+r2+strv);
      delay(10);        // delay in between reads for stability
//******************
  }
   } 
       if (Serial.available() < 1) return; // if serial empty, return to loop().

}

hi
in my project i want to turn on the two realy at a time
once when the key is pereee
secondly when ever i send the data code from blue tooth

i use this code which each one work perfect but i copied together just the BT sectiion is working
i want the program check the connection to BT and key and any of them is working fine do the needed action below is my used code
code
any help appreciated in advanced

#include <IRLibAll.h>

#define MY_PROTOCOL NEC
#define LAMP_ON   0xFF30CF  //Move several clockwise
#define LEFT_ARROW    0xfd10ef //Move servo counterclockwise
#define SELECT_BUTTON 0xfd906f //Center the servo
#define UP_ARROW      0xfda05f //Increased number of degrees servo moves
#define DOWN_ARROW    0xfdb04f //Decrease number of degrees servo moves
#define BUTTON_0 0xFFB04F  //Pushing buttons 0-9 moves to fixed positions
#define BUTTON_1 0xFF30CF  // each 20 degrees greater
#define BUTTON_2 0xFF18E7
#define BUTTON_3 0xFF7A85
#define BUTTON_4 0xFF10EF
#define BUTTON_5 0xFF38C7 

IRrecv myReceiver(7); //pin number for the receiver
IRdecode myDecoder;
uint32_t Previous;//handles NEC repeat codes


//*************************
const int buttonPin = 7;    // the number of the pushbutton pin
const int relay_1 = 3;      // the number of the LED pin
const int relay_2 = 4;      //User2
const int ledpin3=11; // led on D13 will show  Touch is Run

// Variables will change:
int RState = LOW;         // the current state of the output pin
int L3State = LOW;         // the current state of the output pin
int LState = LOW;         // the current state of the output pin

#define TouchSensor 7 // Pin for capactitive touch sensor

   // boolean currentState = LOW;
boolean currentState = HIGH;
    
    boolean lastState = LOW;
    boolean RelayState = LOW;
  //************************************************
const int ledpin1=13; // led on D13 will show Alarm
const int ledpin2=12; // led on D13 will show BT is going ON
//const int ledpin3=11; // led on D13 will show  Touch is Run
const int ledpin4=10; // led on D13 will show  User1
const int ledpin5=9; // led on D13 will show User2
//************************************************
 int vv;// state of voltage 
 int R;// state of user1
 int L;// state of User2
//************************************************
    //int relay = 13; 
  //  int relay_1 = 3; // User 1
  //  int relay_2 = 4; //User2
//************************************************
char data = 0;            //Variable for storing received data


void setup()
{
  pinMode(buttonPin, INPUT);
  //***************************
 pinMode(ledpin1,OUTPUT);
  pinMode(ledpin2,OUTPUT);
 pinMode(ledpin3,OUTPUT);
 pinMode(ledpin4,OUTPUT);
 pinMode(ledpin5,OUTPUT);
//***************************
  pinMode(relay_1, OUTPUT);
  pinMode(relay_2, OUTPUT);
   pinMode(TouchSensor, INPUT);
 //**************************************
    Serial.begin(9600);   //Sets the baud for serial data transmission                               
   
 //**************************************
 
 
   digitalWrite(ledpin1, LOW);
   digitalWrite(ledpin2, LOW);
   digitalWrite(ledpin3, LOW);
   digitalWrite(ledpin4, LOW);
   digitalWrite(ledpin5, LOW);


   
   digitalWrite(relay_1, LOW);
  digitalWrite(relay_2, LOW);
  //***************************************
   digitalWrite(relay_1, RState);
   digitalWrite(relay_2, LState);
    digitalWrite(ledpin3, L3State);
    
      myReceiver.enableIRIn(); // Start the receiver
}
void loop()
{
   String str = "~";
    String str88 = " #";
    String strv = ",";  
    String str0 = "V=";
    String str1 = "R=";
    String str2 = "L=";    
// Read values from the sensor
//    Serial.print(  +strv+round(sht1x.readHumidity())+strv+analogRead(A0)+strv+str); 
  //***********Voltage**************
    int voltagesam = analogRead(A0);
    if (voltagesam >200){
    digitalWrite(ledpin1, LOW); 
       }
      else {
        digitalWrite(ledpin1, HIGH); 
        } 
 
 //***************IR KEY *****************************       
while (myReceiver.getResults()) {
   
       myDecoder.decode();
       if(myDecoder.protocolNum==MY_PROTOCOL) {
         if(myDecoder.value==0xFFFFFFFF)
           myDecoder.value=Previous;
         switch(myDecoder.value) {
          
            case BUTTON_0: 
                          digitalWrite(ledpin3, LOW);
                          digitalWrite(relay_1, LOW);
                          digitalWrite(relay_2, LOW);
                          break;
            case BUTTON_1:
                          digitalWrite(ledpin3, HIGH);
                          digitalWrite(relay_1, HIGH);
                          digitalWrite(relay_2, HIGH);
                          break;
           
         } }
       myReceiver.enableIRIn();
    }
 
    if (Serial.available() < 1) return; // if serial empty, return to loop().

   
//*****************************************
  if (Serial.available() > 0 )      // Send data only when you receive data:
//(Serial.available() > 0 )      // Send data only when you receive data:
   {
     Serial.print("BT");
        data = Serial.read();        //Read the incoming data & store into data

     digitalWrite(ledpin2, HIGH);
     //   Serial.print(data);          //Print Value inside data in Serial monitor
          
      if(data == '1')              // Checks whether value of data is equal to 1
        { 
      digitalWrite( relay_1, HIGH);
      RState =HIGH;
          R=1;
          } 
      else if(data == '2')         //  Checks whether value of data is equal to 0
       { 
          digitalWrite( relay_1, LOW);
             RState =LOW;
        R=0;
         digitalWrite(ledpin2, LOW);

        }       
 
 if(data == '3')              // Checks whether value of data is equal to 1
        { 
         digitalWrite( relay_2, HIGH); L=1; 
         LState =HIGH;
            } 
      else if(data == '4')         //  Checks whether value of data is equal to 0
       { 
          digitalWrite(relay_2, LOW); L=0; 
        digitalWrite(ledpin2, LOW);
        LState =LOW;
        }  
     //Serial.println("R:"+R);Serial.println("L:"+L); 

  else if(data == '5')         //  Checks whether value of data is equal to 0
       {
  
//*********************status reading *********************** 
    int v = digitalRead(ledpin1);
     int r1 = digitalRead(relay_1);
     int r2 = digitalRead(relay_2);
    Serial.println(str0+v+strv+str1+r1+strv+str2+r2+strv);
      delay(10);        // delay in between reads for stability
//******************
  }
   } 
       if (Serial.available() < 1) return; // if serial empty, return to loop().

}

any HELP?????????

any help?

Where is the button connected?

You seem to set up the IR receiver on pin 7 but also use it as a digital input- not sure if that is part of the problem.

If you comment out all the bluetooth section does it work again?

thanks for your kindly email yes i have been used the IR sensor,as the key as i mentioned before the key section( IR sensor) and BT section are work separately but i have a problem when i join them

There seems to be more to the code than what you have described and i find it hard to see what you are trying to achieve but two lines use the same pin number:

const int buttonPin = 7; // the number of the pushbutton pin

and

define TouchSensor 7 // Pin for capactitive touch sensor

Don't mix "#define"s and constant declarations. I think the current thinking is to use the "const int" instead of defines.

Other than defining this pin and opening it as INPUT (twice; once for each definition) you do not test its state anywhere in the program. Therefore your program will not respond to the pins activation.

Please learn to indent your code properly; use tools->autoformat in the IDE.

Also it's common practice NOT to place two statements on one line; it's too easy to overlook.

Comments are useless if they don't reflect what is done; e.g.

    else if (data == '5')        //  Checks whether value of data is equal to 0

I understand where it comes from (copy / paste) but everybody can understand the code till you put the incorrect comments in.

Your BT and IR do different things; from your description, I understand that that's not what you want. Maybe you can rephrase.

Next, if you want your bluetooth and IR to do the same, I would advise to use an additional variable to save the 'command' and use that variable and separate the receive logic from the control logic.

Something in the line of

void loop()
{
  // read IR
  ...
  ...
  if IR command received
  {
    // set command variable
    ...
    ...
  }

  // read BT
  if BT command received
  {
    // set command variable
    ...
    ...
  }


  // action command
  switch(command)
  {
    ...
    ...
  }
}

tnx for your guidline i will try your them and give f/b regards

HI

I HAVE DOE THE CHANGES BUT STILL PROBLEM REMAIN
I WANT TO HAVE THE ir AND BT AT THE TIME MEANS WHENEVER THE SERIAL DATA COME TAKE THE COMMAND AND WHEN THE IR COME THE ACTION HAPPENED

#include <IRLibAll.h>

#define MY_PROTOCOL NEC
#define LAMP_ON   0xFF30CF  //Move several clockwise
#define LEFT_ARROW    0xfd10ef //Move servo counterclockwise
#define SELECT_BUTTON 0xfd906f //Center the servo
#define UP_ARROW      0xfda05f //Increased number of degrees servo moves
#define DOWN_ARROW    0xfdb04f //Decrease number of degrees servo moves
#define BUTTON_0 0xFFB04F  //Pushing buttons 0-9 moves to fixed positions
#define BUTTON_1 0xFF30CF  // each 20 degrees greater
#define BUTTON_2 0xFF18E7
#define BUTTON_3 0xFF7A85
#define BUTTON_4 0xFF10EF
#define BUTTON_5 0xFF38C7 

IRrecv myReceiver(7); //pin number for the receiver
IRdecode myDecoder;
uint32_t Previous;//handles NEC repeat codes
//*************************
const int relay_1 = 3;      // User 1
const int relay_2 = 4;      //User2
const int ledpin3=11; // led on D13 will show  Touch is Run

// Variables will change:
int RState = LOW;         // the current state of the output pin
int L3State = LOW;         // the current state of the output pin
int LState = LOW;         // the current state of the output pin

#define TouchSensor 7 // Pin for capactitive touch sensor

   // boolean currentState = LOW;
boolean currentState = HIGH;
    
    boolean lastState = LOW;
    boolean RelayState = LOW;
 //************************************************
const int ledpin1=13; // led on D13 will show Alarm
const int ledpin2=12; // led on D12 will show BT is going ON
//const int ledpin3=11; // led on D11 will show  Touch is Run
const int ledpin4=10; // led on D10 will show  User1
const int ledpin5=9; // led on D9 will show User2
//************************************************
 int vv;// state of voltage 
 int R;// state of user1
 int L;// state of User2
//************************************************

char data = 0;            //Variable for storing received data
int FDATA=0; 
//************************************************

void setup()
{
 //***************************
 pinMode(ledpin1,OUTPUT);
  pinMode(ledpin2,OUTPUT);
 pinMode(ledpin3,OUTPUT);
 pinMode(ledpin4,OUTPUT);
 pinMode(ledpin5,OUTPUT);
//***************************
  pinMode(relay_1, OUTPUT);
  pinMode(relay_2, OUTPUT);
  pinMode(TouchSensor, INPUT);
 //**************************************
    Serial.begin(9600);   //Sets the baud for serial data transmission       
 //********************************************************************
   digitalWrite(ledpin1, LOW);
   digitalWrite(ledpin2, LOW);
   digitalWrite(ledpin3, LOW);
   digitalWrite(ledpin4, LOW);
   digitalWrite(ledpin5, LOW);   
   digitalWrite(relay_1, LOW);
  digitalWrite(relay_2, LOW);
  //***************************************
   digitalWrite(relay_1, RState);
   digitalWrite(relay_2, LState);
   digitalWrite(ledpin3, L3State);
   myReceiver.enableIRIn(); // Start the receiver
}
void loop()
{
  //***************IR KEY *****************************       
if (myReceiver.getResults()) {
       myDecoder.decode();
       if(myDecoder.protocolNum==MY_PROTOCOL) {
         if(myDecoder.value==0xFFFFFFFF)
           myDecoder.value=Previous;
       else if (myDecoder.value==BUTTON_0) {
                FDATA=0;
                }
       else if (myDecoder.value==BUTTON_1) {
                FDATA=1;
                }
       else{
                  FDATA=2;
                 }
                   
           
         } }
       myReceiver.enableIRIn();

 if (Serial.available() < 1) return; // if serial empty, return to loop().

   //*****************************************
  if (Serial.available() > 0 )      // Send data only when you receive data:
//(Serial.available() > 0 )      // Send data only when you receive data:
   {
     Serial.print("BT");
        data = Serial.read();        //Read the incoming data & store into data

     digitalWrite(ledpin2, HIGH);
     //   Serial.print(data);          //Print Value inside data in Serial monitor
          
      if(data == '0')              // Checks whether value of data is equal to 0
        { 
     FDATA=0;
          } 
      else if(data == '1')         //  Checks whether value of data is equal to 1
       { 
            FDATA=1;
                 }       
  
  else  {
          FDATA=2;
          }
 
  }
 //  if (Serial.available() < 1) return; // if serial empty, return to loop().

//*********************TEST THE CASE **********************************
  switch(FDATA) {
           case 0: 
                  digitalWrite(ledpin3, LOW);// led3 OFF
                  digitalWrite(relay_1, LOW);// USER 1 OFF
                  digitalWrite(relay_2, LOW);// USER 2 OFF
           break;
          case 1:digitalWrite(ledpin3, HIGH);// led3 ON
                   digitalWrite(relay_1, HIGH);// USER 1 ON
                   digitalWrite(relay_2, LOW);// USER 2 ON
                   break; 
           case 2:digitalWrite(ledpin3, HIGH);// led3 ON
                   digitalWrite(relay_1, LOW);// USER 1 OFF
                   digitalWrite(relay_2, HIGH);// USER 2 ON
                   break;
            default:
                  digitalWrite(ledpin3, LOW);// led3 OFF
                  digitalWrite(relay_1, LOW);// USER 1 OFF
                  digitalWrite(relay_2, LOW);// USER 2 OFF
           
         }

    
  }

So my suggestion is that you take the combined code and comment out all the bluetooth code and see if it works. Then gradually uncomment bits until you know where it stops working.

hi the problem is that each part work fine but combination not

What happens if you remove everything related to the touch sensor? Both the IR and touch sensor use pin 7; is that intentionally?

I understand that. Please post the individual codes that work so I can see e.g. where that touch sensor comes from; I don't immediately see something wrong with your current code.

Threads merged.