Send & receive multiple data from a arduino to another arduino via bluetooth

Hi!, I'm currently building my project using Arduino. I need to send & receive 2 sensor values using 2 HC-05 modules Using Hardware Serial communication. there are a Pir & an ultrasonic sensor as the sensors in the master arduino. I used Serial.parseInt(); to receive data to slave Arduino. That works good but sometimes my Pir value is 1 in the slave Arduino when the master Arduino sends value 0. I think it's my Master Arduino code's fault. But I don't know to fix it. help me!. This is the code below I used for Master Arduino.

#define trig 7
#define pir_pin 5
#define echo 4
 

String pir_read;

int c;

void setup() {
  /*int memory=EEPROM.read(0);
    distance=memory * 10;*/





  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(pir_pin, INPUT);

  Serial.begin(38400);




}

void loop() {
    digitalWrite(trig, LOW);
    delayMicroseconds(2);
    digitalWrite(trig, HIGH);
    delayMicroseconds(10);
    digitalWrite(trig, LOW);

    int pir = digitalRead(pir_pin);

    long t = pulseIn(echo, HIGH);
    long Cm = t / 29 / 2;

  

    Serial.print("C");
    Serial.println(Cm);
    delay(5);
    Serial.print("\tP");
    Serial.print(pir);
    Serial.print("  ");
    delay(5);

}

Let the master send a message to the slave to pull of a PIR reading. Then the master stays silent giving the slave the needed time slot do do its sensor reading.

1 Like

I need to receive two values at the same time

Then let the master be silent so the slave can collect and send 2 sets of data.

the problem is when the master sends Pir value 0, the slave detects it as 1

Like this

this is the slave code :point_down:

if (Serial.available()) {
    char type = Serial.read();


    if (type == 'C') {
      cm = Serial.parseInt();
      display.clearDisplay();
      display.setCursor(0, 0);
      display.print("Cm: ");
      display.setCursor(10, 0);
      display.print("   ");
      display.print(cm);
      display.print("cm");
      display.display();
    }

    if (type == 'P') {
      pir = Serial.parseInt();
      display.setCursor(0, 30);
      display.print("Human: ");
      display.setCursor(40, 30);
      display.print("   ");
      display.print(pir);
      display.display();
    }

Post a block drawing of the setup. When the master sends data the slave should read that data. You tell detect. Too many unknown things to give any more suggestion.

No, it's not! It's a cut out. Always post the entire code. All helpers are complete newbies to Your code and need the entire code to get some idea what it tries to do.

If You want to claim You write code then use comments telling what the lines are intended to do. We have no clue. It will be the same for You after time has passed.

That tabell of master and slave output tells nothing so far.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.