42Views0Replies

Author Options:

Pls help me with my code Answered

I want to make home automation project using arduino. I want to control multiple led, password protected door and temperature showing. I have 3 code for these 3 feature. But I want to combine them together to control it from 1 android device. i just want to make work like that- if i press "Q" command then it will show temperature. if i press "R" command then it will work like multiple led controller. if i press "S" command then it will work like password protected locker.

This is the code for bluetooth led control:-

int relay1 = A0;<br>    int relay2 = A1;
    int relay3 = A2;
    int relay4 = A3;
    
    
    String readString;
    
    void setup() {
      Serial.begin(9600);
      pinMode(relay1, OUTPUT); 
      pinMode(relay2, OUTPUT); 
      pinMode(relay3, OUTPUT); 
      pinMode(relay4, OUTPUT); 
      
    }
    
    void loop() {
      while (Serial.available()) {
        delay(3);  
        char c = Serial.read();
        readString += c; 
      }
      if (readString.length() >0) {
        
        if (readString == "A")     
        {
          digitalWrite(relay1, HIGH);
            Serial.print("Relay 1 is On");
        }
        if (readString == "W")
        {
          digitalWrite(relay1, LOW);
            Serial.print("Relay 1 is Off");
        }
        //relay2
        if (readString == "B")     
        {
          digitalWrite(relay2, HIGH);
            Serial.print("Relay 2 is On");
        }
        if (readString == "X")
        {
          digitalWrite(relay2, LOW);
            Serial.print("Relay 2 is Off");
        }
        //relay3    
        if (readString == "C")     
        {
          digitalWrite(relay3, HIGH);
            Serial.print("Relay 3 is On");
        }
        if (readString == "Y")
        {
          digitalWrite(relay3, LOW);
            Serial.print("Relay 3 is Off");
        }
        //relay4    
        if (readString == "D")     
        {
          digitalWrite(relay4, HIGH);
            Serial.print("Relay 4 is On");
        }
        if (readString == "Z")
        {
          digitalWrite(relay4, LOW);
            Serial.print("Relay 4 is Off");
        }
        
        //All on / off    
        if (readString == "E")     
        {
          digitalWrite(relay1, HIGH);
          digitalWrite(relay2, HIGH);
          digitalWrite(relay3, HIGH);
          digitalWrite(relay4, HIGH);
            Serial.print("All Relay Is On");
         
        }
        if (readString == "F")
        {
          digitalWrite(relay1, LOW);
          digitalWrite(relay2, LOW);
          digitalWrite(relay3, LOW);
          digitalWrite(relay4, LOW);
            Serial.print("All Relay Is Off");
          
        }
        //next
        readString="";
      } 
    }

This is the code for password control door:

String inputString = "";<br>    String command = "";
    String value = "";
    String password = "test123"; //setpassword here
                                
    boolean stringComplete = false; 
    int relay = 2; 
    int greenLed = 5;  
    int redLed = 6;    
    
    void setup(){
      Serial.begin(9600);  // make sure it is the same baudrate as your bluetooth module 
      
      inputString.reserve(50);  
      command.reserve(50);
      value.reserve(50);
      boolean stringOK = false;
      pinMode(relay, OUTPUT);
      digitalWrite(relay, HIGH);
      pinMode(greenLed, OUTPUT);
      pinMode(redLed, OUTPUT);
      digitalWrite(redLed, HIGH);
      
    }
    void loop(){
      
      if (stringComplete) {
        delay(100);
        
        int pos = inputString.indexOf('=');
        
        if (pos > -1) {
          
            command = inputString.substring(0, pos);
          
            value = inputString.substring(pos+1, inputString.length()-1);  // extract command up to \n exluded
           
        if(!password.compareTo(value) && (command == "OPEN")){
              
               openDoor(); 
               Serial.println(" OPEN"); 
               delay(100);
               }
        else if(!password.compareTo(value) && (command == "CLOSE")){
             
               closeDoor();
               Serial.println(" CLOSE"); 
               delay(100);
               }
        else if(password.compareTo(value)){
             
               Serial.println(" WRONG");
               delay(100);
               } 
            } 
         
           inputString = "";
           stringComplete = false;
        }  
    }
    void serialEvent() {
      while (Serial.available()) {
       
        char inChar = (char)Serial.read(); 
        //Serial.write(inChar);
      
        inputString += inChar;
        
        if (inChar == '\n' || inChar == '\r') {
          stringComplete = true;
        } 
      }
    }
    void openDoor(){
       digitalWrite(relay, LOW);
       digitalWrite(redLed, LOW);
       digitalWrite(greenLed, HIGH);
    }
    void closeDoor(){
       digitalWrite(relay, HIGH);
       digitalWrite(redLed, HIGH);
       digitalWrite(greenLed, LOW);
       
    }

