How to send and recieve multi value communication with 2 HC-05

MASTER

#define SENSOR 2 // define pint 2 for sensor
#define ACTION 9 // define pin 9 as for ACTION
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>


SoftwareSerial BTserial(10, 11);
LiquidCrystal_I2C lcd(0x27,20, 4);
float value=0;
float rev=0;
int rpm;
int spd;
int oldtime=0;
int time;
int Count = 0;
int Sensor_value =digitalRead(SENSOR);
int mnt;
int mntm;
void isr() //interrupt service routine
{
rev++;
}
void cnt() //count
{
 Count++;
 digitalWrite(6,HIGH);
 delay(100);
}


void setup() {
 
 Serial.begin(9600);
 pinMode(SENSOR, INPUT_PULLUP);
 pinMode(ACTION, OUTPUT);
 attachInterrupt(0,isr,RISING);  //attaching the interrupt
 attachInterrupt(1,cnt,FALLING);  //attaching the interrupt
 
 lcd.begin();
 lcd.setCursor(4,0 ); 
 lcd.print("LCD Display"); 
 lcd.setCursor(4,2 ); 
 lcd.print("Welcome :D"); 
 
 delay(3000);
 lcd.clear();

 Serial.begin(9600);
 BTserial.begin(38400);
 
}



void loop() {
mnt++;


//------------------------------------------ SPEED
delay(1000);
detachInterrupt(0);           //detaches the interrupt
time=millis()-oldtime;        //finds the time 
rpm=(rev/time)*60000;         //calculates rpm
spd=(rpm*0.10472*0.508);         //calculate speed     
oldtime=millis();             //saves the current time
rev=0;
int sspd=spd/2;
int ccnt=Count/2;
int ttm=mnt/60;

lcd.clear();
lcd.setCursor(0,0);
lcd.print("SPEED");
lcd.setCursor(10,0);
lcd.print(     sspd);
lcd.print("   Km/Hr");

attachInterrupt(0,isr,RISING);

//------------------------------------------ ROUND


lcd.setCursor(0,1);
lcd.print("COUNT");
lcd.setCursor(10,1);
lcd.print(ccnt);
lcd.print("   Rounds");

attachInterrupt(1,cnt,FALLING);

//-------------------------------------------- TIME
lcd.setCursor(0,2);
lcd.print("TIME");
lcd.setCursor(10,2);
lcd.print(    ttm);
lcd.print("   Min");

//-------------------------------------------- CALORIES BURN
lcd.setCursor(0,3);
lcd.print("CALORIES");
lcd.setCursor(10,3);
lcd.print("X");
lcd.print("   KCAL");



BTserial.print(sspd);

BTserial.print(ccnt);

BTserial.print(ttm);

delay(100);
}

SLAVE

#include <Wire.h>
#include <Adafruit_SH1106.h>
#include <Adafruit_GFX.h>
#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11);

// OLED display TWI address
#define OLED_ADDR   0x3C
int state = 0;
String strReceived = "";
int speeda=0;

Adafruit_SH1106 display(-1);

#if (SSD1306_LCDHEIGHT != 64)

#endif

void setup() {
 // initialize and clear display
 display.begin(SH1106_SWITCHCAPVCC, OLED_ADDR);
 display.clearDisplay();
 Serial.begin(9600);
 BTserial.begin(38400);
 display.setTextSize(1.5);
 display.setTextColor(WHITE);
 display.setCursor(20,30);
 display.println("Welcome :D");
 display.display();
 delay(3000);
 display.clearDisplay();

 // display a line of text
}

void loop() {

  if (BTserial.available() > 0) // Checks whether data is comming from the serial port
 { 
speeda = BTserial.read(); // Reads the data from the serial port
 }
 else
 {

display.setTextSize(1);
 display.setTextColor(WHITE);
 display.setCursor(0,1);
 display.print("SPEED");
 display.setCursor(70,1);
 display.print(speeda);
 display.setCursor(100,1);
 display.print("KM/H");
 display.setCursor(0,10);
 display.print("CALORIES");
 display.setCursor(100,10);
 display.print("KCAL");
 display.setCursor(0,20);
 display.print("DISTANCE");
 display.setCursor(100,20);
 display.print("M");
 display.setCursor(0,30);
 display.print("TIME");
 display.setCursor(100,30);
 display.print("MIN");

 // update display with all of the above graphics
 display.display();
}
}

I don’t know how to send and recive this data help me please. :confused: :confused:

You need to design / implement a protocol. Based on Serial Input Basics, you can send something like

BTserial.print("<");
BTserial.print(sspd);
BTserial.print(",");
BTserial.print(ccnt);
BTserial.print(",");
BTserial.print(ttm);
BTserial.print(">");

