Connection between Arduinos is not working, I need it for urgent work, please anyone who knows help me

I'm using three ultrasonic sensors for this, apparently the connection sends -1 and some random numbers, I'm using three ultrasonic sensors for this, apparently the connection sends -1 and some random numbers, I don't understand what the problem is, the distance isn't being read either

Arduino that sends code:

#define vaga4_red 2
#define vaga4_green 6
#define echo4 4
#define trigger4 5

float distancia4 = 0;
float tempo4;
float velocidade = 0.172316;




void setup(){
  Serial.begin(9600);
  pinMode(vaga4_red, OUTPUT);
  pinMode(vaga4_green, OUTPUT);
  pinMode(echo4, INPUT);
  pinMode(trigger4, OUTPUT);
}

void loop(){

   //medindo a distancia do sensor 4
  digitalWrite(trigger4, LOW);
  delayMicroseconds(5);
  digitalWrite(trigger4, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger4, LOW);
  tempo4 = pulseIn(echo4, HIGH);
  distancia4 = 3 //tempo4 * velocidade;
  delay(500);
  Serial.print("Distancia 4: ");
  Serial.println(distancia4);
  

  if(distancia4 >= 2.4 && distancia4 <= 3310.0){
    digitalWrite(vaga4_red, HIGH);
    digitalWrite(vaga4_green, LOW);
    Serial.write("2");

  }
  else{
    digitalWrite(vaga4_red, LOW);
    digitalWrite(vaga4_green, HIGH);
    Serial.write("0");
  }
}

Arduino receiving code:

#include <Adafruit_LiquidCrystal.h>
Adafruit_LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

#define vaga1_red 13
#define vaga1_green 10
#define vaga2_red 9
#define vaga2_green 8
#define echo1 15
#define trigger1 14
#define echo2 17
#define trigger2 16


float distancia1 = 0;
float distancia2 = 0;
float tempo1;
float tempo2;
float velocidade = 0.172316;
int vagas_ocupadas = 0;
int vagas_livres = 4;
int ultrasonic1 = 0;
int ultrasonic2 = 0;



void setup()
{
  Serial.begin(9600);
  lcd.begin(16,2);
  pinMode(vaga1_red, OUTPUT);
  pinMode(vaga1_green, OUTPUT);
  pinMode(vaga2_red, OUTPUT);
  pinMode(vaga2_green, OUTPUT);
  pinMode(echo1, INPUT);
  pinMode(trigger1, OUTPUT);
  pinMode(echo2, INPUT);
  pinMode(trigger2, OUTPUT);
}

void loop()
{
  //distancia 1
  digitalWrite(trigger1, LOW);
  delayMicroseconds(5);
  digitalWrite(trigger1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger1, LOW);
  tempo1 = pulseIn(echo1, HIGH);
  distancia1 = tempo1 * velocidade;
  //Serial.print("Distancia 1: ");
  //Serial.println(distancia1);
  
  //distancia 2
  digitalWrite(trigger2, LOW);
  delayMicroseconds(5);
  digitalWrite(trigger2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger2, LOW);
  tempo2 = pulseIn(echo2, HIGH);
  distancia2 = tempo2 * velocidade;
  //Serial.print("Distancia 2: ");
  //Serial.println(distancia2);
  
  int ultrasonic4 = Serial.read();
  Serial.println(Serial.read());
  if (ultrasonic4 == 1){
    ultrasonic4 = 1;
  }else{
    ultrasonic4 = 0;
  }
  
  vagas_livres = 3 - vagas_ocupadas;
 
  
  if(Serial.available() > 0){
      vagas_ocupadas = ultrasonic1 + ultrasonic2 + ultrasonic4;  

      if(distancia1 >= 2.4 && distancia1 <= 3310.0){
        digitalWrite(vaga1_red, HIGH);
        digitalWrite(vaga1_green, LOW);
        ultrasonic1 = 1;
      }
      else{
        digitalWrite(vaga1_red, LOW);
        digitalWrite(vaga1_green, HIGH);
        ultrasonic1 = 0;

      }
      if(distancia2 >= 2.4 && distancia2 <= 3310.0){
        digitalWrite(vaga2_red, HIGH);
        digitalWrite(vaga2_green, LOW);
        ultrasonic2 = 1;
      }
      else{
          digitalWrite(vaga2_red, LOW);
          digitalWrite(vaga2_green, HIGH);
          ultrasonic2 = 0;
      }
       



      if(vagas_livres >= 1){
        lcd.clear();
        lcd.setCursor(3,0);  
        lcd.print("BEM-VINDO!");  
        delay(1000);
        lcd.setCursor(0,0);  
        lcd.print("Vagas livres = ");  
        lcd.print(vagas_livres);  
        lcd.setCursor(0,1);  
        lcd.print("Vagas ocup. = ");  
        lcd.print(vagas_ocupadas);
        delay(1000);
      }
      else{
        lcd.clear();
        lcd.setCursor(3,0);  
        lcd.print("SEM VAGAS");
      }
  	}
   
    
  }
  