This is the code for temperature showing:

int data_pin = 9;<br>    int vcc_pin=8;
    int gnd_pin=10;
    boolean result[41]; //holds the result
    int interval=2000; //Sample every 2 seconds
    unsigned int temp; //in celcius
    unsigned int humidity; //in %RH
    
    void setup() {
    
      //Supply power to DHT11 (Uses upto 2.5mA)
        pinMode(gnd_pin, OUTPUT);
        pinMode(vcc_pin, OUTPUT);
        digitalWrite(gnd_pin,LOW);
        digitalWrite(vcc_pin,HIGH);
        
      //Initiate Serial for Bluetooth Communication
        Serial.begin(9600); 
    }
    
    void loop() {
      //Pause before taking next measurement
        delay(interval); 
     
      //Trigger reading by holding data pin low for 18ms
        pinMode(data_pin, OUTPUT);
        digitalWrite(data_pin,LOW);
        delay(18);
        digitalWrite(data_pin,HIGH);
        pinMode(data_pin, INPUT_PULLUP); 
      
      //read 41 bits of signal
        for(int i=0;i<=40;i++){
          result[i]=(pulseIn(data_pin, HIGH)>40);
        }
    
      //Extract Humidity (from byte 1)
        humidity=0;
        for (int i=1;i<=8;i++){
          humidity=humidity<<1;
          if (result[i]) humidity|=1;
        }
          
      //Extract Temperature (from Byte 3)
        temp=0;
        for (int i=17;i<=24;i++){
          temp=temp<<1;
          if (result[i]) temp|=1;
        }
        
       //Send data over Bluetooth
        Serial.print("*T"+String(temp)+"*");
        Serial.print("*H"+String(humidity)+"*");
    
    }

This is the code i tried to combine all together.i just want to make work like that- if i press "Q" command then it will show temperature. if i press "R" command then it will work like multiple led controller. if i press "S" command then it will work like password protected locker.I tried `if(readString == "Q")` , `if(readString == "R")` , `if(readString == "S")` for control the code seperately but its not working:

