Serial Communication [String Value] from ATTINY85 to Arduino Uno 3

  arduino, arduino-uno, attiny, c++, microcontroller

I have a working Serial communication of ATTINY85 to ARDUINO UNO 3.
The problem is that I can don’t know how to send Serial String value from ATTINY85 to UNO_R3, what am I missing to the code?

To illustrate what output I want, here are the steps to accomplish it.

[PIR SENSOR] detect object
[PIR SENSOR] send signal -> [ATTINY85]
[ATTINY85] 
  IF signal is HIGH or 1 THEN send String x = "1B3" -> [UNO_R3] 
  IF signal is LOW  or 0 THEN send String x = "0"   -> [UNO_R3] 
[UNO_R3] 
  Serial.println(String x received from [ATTINY85])
[UNO_R3 - SERIAL MONITOR]
  Prints either 1B3 or 0. Depending on the signal of [PIR SENSOR]

**note String x can be any string value

Here is the simple pin connection of ATTINY85 to UNO_R3.

ATTINY85 [2] PIR SENSOR
ATTINY85 [4] tx -> rx [7] UNO_R3
ATTINY85 [3] rx -> tx [8] UNO_R3

And here is my code on ATTINY85

#include <SoftwareSerial.h>

#define rx 3
#define tx 4
#define sn 2

SoftwareSerial mSerial(rx, tx);
  
void setup()
{
  mSerial.begin(9600);
  pinMode(sn, INPUT);
}

void loop()
{
  String d = "1B3";
  if (digitalRead(sn) == 1) { mSerial.print(d); }
  if (digitalRead(sn) == 0) { digitalWrite(tx, LOW); }
  delay(1000);
}

And here is my code to UNO_3

#include <SoftwareSerial.h>

#define rx 7
#define tx 8

SoftwareSerial mSerial(rx, tx);

void setup()
{
  pinMode(13, OUTPUT);
  
  Serial.begin(9600);
  mSerial.begin(7200);
}

void loop()
{
  if (mSerial.available()) {
    bool p = mSerial.read();
    
    if (p) {
      Serial.print((String)mSerial.read() + ": ");
      digitalWrite(13, LOW);
    } else { 
      Serial.print((String)mSerial.read() + ": ");
      digitalWrite(13, HIGH);
    }
  }
}

And here is printed text value:

-1:
-1:
-1:
255: 
-1: 
255: 
-1: 
255:

However I can switch on/off the light of LED on [UNO_R3] pin 13.

When the [PIR SENSOR] detects object the LED on [UNO_R3] pin 13 will turn off, otherwise it will turn on.

Thank you.

Source: Windows Questions C++

LEAVE A COMMENT