Transmitting Ultrasonic Sensor Readings between Unos

Hello,

me and my friend are working on a project. We are both using Arduino Unos. We are using the nRF24L01 wireless module(found here:In-Depth: How nRF24L01 Wireless Module Works & Interface with Arduino (lastminuteengineers.com)).

We are also using the A01NYUB ultrasonic sensor( found here:[A01NYUB Waterproof Ultrasonic Sensor Wiki - DFRobot](http://“A01NYUB-DFRobot Waterproof Ultrasonic Sensor SKU: SEN0313”)). Our goal is to use this sensor to measure a distance and transmit this distance using the wireless interface.

Attached is the full code we are trying to use.

We are currently getting an error here

   distance = value/10;

   String myString = String(distance);
   
   int StringLength = myString.length();

   char text[StringLength] = myString;

   radio.write(&text, sizeof(text));
  
  delay(1000);

see the line: char text[StringLength] = myString;

I am getting the error: array must be initialized with a brace-enclosed initializer

We had the transmitter working using this loop:

void loop() 
const char text[] = "Hello World";
radio.write(&text, sizeof(text));
delay(1000);

Where it continually sends a string of a constant length. My larger question is this, given that the distance variable(sensor reading) is of variable length, is it possible to send this with the use of an array? If so how? Is there a better method?

Thanks and if my post isn’t clear, let me know how it can be improved.

Transmitter_Sensor_2.ino (1.97 KB)

Use strings or Strings, but please, not both.

   int sum;

    sum=(data[0]+data[1]+data[2])&0x00FF;

I’ll never comprehend this programming “style”

char handle(float value)

Oops

  do{
    for(int i=0;i<4;i++)
    {
     data[i]=mySerial.read();
    }
  }while(mySerial.read()==0xff);

Read data from mySerial, even if there might not be any there?

Don’t use Strings on an Uno. Strings cause memory problems and program crashes.

To send an integer in binary, the following works:

int message = 12345;
radio.write((uint8_t *) &message, 2); //send 16 bit integer (two bytes)

Similar for receive.

Thanks for the advice.

The transmitter/sensor code has been changed to include:

distance = value/10;

radio.write((uint8_t *) &distance, 2); //send 16 bit integer (two bytes)

This compiles correctly.
I do not know how to alter the receiver code to accommodate this change in radio. write.

For Full sensor/transmitter code see: Transmitter_Sensor_2.ino

For the Original “Hello World” Transmitter code see: SimpleTransmitter.ino

For the Receiver Code I don’t know how to alter see: Receiver.ino

Thanks for the help

Transmitter_Sensor_2.ino (1.99 KB)

SimpleTransmitter.ino (652 Bytes)

Receiver.ino (716 Bytes)

I do not know how to alter the receiver code to accommodate this change in radio. write.

Replace

    char text[32] = "";

    radio.read(&text, sizeof(text));

With

    int message = 0;

    radio.read((uint8_t *) &message, 2);

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.