Combining and sending joystick values using RF transmitter/reciever

Hi Everyone,

I’m currently working on building a RC car, and want to control it using 2 Arduino boards. The first board is hooked to a 2 axis joystick and a 315 MHz transmitter. The joystick gives L/R and U/D integers from 0-1023 each. Since the code below only allows me to send one string of data, I decided to combine the two. The issue created with this is that when the x value goes to 0, the number has maximum length of 4 digits. Is there a way that it will hold zeros in the first 4 digits if nothing is there? Ex, send 00001023.

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

int yPin = A3;
int xPin = A4; 
long xPos = 0;
long yPos = 0;
long toSend = 0;

void setup() {

  // Transmitter is connected to Arduino Pin #10  

void loop() {
  xPos = analogRead(xPin);
  yPos = analogRead(yPin);  
  toSend = xPos*10000+yPos;
  Serial.print("   ");
  Serial.print("   ");

//send(int decimalCode, int bitLength)
  mySwitch.send(toSend, 24);


For what its worth, here is the code so far on the receiving end. It reads what is being sent to it, and I’ll eventually need to split the value sent to it to control a servo and a dc motor.

  Simple example for receiving
  Need help?

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

void setup() {
  mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2

void loop() {
  if (mySwitch.available()) {
    int value = mySwitch.getReceivedValue();
    if (value == 0) {
      Serial.print("Unknown encoding");
    } else {
      Serial.print("Received ");
      Serial.print( mySwitch.getReceivedValue() );
      Serial.print(" / ");
      Serial.print( mySwitch.getReceivedBitlength() );
      Serial.print("bit ");
      Serial.print("Protocol: ");
      Serial.println( mySwitch.getReceivedProtocol() );


Any help or suggestions are appreciated.

There are a few ways to do it.

  1. send bytes. an int is two bytes, so you send the low order byte, then the high order byte. Do the same with the other int., sending 4 bytes altogether recombine them at the receiver

  2. send a string containing one value, then a delimiter, then a string containig the other value. reconstitute the ints at the receiver.

I'd prefer the first method, but it's really up to you.

Look into the sprintf() function, it will help out a lot .

Thanks guys, I'll look at these suggestions and post up my results.

Below is some code from when I was tinkering with pots and servos. The pot/joystick code could be modified to send the servo command position with a servo identifier and a comma , delimiter for receiving by the servo serial receiving code.

pot/joystick test code

//zoomkat dual pot/servo test 12-29-12
//view output using the serial monitor

#include <Servo.h> 
Servo myservo1;
Servo myservo2;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;

int newval1, oldval1;
int newval2, oldval2;

void setup() 
  Serial.println("testing dual pot servo");  

void loop() 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){  
    Serial.print("1- ");

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    Serial.print("2- ");    

servo serial receiving code

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c =;  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
          Serial.print("writing Microseconds: ");
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
          Serial.print("writing Angle: ");
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
         readString=""; //clears variable for new input
    else {     
      readString += c; //makes the string readString