CONTROLING LED and Sent Data Sensor VIA BLUETOOTH

Excusse me…

PLAN MY WORK >> I want to controlling LED in slave Bluetooth from PushButton Master Bluetooth. Then there are Sensor Data on a Slave Bluetooth that will be sent to a master.

PROBLEM >> My LED can’t turn ON. but My signal form Master it has been received by slave.

This My Code for that [SLAVE PROGRAM]

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11);
 
boolean DEBUG = true;

#define ledPin1 2
#define ledPin2 3
#define ledPin3 4

 
// Variables used for incoming data
const byte maxDataLength = 20;
char receivedChars[21] ;
boolean newData = false;

int jarak1, jarak2, jarak3, jarak4; // 4 sensor DISTANCE 

double y1 = 0, x1 = 0, y1_1= 0, y2 = 0, x2 = 0, y2_1= 0;
double  y3 = 0, x3 = 0, y3_1 = 0, y4 = 0, x4 = 0, y4_1= 0;
const double a = 0.1; 



 
void setup()  
{
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
    if (DEBUG)
    {
        // open serial communication for debugging
        Serial.begin(9600);
        Serial.println(__FILE__);
        Serial.println(" ");
    }
 
    //  open a software serial connection to the Bluetooth module.
    BTSerial.begin(38400); 
    if (DEBUG)  {   Serial.println(F("AltSoftSerial started at 9600"));     }
    newData = false;
 
} 
 
void loop()  
{
    recvWithStartEndMarkers(); 
    if (newData)  {   processCommand();  }    
}
 
 
int getjarak1()
{
   //Sensor1
  int sen_val1 = analogRead(A0);
  x1 = sen_val1  * 5.0/1023.0;
  y1= a * x1 + (1 - a) * y1_1; 

   delay(50);
  y1_1 = y1;
     
    //Sensor 1
if(y1>=1.6 && y1<2.84)  {
 jarak1 = 66.139-(y1/0.0603); //sensor3 20-40 
}
 else if(y1>=1.27 && y1<1.6)  { 
 jarak1 = 89.876-(y1/0.0322); //sensor3 40-50 
}
 else if(y1>=0.9 && y1<1.27) {
 jarak1 = 118.925-(y1/0.0186); //sensor3 50-70
}
else if(y1>=0.76 && y1<0.9) {
jarak1 = 134.296-(y1/0.0142); //sensor3 70-80
} 
else if(y1>=0.64&& y1<0.76) {
jarak1 = 142.683-(y1/0.0123); //sensor3 80-90
}
return jarak1;
}
 

void processCommand()
{
  if (DEBUG){
     Serial.println(receivedChars);
     if (strcmp ("kirimjarak",receivedChars) == 0) 
     { 
         float jarak = getjarak1();
         BTSerial.print("<");  
         BTSerial.print( jarak ); 
         BTSerial.print(">");
     }

if (strcmp("Pinbutton", receivedChars) == 0 )
     {
     char receivedChars[100]= "Pinbutton, buttonState1, buttonState2, buttonState3";
     char *token = strtok(receivedChars, ",");
      
 // DOWN 
      token = strtok(NULL, ",");    
      if(token[0] == 'a' ) {
        digitalWrite(ledPin1, HIGH); 
      }
      Serial.print(",");
      if (token[0] == 'b' ) {
        digitalWrite(ledPin1, LOW);
      }
 // UP    
      token = strtok(NULL, ",");
      if(token[0]== 'c') {
        digitalWrite(ledPin2, HIGH);
      }
      Serial.println(",");
      if(token[0]== 'd') {
        digitalWrite(ledPin2, LOW);
      }
 // EMERGENCY     
      token = strtok(NULL, ",");
      if(token[0]== 'e')  {
        digitalWrite(ledPin3, HIGH); 
      }
      Serial.println(",");
      if(token[0]== 'f')  {
      digitalWrite(ledPin3, LOW);
      }
      
      if (DEBUG) { 
      Serial.print("jarak : ");   
      Serial.print(receivedChars);  
      Serial.println("");   
      }
     }
     newData = false;
     receivedChars[0]='\0'; 
  }
}

