RF 433Mhz link "virtualwire" howto send multiple sensorvalue's

I have one of those cheap ebay radio sender and receiver working in the 433mhz band. (not the RF24L01)

Now i have found this code (below) wich allows me to send 1 sensorvalue over the rf link. But i have multiple values that i want to send. Can anyone help me to adjust the code for sending more variables?

For example:

I want to send, what is normally send as Serial.write(note,velocity,channel);
where note is an "int sensorvalue" aswell as velocity and channel.

Here is the transmitter code:

#include <VirtualWire.h>


const int Sensor1Pin = A2;
// const int Sensor2Pin = 3; 
const int ledPin = 13;

int Sensor1Data;
//int Sensor2Data;
char Sensor1CharMsg[4]; 

void setup() {

 // LED 
 pinMode(ledPin,OUTPUT);
 
 // Sensor(s)
 pinMode(Sensor1Pin,INPUT);
 
 // for debuggin
 Serial.begin(9600); 
 
 // VirtualWire setup
 vw_setup(2000);	 // Bits per sec


}

void loop() {
  
  // Read and store Sensor 1 data
  Sensor1Data = analogRead(Sensor1Pin);
  
  // Convert integer data to Char array directly 
  itoa(Sensor1Data,Sensor1CharMsg,10);
  
  // DEBUG
  Serial.print("Sensor1 Integer: ");
  Serial.print(Sensor1Data);
  Serial.print(" Sensor1 CharMsg: ");
  Serial.print(Sensor1CharMsg);
  Serial.println(" ");
  delay(1000);

  // END DEBUG
 
 digitalWrite(13, true); // Turn on a light to show transmitting
 vw_send((uint8_t *)Sensor1CharMsg, strlen(Sensor1CharMsg));
 vw_wait_tx(); // Wait until the whole message is gone
 digitalWrite(13, false); // Turn off a light after transmission
 delay(200); 
 
} // END void loop...

And the receiver code:

#include <VirtualWire.h>

int ledPin = 13;

// Sensors 
int Sensor1Data;

// RF Transmission container
char Sensor1CharMsg[4]; 

void setup() {
  Serial.begin(9600);
  
  // sets the digital pin as output
  pinMode(ledPin, OUTPUT);      
    
    // VirtualWire 
    // Initialise the IO and ISR
    // Required for DR3100
    vw_set_ptt_inverted(true); 
    // Bits per sec
    vw_setup(2000);	 
    
    // Start the receiver PLL running
    vw_rx_start();       

} // END void setup

void loop(){
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    
    // Non-blocking
    if (vw_get_message(buf, &buflen)) 
    {
	int i;
        // Turn on a light to show received good message 
        digitalWrite(13, true); 
	
        // Message with a good checksum received, dump it. 
        for (i = 0; i < buflen; i++)
	{            
          // Fill Sensor1CharMsg Char array with corresponding 
          // chars from buffer.   
          Sensor1CharMsg[i] = char(buf[i]);
	}
        
        // Convert Sensor1CharMsg Char array to integer
        Sensor1Data = atoi(Sensor1CharMsg);
        
        // DEBUG 
        Serial.print("Sensor 1: ");
        Serial.println(Sensor1Data);
        
        // END DEBUG
        
        // Clear Sensor1CharMsg char array for coming messages
        // This needs to be done otherwise problems will occur
        // when the incoming messages has less digits than the
        // one before. 
        for (i = 0; i < 4; i++)
        {
          Sensor1CharMsg[i] = 0;
        }
        
        // Turn off light to and await next message 
        digitalWrite(13, false);
    }
}

Well, the data in here is what gets sent out:

char Sensor1CharMsg[4];

So make this longer, and have every 2 or 4 bytes represent 1 sensor's data. Something along those lines.
Keep the total length under 27 bytes per Virtualwire documentation.

Pick off the 2 or 4 bytes per sensor at the receive side.

Allthough i completely understand what you mean, i am not experienced enough to write it.
i'm kinda new to all this.

Could you maybe show me an example of what i have to duplicate and what not in the code?

I would do it as an array.
This whole thought process probably needs some tweaking, but gives the idea:

int sensorArray[6]; // 6 ints for reading analog pins

in void loop:

sensorArray[0] = analogRead(0); // read the pins and put into the array
sensorArray[1] = analogRead(1); // arrange loop so there is some settling time between reads
sensorArray[2] = analogRead(2); // or just read each one twice
sensorArray[3] = analogRead(3);
sensorArray[4] = analogRead(4);
sensorArray[5] = analogRead(5);

then send it out:
vw_send((uint8_t *)sensorArray, strlen(sensorArray)); // send out array of 6 ints, or 12 bytes

On the receive side, data comes in as 12 bytes:
// Fill sensorArray with corresponding
// chars from buffer.
sensorArray = buf*;*
* }*

