I use sprintf() to convert all my variables to a string, and send it out. Then on the receiving side I use strtok() to split the string again. You can also use sscanf() to do the same thing, but it is written differently.
Another method is to use a structure and send it out, but you also need to use a structure to collect the sent data. I find this method to be slower.
sprintf Transmit:
#include <VirtualWire.h>
char Array[20];
int X,Y,Z;
void setup()
{
Serial.begin(115200); // Debugging only
Serial.println("Sending"); // Debugging only
// Initialise the IO and ISR
//vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_set_tx_pin(33); //RF sending pin
}
void loop()
{
X = constrain(map(analogRead(A14),410,345,0,255),0,255); //415 - 275 415,275
Y = constrain(map(analogRead(A15),415,275,0,255),0,255);//410 - 340 410,340
Z = constrain(map(analogRead(A13),310,410,0,255),0,255);//310 - 415 310,420
sprintf(Array, "%d,%d,%d.",X,Y,Z);
vw_send((uint8_t*)Array, strlen(Array));
vw_wait_tx();
}
strtok Receive: (fixed)
#include <VirtualWire.h>
uint8_t data[25];
void setup()
{
Serial.begin(115200); // Debugging only
Serial.println("Receiving");
pinMode(13,OUTPUT);
// Initialise the IO and ISR
//vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_set_rx_pin(8);
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 X = atoi(strtok((char*)buf, ",")); // Look for a comma, the return the data before it.
int Y = atoi(strtok(NULL, ",")); // same as above
int Z = atoi(strtok(NULL, ".")); // Look for a period, then return data before it.
Serial.print(X); // X axis
Serial.print(", ");
Serial.print(Y); // Y axis
Serial.print(", ");
Serial.print(Z); // Z axis
Serial.println();
}
}
sscanf Receive:
#include <VirtualWire.h>
int X,Y,Z;
void setup()
{
Serial.begin(115200); // Debugging only
Serial.println("setup");
vw_setup(2000); // Bits per sec
vw_set_rx_pin(2); // receiver pin
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
{
sscanf ((char*)buf, "%d%*c%d%*c%d%", &X, &Y, &Z);//123,456,7890
}
}
Structure Transmit:
//send struct data
#include <VirtualWire.h>
const byte xpin = A15; // x-axis of the accelerometer
const byte ypin = A14; // y-axis
const byte zpin = A13; // z-axis
struct {
int X;
int Y;
int Z;
} DATA;
void setup()
{
vw_setup(2000); // Bits per sec
vw_set_tx_pin(33);
}
void loop()
{
DATA.X = constrain(map(analogRead(A14),410,345,0,10),0,10); //415 - 275 415,275
DATA.Y = constrain(map(analogRead(A15),415,275,0,20),0,20);//410 - 340 410,340
DATA.Z = constrain(map(analogRead(A13),310,410,0,20),0,20);//310 - 415 310,420
vw_send((uint8_t*)&DATA, sizeof(DATA));
vw_wait_tx();
}
Structure Receive:
//struct receive
#include <VirtualWire.h>
struct DATA{
byte X;
byte Y;
byte Z;
};
struct DATA *DataIn;
void setup()
{
Serial.begin(115200);
vw_setup(2000); // Bits per sec
vw_set_rx_pin(8);
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)) // Non-blocking
{
DataIn = (struct DATA*)buf;
Serial.print(DataIn->X);
Serial.print(" ");
Serial.print(DataIn->Y);
Serial.print(" ");
Serial.println(DataIn->Z);
}
}