I am experimenting on wireless communication of two Arduinos through RF links using VirtualWire library. I have some problems in data sending and receiving by means of data type. As far as I know, VirtualWire library allows us to send and receive only 8 bit integers. In this case, I must send 2 different values in form of 8 bit integers like X and Y coordinate values. Could you please help me about sending two integers consecutively and receive them in the right order. Thank you very much in advance.
In this case, I must send 2 different values in form of 8 bit integers like X and Y coordinate values. Could you please help me about sending two integers consecutively and receive them in the right order.
Not necessarily. The vw send function only sends bytes and you have to tell it how many. An int is two bytes, so tell vw to send two bytes, and cast the int as a byte pointer.
vw.send((byte *)someInt, 2);
You could use two sends to send highByte(someInt) and lowByte(someInt), or an array where the two values are highByte(someInt) and lowByte(someInt).
There's more than one way to skin this particular cat.
First, thank you very much for your kind assistance. I have tried the vw.send((byte *)someInt, 2);
code and it outputs the divided sections in the serial monitor. However, sending an integer from transmitter board results different (or converted) values from the receive's serial port. Let's say transmitter board sends the integer value=333 and the receiver gets the value as 13. Am I required to run some kind of conversion code or something? Thanks again.
Let's say transmitter board sends the integer value=333 and the receiver gets the value as 13. Am I required to run some kind of conversion code or something? Thanks again.
Or something, yes. Post your code is the "or something".
Ok here is my code
Transmitter :
#include <VirtualWire.h>
int VAL = 0;
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
}
void loop()
{
VAL=333;
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((byte *)VAL, 2);
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);
delay(200);
}
Receiver :
#include <VirtualWire.h>
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
// Initialise the IO and ISR
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(13, true); // Flash a light to show received good message
// Message with a good checksum received, dump it.
Serial.print("Got: ");
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i], DEC);
Serial.print(" ");
}
Serial.println("");
digitalWrite(13, false);
}
}
You are sending an int as two bytes - the high order byte and the low order byte.
333 = 0x0100 + 0x4D.
The high order byte then is 0x01 and the low order byte is 4D.
You are then receiving two bytes and printing them individually. You should be seeing 1 and 77 as those two bytes.
I have no clue why you are getting 1 value, and that value is 13.
First, thank you for your interest PaulS. As you said, the read value should be 1 and 77 however, I've got the serial monitor readings as 13 and 14 like Got : 13 14
Since I have two values that are changing 0 to 255 to send, what kind of alteration should I apply to this code? Thank you for your assistance once again.
I am having a similar problem. But mine is trying to encapsulate four different readings into a single transmission. So, I'm only needing to send a single byte of data. Because of me only needing four bits either the high order bits or the low order bits are gong to be all 0s. My instructor gave me a line of code to breakdown number into binary. The code goes as follows:
value[x] = bit(y) & data
value is going to be a an array with 4 ints inside
y is which int is in slot 0-3
data is the number received
bit(x) takes the value of x bit where x is 0-3 or 4-7 depending on which bits i send the data on.
So, what happens is that first the data is received, then broken into its bits and lastly stored in the array.
To anybody that wants to reply, this thread was started over seven years ago.
You can combine your readings in a struct and send the struct. A struct allows you to combine related information (e.g. name and phone number in a phone book).
Place the below near the top of your code (e.g. after the includes).
struct DATA
{
int reading1;
int reading2;
int reading3;
int reading4;
};
Your transmitter can use something like below
void loop() {
DATA readings;
readings.reading1 = analogRead(A0);
readings.reading2 = analogRead(A1);
readings.reading3 = analogRead(A2);
readings.reading4 = analogRead(A3);
vw_send((byte *)&readings, sizeof(readings));
As vw_send wants a pointer to a byte array; you cast the address of the DATA structure to a pointer to a byte array; you also give vw_send the size of the data struct so it will send the full struct.
The receiver side can be something like
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
if(buflen!=sizeof(DATA))
{
// something is wrong
}
// treat buf as if it's DATA
DATA *rcvd = (DATA*)buf;
Serial.print("Reading 1 "); Serial.println(rcvd->reading1);
Serial.print("Reading 2 "); Serial.println(rcvd->reading2);
Serial.print("Reading 3 "); Serial.println(rcvd->reading3);
Serial.print("Reading 4 "); Serial.println(rcvd->reading4);
}
}
Here you read the data into a buffer in the usual way and next tell the program that buf actually contains DATA and not bytes.
Not tested.