1

I'm using the 3715 RF arduino modules and a hc-sr04, and i'm trying to send the distance i get from the ultrasonic over to the arduino with the rf recieving module but i get something like this: enter image description here

Instead of getting this:enter image description here

Here are the code snippets for each arduino:

Arduino TX:

#include <VirtualWire.h>
const int led_pin = 11;
const int transmit_pin = 12;
const int receive_pin = 2;
const int transmit_en_pin = 3;
long distance;
long time1;
void setup()
{
 // Initialise the IO and ISR
 Serial.begin(9600);
 pinMode(9, OUTPUT); 
 pinMode(8, INPUT); 
 vw_set_tx_pin(transmit_pin);
 vw_set_rx_pin(receive_pin);
 vw_set_ptt_pin(transmit_en_pin);
 vw_set_ptt_inverted(true); // Required for DR3100
 vw_setup(2000); // Bits per sec
}
byte count = 1;
void loop()
{
 digitalWrite(9,LOW); 
 delayMicroseconds(5);
 digitalWrite(9, HIGH); 
 delayMicroseconds(10);
 time1=pulseIn(8, HIGH); 
 distance= int(0.017*time1); 
 Serial.println("Distance ");
 Serial.println(distance);
 Serial.println(" cm");
 delay(1000);
 char msg[1] = {distance};
 msg[0] = count;
 digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
 vw_send((uint8_t *)msg, 3);
 vw_wait_tx(); // Wait until the whole message is gone
 digitalWrite(led_pin, LOW);
 delay(1000);
}

Arduino RX:

#include <VirtualWire.h>
const int led_pin = 6;
const int transmit_pin = 12;
const int receive_pin = 11;
const int transmit_en_pin = 3;
void setup()
{
 delay(1000);
 Serial.begin(9600); // Debugging only
 Serial.println("setup");
 // Initialise the IO and ISR
 vw_set_tx_pin(transmit_pin);
 vw_set_rx_pin(receive_pin);
 vw_set_ptt_pin(transmit_en_pin);
 vw_set_ptt_inverted(true); // Required for DR3100
 vw_setup(2000); // Bits per sec
 vw_rx_start(); // Start the receiver PLL running
}
void loop()
{
 uint8_t buf[VW_MAX_MESSAGE_LEN];
 uint8_t buflen = VW_MAX_MESSAGE_LEN;
 if (vw_get_message(buf, &buflen)) // Non-blocking
 {
 int i;
 digitalWrite(led_pin, HIGH); // Flash a light to show received good message
 // Message with a good checksum received, print it.
 Serial.print("Got: ");
 for (i = 0; i < buflen; i++)
 {
 Serial.print(buf[i], HEX);
 Serial.print(' ');
 }
 Serial.println();
 digitalWrite(led_pin, LOW);
 }
}

I'm open for pointers as i'm pretty much a noob at this, thank you very much for your help! :)

JRobert
15.4k3 gold badges24 silver badges51 bronze badges
asked Oct 7, 2016 at 13:26
2
  • vw_send() can only send bytes. If you want to send numbers, you have to format them as an array of bytes, either as text or binary, before sending. Commented Oct 7, 2016 at 14:04
  • @EdgarBonet - or send the raw bytes comprising the existing storage of the number. Commented Oct 7, 2016 at 17:47

2 Answers 2

0

If you only need to display the value at the other end rather than do any calculations on it then it's nicer to send it as text rather than a number.

Sending:

 char msg[10]; // bigger than it needs to be to avoid overflow issues
 int length = sprintf(msg,"%ld",distance); // convert value to ascii
 digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
 vw_send((uint8_t *)msg, length + 1); // send value (add 1 to length to include the string terminator)
 vw_wait_tx(); // Wait until the whole message is gone
 digitalWrite(led_pin, LOW);
 delay(1000);

Since the data sent over the radio is now a standard c string complete with null terminator the receiving code becomes:

if (vw_get_message(buf, &buflen)) { // Non-blocking
 int i;
 digitalWrite(led_pin, HIGH); // Flash a light to show received good message
 // Message with a good checksum received, print it.
 Serial.print("Got: ");
 Serial.print(buf);
 Serial.println();
 digitalWrite(led_pin, LOW);
}

This is making the assumption that the whole thing is sent in a single radio packet, if the vw_get_message doesn't return the entire message in one go then nasty things will happen. You could always check for this be ensuring that strlen(buf) returns one less than buflen since buflen should include the null terminator but strlen() won't

answered Oct 7, 2016 at 14:21
1
  • "Since the data sent over the radio is now a standard c string complete with null terminator" You may want to make sure the terminator is there before calling anything, including strlen() which assumes one. Commented Oct 7, 2016 at 17:45
3

It looks like you're sending only one byte, but expecting to receive three. In the TX sketch, char msg[1] = {distance}; declares an array of one char, assigns its zero-th element the value of distance. Then msg[0] = count; over-writes it with the value of count. vw_send((uint8_t *)msg, 3); tries to send three bytes but only the first one has been defined. The second and third bytes transmitted are outside of the 1-length array.

You need something more like:

vw_send(distance, sizeof(distance));

on the TX side, and:

long distance;
uint8_t count;
vw_get_message(distance, &count));
Serial.println(distance);

on the RX side. You must receive the count into a variable but can ignore it, on the RX side; you know you'll be receiving a long; though good defensive coding would check the value anyway.

answered Oct 7, 2016 at 14:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.