Here is my code
#include <SoftwareSerial.h>
#include <IRremote.h>
#define BAUDRATE 9600
#define MAX_Dist 128
byte Packet[MAX_Dist] = {0};
byte Checksum = 0, Calculated_Checksum = 0, Current_Read, Previous_Read , Connection_Check;
int Dist;
int IR_Pin = 3;
unsigned int Motor_Forward[] = {5700, 5700, 550, 1700, 550, 1700, 550, 1700, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 1700, 550, 1700, 550, 1700, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 1700, 550, 1700, 550, 1700, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 1700, 550, 1700, 550, 1700, 550, 1700, 550, 1700, 550};
unsigned int Motor_Reverse[] = {5700, 5700, 500, 1700, 550, 1700, 550, 1700, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 1700, 550, 1700, 550, 1700, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 1700, 550, 1700, 550, 550, 550, 1700, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 1700, 550, 550, 550, 1700, 550, 1700, 550, 1700, 550, 1700, 550};
unsigned int Motor_Stop[] = {5700, 5700, 550, 550, 550, 1700, 550, 1770, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 1700, 550, 1700, 550, 1700, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 1700, 550, 1700, 550, 1700, 550, 1700, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 1700, 550, 1700, 550, 1700, 550, 1700, 550};
SoftwareSerial ss(10, 11);
IRsend irsend;
byte Read_One_Byte()
{
int i = 0;
int Temp_Byte;
while (!ss.available())
{
i++;
if (i > 10000)
{
irsend.sendRaw(Motor_Stop, sizeof(Motor_Stop) / sizeof(int), 38);
}
}
Temp_Byte = ss.read();
return Temp_Byte;
}
void setup()
{
ss.begin(BAUDRATE);
Serial.begin(BAUDRATE);
pinMode (IR_Pin, OUTPUT); //output of IR as used in library
}
void loop()
{
if (Read_One_Byte() == 255)
{
if (Read_One_Byte() == 255)
{
Dist = Read_One_Byte();
if (Dist == MAX_Dist)
{
Calculated_Checksum = 0;
for (int i = 0; i < Dist; i++)
{
Packet[i] = Read_One_Byte();
Calculated_Checksum += Packet[i] ;
}
Calculated_Checksum = 255 - Calculated_Checksum;
Checksum = Read_One_Byte();
ss.end();
if (Checksum == Calculated_Checksum)
{
Connection_Check = Packet[0];
if (Connection_Check == 64)
{
Current_Read = Packet[98];
if (Current_Read >= Previous_Read)
{
irsend.sendRaw(Motor_Forward, sizeof(Motor_Forward) / sizeof(int), 38);
}
else
{
irsend.sendRaw(Motor_Reverse, sizeof(Motor_Reverse) / sizeof(int), 38);
}
Previous_Read = Current_Read;
}
else
{
Serial.println("Re_Connect device");
irsend.sendRaw(Motor_Stop, sizeof(Motor_Stop) / sizeof(int), 38);
Current_Read = 0;
}
ss.begin(BAUDRATE);
}
}
}
}
}
and for bluetooth communication, I've HC-05 bluetooth module in masted mode(one use in intermediate device).