It appears you have the RX pin of each Arduino tied together. That won't allow communication between the two. The Sender has to use the Tx pin (pin 1) and the receiver has to use the Rx pin (Pin 0).

It is also much easier to debug a project if you don't use pin 0 & 1 so you can use the Serial Monitor to see debug messages. You might want to switch to SoftwareSerial and use different pins.

You are also not sending/receiving the same information. Your sender code

Serial.write("2");

transmits the character '2' which is hex 0x32 or ASCII 50

Your reciver code

int ultrasonic4 = Serial.read();

reads in a byte so ultrasonic4 would contain the value 50, not 0 or 1 or 2

1 Like

Do you have real components or you are doing simulation?

1 Like

If this is a real lashup, you'll need to add GND from one Arduino to the other; if you're only playing in TinkerCAD, it probably doesn't matter, though I wish it did.

1 Like

Can you give a public link to the Tinkercad project ?
Be careful, there is also a "share" link, but then everyone can change your project. With a public link, others can make a copy and tinker with it.

1 Like

Sorry for the delay, my variables are written in Portuguese, if you have any questions let me know, thank you!

distance = distancia
time = tempo
speed = velocidade

Link: https://www.tinkercad.com/things/3GuKbNMMVLo-copy-of-estacionamento2a

Sorry for the delay, I'm just using a simulation, I don't have the real components, thank you!

Sorry for the delay, I don't have the real components, my work has to be done simulated, thank you!

1 Like

Sorry for the delay, I'll test the ways you told me, I'll try to use the SoftwareSerial library, I had tried before, but I couldn't, thank you!

We use the common "REPLY" button at the bottom. Replying to a message does not really work on this forum.

Did you remove the project of that link ? Was it a "shared" link ? The difference between those links is always confusing for me.

1 Like

Hello, I understand, I didn't know the "Reply" button, I also don't understand the difference between the links, I followed a step by step and got to this, for me it goes into my project, but if necessary I will share the link to a copy. Sorry for the English mistakes!

Your problem is here.

First you must check Serial.available() before using Serial.read() to see if there is even any data there to be read. If you call Serial.read() when no data is there to be read it will return -1. That explains the -1 you are seeing.

The second problem is that Serial data is sent one byte at a time in ASCII. If your sensor measures a distance of 15 and sends it on Serial, then what is actually sent are the numbers 49 (the ASCII code for '1') and then 53 (the ASCII code for '5').

Have a look at this great thread used by thousands to learn how to handle serial data on an Arduino.

This also is sending as ASCII text. So this will send the number 49.

This will send a long string of ASCII codes. Go look up the ASCII table and see if this helps you to make sense of the numbers you are seeing in your output.

===> Serial.println(ultrasonic4);

1 Like

Ok, what are you trying to accomplish? Why is there one sensor on the data receiving Arduino and the other two on the data sending Arduino?
Is this intentional?
If I was you, I would send chars over Serial based on the conditions you need from the sensors to drive a state machine on the receiving Arduino, since you don't seem to be sending the distance values over serial and instead just a detection state (ocupadas/livres).

It's easiest to use Serial.print('1'); on the sending Arduino (or any of the Ascii set of chars, letters, numbers, punctuation and some control characters - use single quotes and one char at a time).
Then read them in on the receiving Arduino, here's an example of receiving code you can try out just typing in chars 0-7 that might help:

char mode;
const int light = 3;
const int pump = 4;
const int fan = 5;