Next you can use example #5 for the receiving end; the Serial in the reading part of the example needs to be changed to BTserial.

Please read How to use this forum, specifically point #7 about posting code.

Thank you for help sterretje ;)

I try to write code like this. But the displayed value does not change. It’s 0. What am I doing wrong?

SLAVE

#include <Wire.h>
#include <Adafruit_SH1106.h>
#include <Adafruit_GFX.h>
#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11);

// OLED display TWI address
#define OLED_ADDR   0x3C
//values-----------------------
int speeda = 0.0;
int counta = 0.0;
int timea = 0.0;

//-----------------------------
//receiving data---------------
const byte numChars = 64;
char receivedChars[numChars];
boolean newData = false;
char tempChars[numChars];
//-----------------------------

Adafruit_SH1106 display(-1);

#if (SSD1306_LCDHEIGHT != 64)

#endif

void setup() {
 // initialize and clear display
 display.begin(SH1106_SWITCHCAPVCC, OLED_ADDR);
 display.clearDisplay();
 Serial.begin(9600);
 BTserial.begin(38400);
 display.setTextSize(2);
 display.setTextColor(WHITE);
 display.setCursor(20,30);
 display.println("Welcome!");
 display.display();
 display.clearDisplay();
 

 // display a line of text 
}
void recvWithStartEndMarkers() {
 static boolean recvInProgress = false;
   static byte ndx = 0;
   char startMarker = '<';
   char endMarker = '>';
   char rc;

while (BTserial.available() > 0 && newData == false) {
       rc = BTserial.read();

       if (recvInProgress == true) {
           if (rc != endMarker) {
               receivedChars[ndx] = rc;
               ndx++;
               if (ndx >= numChars) {
                   ndx = numChars - 1;
               }
           }
           else {
               receivedChars[ndx] = '\0'; // terminate the string
               recvInProgress = false;
               ndx = 0;
               newData = true;
           }
       }

       else if (rc == startMarker) {
           recvInProgress = true;
       }
   }
}

void parseData() {      // split the data into its parts

   char * strtokIndx; // this is used by strtok() as an index

  
   strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
   speeda = atoi(strtokIndx);     // convert this part to an integer

   strtokIndx = strtok(NULL, ",");
   counta = atoi(strtokIndx);     // convert this part to a float

   strtokIndx = strtok(NULL, ",");
   timea = atoi(strtokIndx);     // convert this part to a float


}

void showParsedData() {
 

display.setTextSize(1);
 display.setTextColor(WHITE);
 display.setCursor(0,1);
 display.print("SPEED");
 display.setCursor(70,1);
 display.print(speeda);
 display.setCursor(100,1);
 display.print("KM/H");
 display.setCursor(0,10);
 display.print("CALORIES");
 display.setCursor(100,10);
 display.print("KCAL");
 display.setCursor(0,20);
 display.print("DISTANCE");
 display.setCursor(70,20);
 display.print(counta);
 display.setCursor(100,20);
 display.print("M");
 display.setCursor(0,30);
 display.print("TIME");
 display.setCursor(70,30);
 display.print(timea);
 display.setCursor(100,30);
 display.print("MIN");
 display.display();
 
}

  void loop() {

      recvWithStartEndMarkers();
   if (newData == true) {
       strcpy(tempChars, receivedChars);
           // this temporary copy is necessary to protect the original data
           //   because strtok() used in parseData() replaces the commas with \0
       parseData();
       showParsedData();
       newData = false;
   }
}

Start by adding some debug print statements; e.g. in loop

void loop() {

  recvWithStartEndMarkers();
  if (newData == true) {
    [color=red]Serial.print("Received: ");
    Serial.println(receivedChars);[/color]
    strcpy(tempChars, receivedChars);
    // this temporary copy is necessary to protect the original data
    //   because strtok() used in parseData() replaces the commas with \0
    parseData();
    showParsedData();
    newData = false;
  }
}

The first time that you call strtok() to start parsing, the first argument should not be NULL but tempChars.

void parseData() {      // split the data into its parts

  char * strtokIndx; // this is used by strtok() as an index

  [color=red][s]strtokIndx = strtok(NULL, ","); // this continues where the previous call left off[/s][/color]
  [color=green]strtokIndx = strtok(tempChars, ","); // this continues where the previous call left off[/color]
  speeda = atoi(strtokIndx);     // convert this part to an integer

  strtokIndx = strtok(NULL, ",");
  counta = atoi(strtokIndx);     // convert this part to a float

  strtokIndx = strtok(NULL, ",");
  timea = atoi(strtokIndx);     // convert this part to a float
}

Please fix your comments; atoi does not convert to float.