Convert ascii to int

Using two Nanos and two RF24L01, I'm sending ultrasonic data from a JSN-SR04T. The Tx serial monitor shows the correct numbers but the Rx shows the asscii value. I have tried several suggestions listed here without success. This is my first attempt to program and surprised I have made it this far. First the Rx then the Tx.

void loop()
{
  
  if (radio.available()) {
 
    char text[32] = "";
    int received;
    
    radio.read(&text, sizeof(text));
      
      lcd.clear();
      lcd.print("Distance= ");     
      //Serial.print(Serial.readString().toInt());
      lcd.print(text);
      Serial.println(text);
      
      delay(500);
}
}


  // Measure distance
  unsigned int distance_cm = sonar.ping_cm();

  // Print distance to Serial Monitor 
  Serial.print("Distance: ");
  
  distance_cm = (distance_cm)*.393701;
  Serial.print(distance_cm);
  Serial.println(" inches");

  // Prepare data to transmit
  int data[sizeof(distance_cm)];
  memcpy(data, &distance_cm, sizeof(distance_cm));

  // Transmit the data
  if (radio.write(data, sizeof(data))) {
    
  }   
 

    delay(500); // Wait for 1/2 second
}
``
void loop() {
  // Measure distance
  unsigned int distance_cm = sonar.ping_cm();

  // Print distance to Serial Monitor 
  Serial.print("Distance: ");
  
  distance_cm = (distance_cm)*.393701;
  Serial.print(distance_cm);
  Serial.println(" inches");

  // Prepare data to transmit
  int data[sizeof(distance_cm)];
  memcpy(data, &distance_cm, sizeof(distance_cm));

  // Transmit the data
  if (radio.write(data, sizeof(data))) {
    
  }   
 

    delay(500); // Wait for 1/2 second
}

So what you get with this?

  // Prepare data to transmit
  int data = distance_cm;
  //memcpy(data, &distance_cm, sizeof(distance_cm));

  // Transmit the data
  if (radio.write(data, sizeof(data))) {

You need to post the output also in code tags.

Check the difference between serial.print and serial.write. You should find: "Serial.print() converts data to a human-readable format (ASCII), while Serial.write() sends raw byte data directly. Using Serial.write() can be faster for transmitting binary data, but the output won't be easily readable in the Serial Monitor."

Thank you for your help.
I did try the Serial.write() you suggested but still the same ascii on the receiver end.
Randoman019

There are many things wrong with the code you posted.

Better to start with tutorials that have working examples, e.g.

The problem is the receiver end.

The data is not being transmitted as text, but your receiver code treats the data as text.

Use the change suggested by @kmin for the transmit code.

Try this for receiver code:

void loop()
{
  
  if (radio.available()) {
 
    //char text[32] = "";
    int received;
    
    radio.read(&received, sizeof(received));
      
    lcd.clear();
    lcd.print("Distance= ");     
    //Serial.print(Serial.readString().toInt());
    lcd.print(received);
    Serial.println(received);
      
    delay(500);
  }
}
1 Like

Don't try to read the transmitted value until all the bytes have "crossed the wire"

  int received;
  if (radio.available() >= sizeof(received)) {
    radio.read(&received, sizeof(received));

If you're paranoid, read returns the number of bytes read (past tense) so you can double-check.

Hi Paul,
Thank you. Using the (received) was the solution.
Randy

1 Like