HC-05 Arduino Android ON/OFF Led

print

Android code https://github.com/shadowmanpat/arduino-android


#include <SoftwareSerial.h>
SoftwareSerial BTserial(8, 9); // RX | TX
 
const long baudRate = 9600; 
//char c=' ';
boolean NL = true;
 int LED = LED_BUILTIN; 

int index = 0; 
char data[10]; 
char c; 
boolean flag = false;
 
void setup() 
{
    Serial.begin(9600);
    Serial.print("Sketch:   ");   Serial.println(__FILE__);
    Serial.print("Uploaded: ");   Serial.println(__DATE__);
    Serial.println(" ");
 
    BTserial.begin(baudRate);  
    Serial.print("BTserial started at "); Serial.println(baudRate);
    Serial.println(" ");
}
void processCommand(){ 
 char command = data[0]; 
 char inst = data[1]; 
 switch(command){ 
  
   case 'Y': 
        digitalWrite(LED_BUILTIN,HIGH); 
          Serial.println("Light: ON"); 
           BTserial.write("mLight: ON");
   break; 
    case 'N': 
          digitalWrite(LED_BUILTIN,LOW); 
           Serial.println("Light: OFF"); 
           BTserial.write("mLight: OFF");
   break; 
 } 
}
 
void loop()
{

 
    // Read from the Bluetooth module and send to the Arduino Serial Monitor
    if (BTserial.available())
    {

           while(BTserial.available() > 0){ 
              c = BTserial.read(); 
              delay(10); //Delay required 
              data[index] = c; 
              index++; 
         } 
         Serial.println(c);
         data[index] = '\0'; 
         flag = true; 
           if(flag){ 
              processCommand(); 
              flag = false; 
              index = 0; 
              data[0] = '\0'; 
        } 

 
    }
 
 
    // Read from the Serial Monitor and send to the Bluetooth module
    if (Serial.available())
    {
        c = Serial.read();
        BTserial.write(c);   
 
        // Echo the user input to the main window. The ">" character indicates the user entered text.
        if (NL) { Serial.print(">");  NL = false; }
        Serial.write(c);
        if (c==10) { NL = true; }
    }
 
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.