void setup() {
  Serial.begin(115200);
  pinMode(light, OUTPUT);
  pinMode(pump, OUTPUT);
  pinMode(fan, OUTPUT);
  Serial.println(F("charDrivenThreeDeviceStateMachine"));
  Serial.println();
  Serial.println(F("Type 0-7 to change functions"));
  Serial.println();
  mode = '0'; // start state variable at mode 0
  modeZero(); // let us know and init devices to off
}

void loop() {
  if (Serial.available() > 0) {
    mode = Serial.read();
    switch (mode) {
      case '0':
        modeZero();
        break;
      case '1':
        modeOne();
        break;
      case '2':
        modeTwo();
        break;
      case '3':
        modeThree();
        break;
      case '4':
        modeFour();
        break;
      case '5':
        modeFive();
        break;
      case '6':
        modeSix();
        break;
      case '7':
        modeSeven();
        break;
    }
  }
}

void modeZero() {
  Serial.println(F("mode 0: light off, pump off, fan off"));
  Serial.println("");
  allOff();
}
void modeOne() {
  Serial.println(F("mode 1: light on, pump on, fan on"));
  Serial.println("");
  allOn();
}
void modeTwo() {
  Serial.println(F("mode 2: light on, pump on, fan off"));
  Serial.println("");
  lightAndPump();
}
void modeThree() {
  Serial.println(F("mode 3: light off, pump on, fan on"));
  Serial.println("");
  fanAndPump();
}
void modeFour() {
  Serial.println(F("mode 4: light on, pump off, fan on"));
  Serial.println("");
  lightAndFan();
}
void modeFive() {
  Serial.println(F("mode 5: light on, pump off, fan off"));
  Serial.println("");
  justLight();
}
void modeSix() {
  Serial.println(F("mode 6: light off, pump on, fan off"));
  Serial.println("");
  justPump();
}
void modeSeven() {
  Serial.println(F("mode 7: light off, pump off, fan on"));
  Serial.println("");
  justFan();
}
void allOff() {
  digitalWrite(light, LOW);
  digitalWrite(pump, LOW);
  digitalWrite(fan, LOW);
}
void allOn() {
  digitalWrite(light, HIGH);
  digitalWrite(pump, HIGH);
  digitalWrite(fan, HIGH);
}
void lightAndPump() {
  digitalWrite(light, HIGH);
  digitalWrite(pump, HIGH);
  digitalWrite(fan, LOW);
}
void fanAndPump() {
  digitalWrite(light, LOW);
  digitalWrite(pump, HIGH);
  digitalWrite(fan, HIGH);
}
void lightAndFan() {
  digitalWrite(light, HIGH);
  digitalWrite(pump, LOW);
  digitalWrite(fan, HIGH);
}
void justLight() {
  digitalWrite(light, HIGH);
  digitalWrite(pump, LOW);
  digitalWrite(fan, LOW);
}
void justPump() {
  digitalWrite(light, LOW);
  digitalWrite(pump, HIGH);
  digitalWrite(fan, LOW);
}
void justFan() {
  digitalWrite(light, LOW);
  digitalWrite(pump, LOW);
  digitalWrite(fan, HIGH);
}

And here's a sender I cooked up using three ultrasonic sensors and Serial chart comms. Only tested with one sensor, didn't test the comms between two Arduinos (but should work unless I made a simple error)

/*
  Companion sketch: charDrivenThreeDeviceStateMachine
  Read and act on 3 ultrasonic sensors, based on Tim Eckel's
  example NewPing library sketch that pings 3 sensors 20 times a second.
  Receiving sketch is set up to handle 8 states (0-7) to act on three devices
  (a light, a pump and a fan if so configured in such a circuit)
*/

#include <NewPing.h>

#define SONAR_NUM 3      // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.

NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(4, 5, MAX_DISTANCE), // sensor 0 trigger pin, echo pin, and max distance.
  NewPing(6, 7, MAX_DISTANCE), // sensor 1 trigger pin, echo pin, and max distance.
  NewPing(8, 9, MAX_DISTANCE)  // sensor 2 trigger pin, echo pin, and max distance.
};
int mode = -1;
int lastMode = -2;

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  Serial.println(F("charDriverUltrasonicSender"));
  Serial.println();
}