* // Convert Sensor1CharMsg Char array to integer*
* //Sensor1Data = atoi(Sensor1CharMsg);*
and put the ints back together
sensor0 = (sensorArray[0]<<8 )+sensorArray[1];
sensor1 = (sensorArray[2]<<8 )+sensorArray[3];
sensor2 = (sensorArray[4]<<8 )+sensorArray[5];
sensor3 = (sensorArray[6]<<8 )+sensorArray[7];
sensor4 = (sensorArray[8]<<8 )+sensorArray[9];
sensor5 = (sensorArray[10]<<8 )+sensorArray[11];
Might have to play with ascii to int somewhere too:
* // Convert Sensor1CharMsg Char array to integer*
* Sensor1Data = atoi(Sensor1CharMsg);*
Hard to say with out sending some data across and see how it comes out.

I would like to do the same , but can some one tel me which pins are you using on the arduino uno ?

hi,
there is something that i dont understand.

why do you add sensorArry[1] ?
sensor0 = (sensorArray[0]<<8 )+sensorArray[1];

I needed this and came on this post. I adapted guide Crossroads to get it working. Sensor data is converted to uint8, send, and reconstructed. In this case, I send joystick values to a remote car.

Transmit code:

#include <VirtualWire.h>
const int transmit_pin = 12;

// Analog pins Joystick
const int joyH = 0; //horizontaal - X
const int joyV = 1; //vertikaal   - Y
const int joyK = 2; //knop        - K

bool test = false;

// Waarden opslaan
unsigned int Hval, Vval, Kval;
uint8_t valArray[6];
unsigned int gebruiker = 2000 * 0;

void setup() {
  // Begin Seriele monitor
  if (test) Serial.begin(9600);
    
  vw_set_tx_pin(transmit_pin);
  vw_setup(2000);	 // Bits per sec
}

void loop(){
  // value between 0 en 1023 
  Hval = analogRead(joyH);
  Vval = analogRead(joyV);
  Kval = analogRead(joyK);
  Kval = map(Kval, 0, 1023, 0, 1);  //button joystick is 0 or 1
    
  //convert reads: uint16 to 2 tomes uint8 :
  valArray[0] = (Hval + gebruiker) >> 8;
  valArray[1] = (Hval + gebruiker) % 256;
  valArray[2] = (Vval + gebruiker) >> 8;
  valArray[3] = (Vval + gebruiker) % 256;
  valArray[4] = (Kval + gebruiker) >> 8;
  valArray[5] = (Kval + gebruiker) % 256;
  
  // send bytes. Here 6 bytes, that is 6 uint8 or 3 uint16
  vw_send((uint8_t *)valArray, 6);
  // wait for send to end
  vw_wait_tx();
  // Don't send too much the joystick values
  delay(200);
  
  if (test) {
    toonWaarden();
    delay(50);
  }
  
}

void toonWaarden() {
  Serial.print ("  "); 
  Serial.print (Hval);
  Serial.print ("  --  "); 
  Serial.print (Vval);
  Serial.print ("  --  "); 
  Serial.print (Kval);
  Serial.print ("  -- ");
  Serial.print (valArray[0]);
  Serial.print ("  --  ");
  Serial.print (valArray[1]);
  Serial.print ("  --  ");
  Serial.print (valArray[2]);
  Serial.print ("  --  ");
  Serial.print (valArray[3]);
  Serial.print ("  --  ");
  Serial.print (valArray[4]);
  Serial.print ("  --  ");
  Serial.print (valArray[5]);
  Serial.println ("  --  ");
}

The receiver code then recreates the integer values from the uint8. Note, the variable gebruiker is too allow two cars to drive, one sends integers in the range 0 to 2000, the other from 2000 to 4000. In case of collision, both receive noise ...

/*
Robot Auto - Remote car receives joystick values from MX-FS-03V transmitter to the XY-MK-5V receiver
*/

// download library van http://www.pjrc.com/teensy/td_libs_VirtualWire.html
#include <VirtualWire.h>

const int receive_pin = 11;

bool test = true;

// save values
int Hval, Vval, Kval;

// motor connections of the motor shield ar
#define motorrechtsPWM 3  //B-IB 
#define motorrechtsDir 2  //B-IA 
#define motorlinksPWM  5  //A-IB
#define motorlinksDir  4  //A-IA
//kalibratie variabelen, niet wijzigen als auto recht rijdt!
#define calib_speed_corrR   0  //als auto rechtdoor afwijkt, doe een correctie
#define calib_speed_corrL   0  //als auto rechtdoor afwijkt, doe een correctie

int calib_max_speed, calib_no_speed;

//internal variabeles
int right_speed;
int max_speed;
int min_speed;
int left_speed;

unsigned int gebruiker = 2000 * 0;

