UnoJoy and Serial Communication

Hi folks!

I made a remote control for my RC copter using xbees and arduinos, and now i want to use just the remote control to acts as a simple game controller using UnoJoy.

The idea is, the remote control sends control data using serial communication to arduino which already flashed with UnoJoy.

When activated, the remote control sends a, b, c, d, e, f , and i only need the e and f values.
e is the X axis, and f is the Y axis data.

Here’s the full code for the receiver:

#include "UnoJoy.h"
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11, 12); // RX, TX
int axisValue[2];

void setup(){
  setupUnoJoy();
  mySerial.begin(38400);
}

void loop(){
  if (mySerial.available() > 0) {
    int but1 = mySerial.parseInt();
    int but2 = mySerial.parseInt();
    int but3 = mySerial.parseInt();
    int but4 = mySerial.parseInt();
    int xValue = mySerial.parseInt();
    int yValue = mySerial.parseInt();
    if (mySerial.read() == '\n') {
      axisValue[0] = map(xValue,0,1023,0,255);
      axisValue[1] = map(yValue,0,1023,0,255);
    }
  }
  dataForController_t controllerData = getControllerData();
  setControllerData(controllerData);
}

dataForController_t getControllerData(void){
  dataForController_t controllerData = getBlankDataForController();
  controllerData.leftStickX = axisValue[0];
  controllerData.leftStickY = axisValue[1];
  return controllerData;
}

If i hardcode the axisValue[0] and axisValue[1] using values from 0 to 255 in the loop section, there’s no problem, the computer can read it correctly.

But when i use

...
      axisValue[0] = map (xValue,0,1023,0,255);
      axisValue[1] = map (yValue,0,1023,0,255);
...

Nothing happens…

Anyone had a clue what’s happening?

You are using software serial on two pins that are not the hardware serial pins. That implies that the hardware serial pins are available for debugging. Why aren't you using them for that?

PaulS: You are using software serial on two pins that are not the hardware serial pins. That implies that the hardware serial pins are available for debugging. Why aren't you using them for that?

Hi Paul, Thanks for the fast response.

The hardware serial pins are already used by the UnoJoy to communicate with my computer. When flashed, arduino acts as a game controller. So i had to use the software serial instead.

I had try to read the data using the comm monitor before i flashed the arduino using UnoJoy, and everything looks OK. My remote control sends the correct data, and the receiver (from which i use the comm monitor), read it perfectly.

Oh, btw, here's link to UnoJoy project: code.google.com/p/unojoy/?

The hardware serial pins are already used by the UnoJoy to communicate with my computer.

OK.

So, the question is whether the Arduino is actually getting 6 ints AND a carriage return.

Can you control what is sent to the Arduino? If you, you could connect 6 LEDs, and compare the values sent to what you received, and light the appropriate LED when the values match.

Hi Paul,

For debugging purpose, i did this:

//#include "UnoJoy.h"
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11, 12); // RX, TX
int axisValue[2];

void setup(){
//  setupUnoJoy();
  mySerial.begin(38400);
  Serial.begin(38400);
}

void loop(){
  if (mySerial.available() > 0) {
    int but1 = mySerial.parseInt();
    int but2 = mySerial.parseInt();
    int but3 = mySerial.parseInt();
    int but4 = mySerial.parseInt();
    int xValue = mySerial.parseInt();
    int yValue = mySerial.parseInt();
    if (mySerial.read() == '\n') {
      axisValue[0] = map(xValue,0,1023,0,255);
      axisValue[1] = map(yValue,0,1023,0,255);
      Serial.print (but1);
      Serial.print (", ");
      Serial.print (but2);
      Serial.print (", ");
      Serial.print (but3);
      Serial.print (", ");
      Serial.print (but4);
      Serial.print (", ");
      Serial.print (axisValue[0]);
      Serial.print (", ");
      Serial.println (axisValue[1]);
    }
  }
//  dataForController_t controllerData = getControllerData();
//  setControllerData(controllerData);
}
/*
dataForController_t getControllerData(void){
  dataForController_t controllerData = getBlankDataForController();
  controllerData.leftStickX = axisValue[0];
  controllerData.leftStickY = axisValue[1];
  return controllerData;
}
*/

Then fire it up, and here’s the output at the serial monitor (let’s say the data format is a, b, c, d, e, f):
0, 0, 0, 0, 128, 128

Everything looks normal. When i wiggle my joystick at the remote control, the values of e and f changed between 0 to 255.

When i wiggle my joystick at the remote control

Then it's time you tell us about this remote control. What kind of joystick does it have? What is reading the values from the joystick?

It's a thumb joystick. It consists of 2 potentiometers and a pushbutton. Pretty similar like this https://www.sparkfun.com/products/9032

I connect the joystick to an arduino to its A0 pin for X axis and A1 pin for Y axis. The readings from the Joystick are between 0 to 1023. This arduino acts as a remote control.

Then i transmit the data to the other arduino using xbees, both xbees are already configured to communicate at 38400 bps. The speed for all serial comms are also set at 38400 bps.

I think the comms are okay since i can see the data transmitted from the remote control at the other arduino's serial monitor (which acts as a game controller).

I don't understand why this works when i put this in the loop section: axisValue[0] = 0; axisValue[1] = 255; It reads OK in the game controller calibration screen. The "+" sign moves to the top-right of the calibration box. Of course nothing happens if i wiggle my joystick. But when i use this: axisValue[0] = map(xValue,0,1023,0,255); axisValue[1] = map(yValue,0,1023,0,255); The "+" sign doesn't move at all.

I know it's much easier to buy a wireless game controller. But hey, i'm just curious :D

But when i use this: axisValue[0] = map(xValue,0,1023,0,255); axisValue[1] = map(yValue,0,1023,0,255); The "+" sign doesn't move at all.

I know it's much easier to buy a wireless game controller. But hey, i'm just curious

I think you need to disconnect the Unojoy thing for a while. Send data to the Arduino using the Serial Monitor application, so you can verify that the code is correct. It does not look like your Unojoy thing is sending values in the range 0 to 1023 for the joystick values.

If it is sending values of 0 or 1 (the thing moved or it didn't) then, the value that map() returns is going to be 0 in either case (since the map() call is simply an expensive way to divide by 4).