int data_pin = 9;<br>    int vcc_pin=8;
    int gnd_pin=10;
    int relay1 = A0;
    int relay2 = A1;
    int relay3 = A2;
    int relay4 = A3;
    int relay = 2; 
    int greenLed = 5;  
    int redLed = 6;  
    boolean result[41]; //holds the result
    int interval=2000; //Sample every 2 seconds
    unsigned int temp; //in celcius
    unsigned int humidity; //in %RH
    String inputString = "";
    String command = "";
    String value = "";
    String password = "test123"; //setpassword here                          
    boolean stringComplete = false; 
    String readString;
    
    void setup() {
    
      //Supply power to DHT11 (Uses upto 2.5mA)
        pinMode(gnd_pin, OUTPUT);
        pinMode(vcc_pin, OUTPUT);
        digitalWrite(gnd_pin,LOW);
        digitalWrite(vcc_pin,HIGH);
          pinMode(relay1, OUTPUT); 
      pinMode(relay2, OUTPUT); 
      pinMode(relay3, OUTPUT); 
      pinMode(relay4, OUTPUT);
        pinMode(relay, OUTPUT);
      digitalWrite(relay, HIGH);
      pinMode(greenLed, OUTPUT);
      pinMode(redLed, OUTPUT);
      digitalWrite(redLed, HIGH);
        inputString.reserve(50);  
      command.reserve(50);
      value.reserve(50);
      boolean stringOK = false;
      //Initiate Serial for Bluetooth Communication
        Serial.begin(9600); 
    }
    
    void loop(){
    
      if (readString == "Q"){
      //Pause before taking next measurement
        delay(interval); 
     
      //Trigger reading by holding data pin low for 18ms
        pinMode(data_pin, OUTPUT);
        digitalWrite(data_pin,LOW);
        delay(18);
        digitalWrite(data_pin,HIGH);
        pinMode(data_pin, INPUT_PULLUP); 
      
      //read 41 bits of signal
        for(int i=0;i<=40;i++){
          result[i]=(pulseIn(data_pin, HIGH)>40);
        }
    
      //Extract Humidity (from byte 1)
        humidity=0;
        for (int i=1;i<=8;i++){
          humidity=humidity<<1;
          if (result[i]) humidity|=1;
        }
          
      //Extract Temperature (from Byte 3)
        temp=0;
        for (int i=17;i<=24;i++){
          temp=temp<<1;
          if (result[i]) temp|=1;
        }
        
       //Send data over Bluetooth
        Serial.print("*T"+String(temp)+"*");
        Serial.print("*H"+String(humidity)+"*");
      }
    
    
    
    
    
    
    if (readString == "R"){
          while (Serial.available()) {
        delay(3);  
        char c = Serial.read();
        readString += c; 
      }
      if (readString.length() >0) {
        
        if (readString == "A")     
        {
          digitalWrite(relay1, HIGH);
            Serial.print("Relay 1 is On");
        }
        if (readString == "W")
        {
          digitalWrite(relay1, LOW);
            Serial.print("Relay 1 is Off");
        }
        //relay2
        if (readString == "B")     
        {
          digitalWrite(relay2, HIGH);
            Serial.print("Relay 2 is On");
        }
        if (readString == "X")
        {
          digitalWrite(relay2, LOW);
            Serial.print("Relay 2 is Off");
        }
        //relay3    
        if (readString == "C")     
        {
          digitalWrite(relay3, HIGH);
            Serial.print("Relay 3 is On");
        }
        if (readString == "Y")
        {
          digitalWrite(relay3, LOW);
            Serial.print("Relay 3 is Off");
        }
        //relay4    
        if (readString == "D")     
        {
          digitalWrite(relay4, HIGH);
            Serial.print("Relay 4 is On");
        }
        if (readString == "Z")
        {
          digitalWrite(relay4, LOW);
            Serial.print("Relay 4 is Off");
        }
        
        //All on / off    
        if (readString == "E")     
        {
          digitalWrite(relay1, HIGH);
          digitalWrite(relay2, HIGH);
          digitalWrite(relay3, HIGH);
          digitalWrite(relay4, HIGH);
            Serial.print("All Relay Is On");
         
        }
        if (readString == "F")
        {
          digitalWrite(relay1, LOW);
          digitalWrite(relay2, LOW);
          digitalWrite(relay3, LOW);
          digitalWrite(relay4, LOW);
            Serial.print("All Relay Is Off");
          
        }
        //next
        readString="";
      } 
    }
    
    
    
    if (readString == "S"){
       if (stringComplete) {
        delay(100);
        
        int pos = inputString.indexOf('=');
        
        if (pos > -1) {
          
            command = inputString.substring(0, pos);
          
            value = inputString.substring(pos+1, inputString.length()-1);  // extract command up to \n exluded
           
        if(!password.compareTo(value) && (command == "OPEN")){
              
               openDoor(); 
               Serial.println(" OPEN"); 
               delay(100);
               }
        else if(!password.compareTo(value) && (command == "CLOSE")){
             
               closeDoor();
               Serial.println(" CLOSE"); 
               delay(100);
               }
        else if(password.compareTo(value)){
             
               Serial.println(" WRONG");
               delay(100);
               } 
            } 
         
           inputString = "";
           stringComplete = false;
        } 
    }
    }
    void serialEvent() {
      while (Serial.available()) {
       
        char inChar = (char)Serial.read(); 
        //Serial.write(inChar);
      
        inputString += inChar;
        
        if (inChar == '\n' || inChar == '\r') {
          stringComplete = true;
        } 
      }
    }
    void openDoor(){
       digitalWrite(relay, LOW);
       digitalWrite(redLed, LOW);
       digitalWrite(greenLed, HIGH);
    }
    void closeDoor(){
       digitalWrite(relay, HIGH);
       digitalWrite(redLed, HIGH);
       digitalWrite(greenLed, LOW);
       
    }

Discussions