Problem with send date from one bluetooth to second use arduino

Hello,

I want to make system doors with sensors magnetic with bluetooth.
My second arduino(slave) change or sand wrong date to first arduino(Master).
And second my question is posible to print information to terminal only if is some change ?

//Master
#include <SoftwareSerial.h>
#define SENSOR  2

#define tx 11
#define rx 10
SoftwareSerial bt(rx,tx); //RX, TX


int sensorState =0;  

void setup() 
{
  Serial.begin(9600);
  bt.begin(9600);
  pinMode(tx, OUTPUT);
  pinMode(rx, INPUT);

  pinMode(SENSOR, INPUT_PULLUP);
}
void loop() 
{

  
  delay(2000);
  sensorState = digitalRead(SENSOR);
  if(sensorState == HIGH) {
      //Serial.println("Status door from clean area: Open");
       
       
    }
    else{
    Serial.println("Status door from clean area: Close");
    //bt.write("Status door from clean area: Close");
    }

   unsigned int sensorStatedirty={0};
   
   if (bt.available())
   sensorStatedirty = bt.read();
  
   
   if (sensorStatedirty != 1){
       Serial.println("Status door from dirty area: Open");
       bt.write("Status door from dirty area: Open");
    }
    else{
    Serial.println("Status door from dirty area: Close");
    bt.write("Status door from dirty area: Close");
   }
    
  Serial.println("");
    
  //if (Serial.available())
  //bt.write(Serial.read());
}
//Slave
#include <SoftwareSerial.h>
#define SENSOR  2

#define tx 11
#define rx 10
SoftwareSerial bt(rx,tx); //RX, TX


int sensorState =0;  
int c ={};

void setup() 
{
  Serial.begin(9600);
  bt.begin(9600);
  pinMode(tx, OUTPUT);
  pinMode(rx, INPUT);

  pinMode(SENSOR, INPUT_PULLUP);
}
void loop() 
{

  
  delay(2000);
  sensorState = digitalRead(SENSOR);
  if(sensorState == HIGH) {
      //Serial.println("Status door from clean area: Open");
       
       
    }
    else{
    Serial.println("Status door from clean area: Close");
    //bt.write("Status door from clean area: Close");
    }

   unsigned int sensorStatedirty={0};
   
   if (bt.available())
   sensorStatedirty = bt.read();
  
   
   if (sensorStatedirty != 1){
       Serial.println("Status door from dirty area: Open");
       bt.write("Status door from dirty area: Open");
    }
    else{
    Serial.println("Status door from dirty area: Close");
    bt.write("Status door from dirty area: Close");
   }
    
  Serial.println("");
    
  //if (Serial.available())
  //bt.write(Serial.read());
}

write sends a single byte
for multiple bytes use print() or println()

read takes a single byte out of the receive buffer

If you send multiple bytes there are more byte to read out in the receive buffer.

You should start from scratch with a testcode that sends a single byte over bluetooth receive that byte and print this byte to the serial monitor to check if this has worked.

Then send to bytes and watch in the serial monitor what happends.
In this way you will learn how it works

printing only once if a change has occured is done with state-change detecting

compare last_state with actual state if not equal print and update last_state with state

best regards Stefan

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