I asked a question about this last time but I still couldn't make it work but it somehow gave me an overview about my project. Now I've created an another project that is similar to the last one, the only different thing is that I used a VirtualWire library. So I connected an HC-SR04 to an arduino board(BOARD1) with a transmitter; and a receiver to another board(BOARD2). I Followed a code in the internet similar to my project but he has an lcd display but I just want to make my sensor readings displayed in my Serial monitor what should I do?
HERE IS THE CODE FOR THE TRANSMITTER
#include <VirtualWire.h>
const int trig= 9;
const int echo= A1;
float sensorval;
float distance;
char msg[6];
void setup() {
vw_set_tx_pin(12);
vw_setup(2000);
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trig,LOW);
delayMicroseconds(10);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
delayMicroseconds(10);
sensorval=pulseIn(echo,HIGH);
distance = (sensorval*.0343)/2;
Serial.print("Distance: ");
Serial.println(distance);
dtostrf(distance, 6,2,msg);
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx();
delay(500);
}
AND HERE IS THE CODE FOR THE RECEIVER
#include<VirtualWire.h>
#include <Wire.h>
int i;
void setup() {
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)){
Serial.print("distance is: ");
Serial.print((char *)buf);
Serial.print(buf[i]);
}
}