void setup() {
  // Begin Seriele monitor
  if (test) Serial.begin(9600);
  
  vw_set_rx_pin(receive_pin);
  vw_setup(2000);     
  
  // Start the receiver PLL running
  vw_rx_start();       
  
  calib_max_speed = 255; //maximum speed forward motors
  calib_no_speed  = 100; //min speed to motors that makes them stand still

  max_speed = calib_max_speed;
  min_speed = calib_no_speed;

  pinMode(motorlinksPWM, OUTPUT);
  pinMode(motorlinksDir, OUTPUT);
  pinMode(motorrechtsPWM, OUTPUT);
  pinMode(motorrechtsDir, OUTPUT);
  
  //startwaarden joystick
  Hval = 0;
  Vval = 0;
  Kval = 1;
}

void loop(){  
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  uint16_t tmp_Hval, tmp_Vval, tmp_Kval;
  
  // non blocking, read values when processed
  if (vw_get_message(buf, &buflen)) { // Non-blocking
    // Message with a good checksum received, print it.
    if (test) {
      Serial.print("Got: ");
      for (int i = 0; i < buflen; i++) {
        Serial.print(buf[i], HEX);
        Serial.print(' ');
      }
      Serial.println();
    }
    //uint8 received, convert to normal uint16:
    if (buflen == 6)  {
      tmp_Hval =  buf[0]; tmp_Hval = (tmp_Hval<<8) +  buf[1];
      tmp_Vval =  buf[2]; tmp_Vval = (tmp_Vval<<8) +  buf[3];
      tmp_Kval =  buf[4]; tmp_Kval = (tmp_Kval<<8) +  buf[5];

      // discrimate between cars using the gebruiker varible. Sensor values are 0 to 1024
      if (tmp_Hval >= gebruiker && tmp_Hval < gebruiker+1025 &&
          tmp_Vval >= gebruiker && tmp_Vval < gebruiker+1025 &&
          tmp_Kval >= gebruiker && tmp_Kval < gebruiker+1025 ) {
        // waarden van de juiste gebruiker !
        Hval = tmp_Hval-gebruiker;
        Vval = tmp_Vval-gebruiker;
        Kval = tmp_Kval-gebruiker;  // zal 0 of 1 zijn
        if (test) {
          Serial.print("Computed received values: ");
          toonWaarden();
        }
        //now convert to a speed between -255 en 255
        Hval = map(Hval, 0, 1023, -calib_max_speed, calib_max_speed);
        Vval = map(Vval, 0, 1023, -calib_max_speed, calib_max_speed);
        if (test) {
          Serial.print("Speed values: ");
          toonWaarden();
        }
      } else {
        if (test) {
          Serial.print("Problem, Converted to: ");
          Serial.print ("  "); 
          Serial.print (tmp_Hval);
          Serial.print ("  --  "); 
          Serial.print (tmp_Vval);
          Serial.print ("  --  "); 
          Serial.print (tmp_Kval);
          Serial.print ("  --  "); 
          Serial.print ((unsigned int) buf[0]);
          Serial.print ("  --  "); 
          Serial.print ((unsigned int) buf[1]);
          Serial.print ("  --  "); 
          Serial.print ((unsigned int) buf[4]);
          Serial.print ("  --  "); 
          Serial.print ((unsigned int) buf[5]);
          Serial.println ("  --");
        }
      }
    } else {
      if (test) {Serial.println("Error, buffer of wrong length received!");}
    }
  }
  
  motor_drive(Vval+Hval, Vval-Hval);
  
}

void toonWaarden() {
  Serial.print ("  "); 
  Serial.print (Hval);
  Serial.print ("  --  "); 
  Serial.print (Vval);
  Serial.print ("  --  "); 
  Serial.print (Kval);
  Serial.println ("  --");
}

void motor_drive(int right_speed, int left_speed){
  // Motors bewegen met de gegeven snelheid. 
  // snelheid is een waarde tussen -255 en 255!
  int sgvr = 1;
  if (right_speed != 0) {sgvr = abs(right_speed)/right_speed;}
  int sgvl = 1;
  if (left_speed != 0) {sgvl = abs(left_speed)/left_speed;}
  int vr = abs(right_speed) + calib_speed_corrR;
  int vl = abs(left_speed)  + calib_speed_corrL;
  if (vr > calib_max_speed) {vr = calib_max_speed;}
  else if (vr<0) {vr = 0;}
  vr = vr * sgvr;
  if (vl > calib_max_speed) {vl = calib_max_speed;}
  else if (vl<0) {vl = 0;}
  vl = vl * sgvl;
  
  if (right_speed>=0){
    digitalWrite(motorrechtsDir, LOW); //forward
    analogWrite(motorrechtsPWM, vr);
  } else {
    digitalWrite(motorrechtsDir, HIGH); //backward
    analogWrite(motorrechtsPWM, 255+vr);
  }
  if (left_speed>=0){
    digitalWrite(motorlinksDir, LOW); //forward
    analogWrite(motorlinksPWM, vl);
  } else {
    digitalWrite(motorlinksDir, HIGH); //backward
    analogWrite(motorlinksPWM, 255+vl);
  }
}