void loop() {
  /* uncomment this function to just read and set up sensors */
  // justReadSensors();
  /* comment out everything else in loop() if just using above function */
  checkSensorPings();
  switch (mode) {
    case 0:
      allOff();
      break;
    case 1:
      allOn();
      break;
    case 2:
      lightAndPump();
      break;
    case 3:
      fanAndPump();
      break;
    case 4:
      lightAndFan();
      break;
    case 5:
      justLight();
      break;
    case 6:
      justPump();
      break;
    case 7:
      justFan();
      break;
    default:
      // do nothing
      break;
  }
  lastMode = mode;
}

void checkSensorPings() {
  if (sonar[0].ping_cm() == 0 && sonar[1].ping_cm() == 0 && sonar[2].ping_cm() == 0) {
    mode = 0;
  }
  else if ((sonar[0].ping_cm() > 50 && sonar[0].ping_cm() < 100) && (sonar[1].ping_cm() > 50 && sonar[1].ping_cm() < 100) && (sonar[2].ping_cm() > 50 && sonar[2].ping_cm() < 100)) {
    mode = 1;
  }
  else if ((sonar[0].ping_cm() > 50 && sonar[0].ping_cm() < 100) && (sonar[1].ping_cm() > 50 && sonar[1].ping_cm() < 100) && (sonar[2].ping_cm() == 0)) {
    mode = 2;
  }
  else if ((sonar[0].ping_cm() == 0) && (sonar[1].ping_cm() > 50 && sonar[1].ping_cm() < 100) && (sonar[2].ping_cm() > 50 && sonar[2].ping_cm() < 100)) {
    mode = 3;
  }
  else if ((sonar[0].ping_cm() > 50 && sonar[0].ping_cm() < 100) && (sonar[1].ping_cm() == 0) && (sonar[2].ping_cm() > 50 && sonar[2].ping_cm() < 100)) {
    mode = 4;
  }
  else if ((sonar[0].ping_cm() > 5 && sonar[0].ping_cm() < 15) && (sonar[1].ping_cm() == 0) && (sonar[2].ping_cm() == 0)) {
    mode = 5;
  }
  else if ((sonar[0].ping_cm() == 0) && (sonar[1].ping_cm() > 50 && sonar[1].ping_cm() < 100) && (sonar[2].ping_cm() == 0)) {
  mode = 6;
}
else if ((sonar[0].ping_cm() == 0) && (sonar[1].ping_cm() == 0) && (sonar[2].ping_cm() > 50 && sonar[2].ping_cm() < 100)) {
  mode = 7;
}
}

void allOff() {
  if (lastMode != mode) {
    Serial.print('0');// allOff(); to companion sketch
  }
}
void allOn() {
  if (lastMode != mode) {
    Serial.print('1');// allOn(); to companion sketch
  }
}
void lightAndPump() {
  if (lastMode != mode) {
    Serial.print('2');// lightAndPump(); to companion sketch
  }
}
void fanAndPump() {
  if (lastMode != mode) {
    Serial.print('3');// fanAndPump(); to companion sketch
  }
}
void lightAndFan() {
  if (lastMode != mode) {
    Serial.print('4');// lightAndFan(); to companion sketch
  }
}
void justLight() {
  if (lastMode != mode) {
    Serial.print('5');// justLight(); to companion sketch
  }
}
void justPump() {
  if (lastMode != mode) {
    Serial.print('6');// justPump(); to companion sketch
  }
}
void justFan() {
  if (lastMode != mode) {
    Serial.print('7');// justFan(); to companion sketch
  }
}

void justReadSensors() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    Serial.print(i);
    Serial.print("=");
    Serial.print(sonar[i].ping_cm());
    Serial.print("cm ");
    Serial.println();
  }
}

Sorry for the delay in responding, I'll use this, I didn't realize that I was using Serial.write() before Serial.available(), I had seen talk about the issue of bytes, I'll check, thank you!

I'm making a parking system, the Arduinos with 2 sensors are receiving the value from the Arduino that only has 1, but as I'm new to this I'm not sure if that's what's really happening, thanks for your code, I'll compare our projects, sorry for the delay in responding

Ok, I'll test it, thanks!

Did you manage to access the link I left here?