why the receive data arranged line not the same as the transmit

Transmit

#include<SoftwareSerial.h>

SoftwareSerial mySerial(2 , 3 );//TX,RX

const int analogInPin = A0;//Analog input pin that the potentiometer
const int analogInPin1 = A1;
//const int threshold = 1 ; 

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  pinMode(A0,INPUT);
  pinMode(A1,INPUT); 
  Serial.begin(19200);
  mySerial.begin(19200);
  
}


// the loop routine runs over and over again forever:
void loop()
{
int sensorValue1 = analogRead(A0) ; //value read from the pot
int sensorValue2 = analogRead(A1) ;
float voltageValue1 = sensorValue1 * (5.0 / 1023.0); //value outputto the PWM(analog out)
float voltageValue2 =  sensorValue2 * (5.0 / 1023.0);

 Serial.print("\n\nsensor1 = ");
 mySerial.println("\n\nsensor1 = ");
  
  Serial.print(sensorValue1);
  mySerial.println(sensorValue1);
  
  Serial.print("\t voltage1 = ");
  mySerial.print("\t voltage1 = ");
  
  Serial.print(voltageValue1);
  mySerial.println(voltageValue1);
  
  Serial.print("\nsensor2 = ");
  mySerial.print("sensor2 =");
  
  Serial.print(sensorValue2);
  mySerial.println(sensorValue2);

  Serial.print("\t voltage2 = ");
  mySerial.print("\t voltage2 =");

  Serial.print(voltageValue2);
  mySerial.println(voltageValue2);


  //wait 2 miliseconds before the nect loop
 delay(1000);        // delay in between reads for stability
    }

Receive

#include<SoftwareSerial.h>

SoftwareSerial mySerial(2 ,3 );//TX,RX
void setup() {
  mySerial.begin(19200);
  Serial.begin(19200);
}

void loop ()
{
  
      String text = mySerial.readString();
      int data = mySerial.read();
      Serial.println(text); 
      Serial.println(data); 
  
      mySerial.flush();
      delay(1000);
}

Here's that appear at Serial Monitor

Transmit at COM 4 & Receive at COM 3

Capture2.PNG

Capture1.PNG

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

Your subject line is not sufficient to explain your problem. Thanks for the code tags, but you really need to explain in detail, what is different from what you expect to happen.

aarg:
Your subject line is not sufficient to explain your problem. Thanks for the code tags, but you really need to explain in detail, what is different from what you expect to happen.

well i expected the arranged line like sensor value, voltage value appear at COM3 maybe should be the same at COM4 but i jst wonder why

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

alright thanks for that advice :slight_smile: .

Imtheperson97:
well i expected the arranged line like sensor value, voltage value appear at COM3 maybe should be the same at COM4 but i jst wonder why

You just need to follow a reliable method for serial transmission as outlined in the first link in reply #2.

aarg:
You just need to follow a reliable method for serial transmission as outlined in the first link in reply #2.

okay . i will :slight_smile:

Imtheperson97:
alright thanks for that advice :slight_smile: .

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

i want to ask when char is 8 bit meanwhile i use string bcs the data sent is 10 bit , do i have to download library string.h in arduino ? its that possible?

string.h is already included in the basic Arduino package. You can use those string functions without any extra work. (Try it!)

If you have 10-bit data then you must have a system of sending more than one 8-bit byte to represent a single value. You can send a 16-bit int easily enough but it's a nightmare to debug. It is usually easier to send the data as human-readable characters, eg "1023", so that it's possible to look at the data transmitted on the serial monitor and understand what it's doing.

MorganS:
string.h is already included in the basic Arduino package. You can use those string functions without any extra work. (Try it!)

If you have 10-bit data then you must have a system of sending more than one 8-bit byte to represent a single value. You can send a 16-bit int easily enough but it's a nightmare to debug. It is usually easier to send the data as human-readable characters, eg "1023", so that it's possible to look at the data transmitted on the serial monitor and understand what it's doing.

if string.h in the arduino but why the string.h words still in black colour like #include<string.h>

Imtheperson97:
i want to ask when char is 8 bit meanwhile i use string bcs the data sent is 10 bit ,

Arduinos only work with 8 bit data - or multiples of 8.

...R

Robin2:
Arduinos only work with 8 bit data - or multiples of 8.

...R

To be specific it's the UART that is 8 bit.

aarg:
To be specific it's the UART that is 8 bit.

I was also thinking of chars, bytes, ints, and longs.

...R

If your potentiometer setting can get by with only 256 level precision (and it usually could), you can divide the 10 bit quantity by 4, and just send an 8 bit message.

Imtheperson97:
if string.h in the arduino but why the string.h words still in black colour like #include<string.h>

Because the Arduino editor is relatively dumb. Every library you have installed can tell it to highlight certain words. But it doesn't look at which libraries you are actually using - those words are highlighted even if you aren't using that library.

The syntax highlighting is useful if you've mispelled for() as For() but beyond that, I ignore it.