Hello guys, I am currently making a system which displays the distance of a ultrasonic sensor (HC SR 04 )via a 433 mHz radio module.
When I upload both of my codes, the serial monitor prints numbers like
"E35363132382". It would be awesome if someone can help me out.
Thx in advance.
Transmitter code
#include <VirtualWire.h> //Load the library
#include <SPI.h>
#include <Wire.h>
#define trigPin 10
#define echoPin 9
float duration, distance;
char msg[4];
void setup()
{
vw_set_tx_pin(12); // Sets pin D12 as the TX pin
vw_setup(2000); // Bits per sec
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trigPin, LOW);
delay(10);
digitalWrite(trigPin, HIGH);
delay(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration/2/29.1;
Serial.print("Distance: ");
Serial.println(distance);
dtostrf(distance, 6,2,msg); //converts the float into a char
vw_send((uint8_t *)msg, strlen(msg)); //transmits the data
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite (12,0);
delay(200);
}
RECEIVER CODE
#include <VirtualWire.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
int i;
void setup()
{
Serial.begin (9600);
vw_set_rx_pin(12);
vw_setup(2000);
vw_rx_start();
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if ( vw_get_message(buf, &buflen) )
{
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i]); // this is the part where I think it went wrong, the //receiever does receiver data but I think it isnt in the correct type. I am supposed //to get the distance in centimeters, so about couple digits like 123,45
}
}
}