Рroblem with transmitting a variable between two Arduino Pro Mini boards via UART

Two Arduino Pro Mini boards are connected via UART, and one transmits a uint16_t variable to the other. Why does the variable get distorted on the receiver side when displayed on its own monitor? For example, if the variable is 25, the receiver shows 4560. But if you connect the Arduino IDE via the FOCA programmer to the receiver, everything becomes normal, and the Nextion monitor shows the correct value

It’s probably the double decker bus in-between, that you didn’t show in your diagram.

I did not understand your response, can you explain in more detail what you mean?

Is that clearer ?
The code might help as well.

1 Like

You made 2 mistakes:

  1. An error in your code or circuit
  2. You assumed forum members have psychic powers

Have you read the forum guide in the sticky post? This is a great example of why that guide exists.

1 Like

did you forget to connect GNDs ?

The ground is connected correctly. The power supply and RX/TX pins are fine. Additionally, the RX and TX pins are connected accordingly. The power supply's output matches the load. Besides, I tried using two independent power sources with a common ground. The variable is output to an independent display, not to the Serial monitor. I can't understand how connecting to the Arduino IDE can affect the transmission of this variable. I'll post the receiver and transmitter code a bit later.Tnks

do you have the Serial monitor open ?

The Serial Monitor is closed. It is not used in the project. I connect the FOCA to the receiver, launch the IDE, the port (COM4) is detected, and the variable enters the normal range. This only works on the receiver side.

OK - code and exact diagram would help

Hi, @stborys
Welcome to the forum.

The above link will show you how to post your code.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

[code]

#include<SoftwareSerial.h>
#include <TimerOne.h>
SoftwareSerial mySUART(7, 8);  //D7, D8 = SRX, STX
const int numReadings = 100;    // samples are taken at 100Hz so calculate average over 1sec
int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
long int total = 0;             // the running total
int latest_reading = 0;         // the latest reading
int average_reading = 0;        // the average reading
int dustPin = A0;
//uint16_t Dust_val1;
float percent;
void setup()
{
  Serial.begin(9600);
  mySUART.begin(9600);
  // Configure PWM Dust Sensor Sample pin (OC1A)
  pinMode(9, OUTPUT);
  // Configure PWM Dust Sensor LED pin (OC1B)
  pinMode(10, OUTPUT);
  // Configure INT0 to receive Dust Sensor Samples
  pinMode(2, INPUT_PULLUP);
  // Put Timer 1 into 16-bit mode to generate the 0.32ms low LED pulses every 10ms
  // A0 needs to be sampled 0.28ms after the falling edge of the LED pulse - via INT0 driven by OC1A (D9)
  Timer1.initialize(10000); // Set a timer of length 10000 microseconds (or 10ms - or 100Hz)
  Timer1.pwm(10, 991); // Set active high PWM of (10 - 0.32) * 1024 = 991
  Timer1.pwm(9, 999); // Set active high PWM of (10 - 0.28) * 1024 = 995 BUT requires a fiddle factor making it 999
  // Attach the INT0 interrupt service routine
  attachInterrupt(0, takeReading, RISING); // Sample A0 on the rising edge of OC1A

  // Initialise sample buffer
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}
// Dust sample interrupt service routine
void takeReading() {
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  latest_reading = analogRead(dustPin);
  readings[readIndex] = latest_reading;
  // add the reading to the total:
  total = total + latest_reading;
  // advance to the next position in the array:
  readIndex = readIndex + 1;
  // if we're at the end of the array...wrap around to the beginning:
  if (readIndex >= numReadings) readIndex = 0;
  // calculate the average:
  average_reading = total / numReadings; // Seems to work OK with integer maths - but total does need to be long int
  percent = ((average_reading * 100.0) / 1023); //% Dust  in Air
}
void loop()
{
  uint16_t Dust_val1;
  if (percent > 0 && percent <= 100.0) {
    Dust_val1 = (uint16_t)(percent * 10); //% Dust  in Air*10
  }
  mySUART.flush();
  uint8_t byte1 = Dust_val1 & 0xFF;
  uint8_t byte2 = (Dust_val1 >> 8) & 0xFF;
  mySUART.write(byte1);
  mySUART.write(byte2);
  delay(1000);
  // Serial.println( Dust_val1 );
}

[/code]
[code]
#include <SoftwareSerial.h>
SoftwareSerial mySUART(7, 8);  //D7, D8 = SRX, STX
bool Dust_flag = true;
uint16_t dust_percent;
uint32_t function_time = millis();
void setup() {
  mySUART.begin(9600);
}
void loop() {
  if ((millis() - function_time) >= 4000)
  {
    function_time = millis();
    Dust_flag = true;
  }

  else if ((millis() - function_time) >= 2000 && Dust_flag) {
    Feedback_Dust_sensor();
    Dust_flag = false;
  }
}
void Feedback_Dust_sensor() {
  if (mySUART.available())
  {
    //uint16_t persent;
    mySUART.readBytes((uint8_t *)&dust_percent, 2);
    delay(5);

  }
}
[/code]

I hope nothing now prevents us returning to the essence of the question.

If you had read and followed the Forum Guidelines before your first post, we wouldn't be 16 posts into this thread before you did it correctly.

Your code has no synchronization to guarantee that the RX will receive the TX bytes in the correct order rather than than getting bytes from two different uint16_t values sent from the TX. There's also no error handling. One solution is to send values as ASCII with data start / end markers ... See Robin2's Serial Input Basics Tutorial. Another is to send it as binary but with proper framing. This is a little more difficult as the start / end markers can also appear in the binary data. Take a look at the technique used by the SerialTransfer Library.

I don't see the GND to GND connection between the two boards ?
the image is so compressed I can't read it (I've poor eye sight)

How does connecting to the Arduino IDE via FOCA fix this and set the correct range of the variable? Tnks

This is not a schematic, it doesn't show the power supply. Trust me, their ground is common. Otherwise, connecting to anything would not yield any results

OK

You are using interrupts and your variables are not volatile and there is no critical section in the loop for accessing the variables shared with the ISR.