void recvWithStartEndMarkers()
{
     static boolean recvInProgress = false;
     static byte ndx = 0;
     char startMarker = '<';
     char endMarker = '>';
     char rc;
 
     if (BTSerial.available() > 0) 
     {
          rc = BTSerial.read();
          if (recvInProgress == true) 
          {
               if (rc != endMarker) 
               {
                    receivedChars[ndx] = rc;
                    ndx++;
                    if (ndx > maxDataLength) { ndx = maxDataLength; }
               }
               else 
               {
                     receivedChars[ndx] = '\0'; // terminate the string
                     recvInProgress = false;
                     ndx = 0;
                     newData = true;
               }
          }
          else if (rc == startMarker) { recvInProgress = true; }
     }
 
}

And this My Code For [MASTER]

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11);
#include <Wire.h>  

#define button1 5
#define button2 6
#define button3 7
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;


boolean DEBUG = true;
const byte maxDataLength = 20;
char receivedChars[21] ;
boolean newData = false;
 
unsigned long startTime = 0;
unsigned long waitTime = 1000;
unsigned long startTime2 = 0;
unsigned long waitTime2 = 1000;
 

void setup()  
{
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  if (DEBUG)
  {
       // open serial communication for debugging
       Serial.begin(9600);
       Serial.println(__FILE__);
       Serial.println(" ");
  }
 
    BTSerial.begin(38400); 
    if (DEBUG)  {  Serial.println("AltSoftSerial started at 9600");     }
 
    newData = false; 
    startTime = millis();
 
} // void setup()

void loop()  
{
    if (  millis()-startTime > waitTime ) 
    {
       BTSerial.print("<kirimjarak>");  
       if (DEBUG) { 
       Serial.println("Request sent1"); }
       startTime = millis();
   }

    if ( millis() -startTime2 > waitTime2 )
    {
      delay(100);
      BTSerial.print ("<Pinbutton,");
      buttonState1 = digitalRead(button1);
      buttonState2 = digitalRead(button2);
      buttonState3 = digitalRead(button3);
      if (buttonState1 == HIGH){
        BTSerial.print('a');
      }
      else {
        BTSerial.print('b');
      }
      BTSerial.print(",");
      
      if (buttonState2 == HIGH){
        BTSerial.print('c');
      }
      else {
        BTSerial.print('d');
      }
      BTSerial.print(",");

      if(buttonState3 == HIGH){
        BTSerial.print('e');
      }
      else {
        BTSerial.print('f');
      }
      BTSerial.print(">");
      if (DEBUG) { Serial.println("Request sent1"); }
      startTime2 = millis();
    }
 
    recvWithStartEndMarkers(); 
    if (newData)  
    {    
         if (DEBUG) { 
         Serial.print("Data received Distance :"); }
         Serial.println( receivedChars );  
         newData = false;  
         receivedChars[0]='\0';     
    }    
}

void recvWithStartEndMarkers()
{
     static boolean recvInProgress = false;
     static byte ndx = 0;
     char startMarker = '<';
     char endMarker = '>';
     char rc;
 
     if (BTSerial.available() > 0) 
     {
          rc = BTSerial.read();
          if (recvInProgress == true) 
          {
               if (rc != endMarker) 
               {
                    receivedChars[ndx] = rc;
                    ndx++;
                    if (ndx > maxDataLength) { ndx = maxDataLength; }
               }
               else 
               {
                     receivedChars[ndx] = '\0'; // terminate the string
                     recvInProgress = false;
                     ndx = 0;
                     newData = true;
               }
          }
          else if (rc == startMarker) { recvInProgress = true; }
     }
}