Friday, February 1, 2013

Building an Arduino dual-joystick module from the freebox Gamepad


The freebox Gamepad
A nice dual-joystick for Arduino

After beginning the discovery of the freebox controller internal parts (See my previous post: Inside the Freebox V6 controller), I just remembered that I needed a joystick shield for my Arduino. I found a a plenty of shields available (eBay search: Joystick Arduino ShieldSparkfun and Adafruit also sell some nice ones)  but tonight I decided to quickly build one from the freebox gamepad.


So I grabbed an USB freebox gamepad that was stored nearby and never used. Then I took it apart.


I cut the ribbon connector and separated the dual-joystick from the main board.


I then soldered the 9 pins of the ribbon cable to a single row Female Pin Header.

Pinout:
From Left to right:

  • Pin1: Left Joystick Vertical potentiometer
  • Pin2: Left Joystick Horizontal potentiometer
  • Pin3: Right Joystick Horizontal potentiometer
  • Pin4: Right Joystick Horizontal potentiometer
  • Pin5: Left Push button
  • Pin6: Right Push button
  • Pin7: Ground (-)
  • Pin8: Vcc (+)
  • Pin9: Center LED

I attached a Male to Male Dupont wire to te header female pins.


Then snapped everything to my breadboard.
The four LEDs are connected to pins 11, 10, 9 and 6 (PWM pins).


I then modified Tom Igoe's Arduino code for Analog input/output and tested it.

Now the dual-joysticks are working perfectly and their values are mapped correctly. The LEDs are lit depending on the position of the joystick's internal Left/Right potentiometers.

Here is the Arduino Code:
 /*  
  Dual Joystick (Analog input, analog output, serial output)  
  Used for the freebox V6 dual joystick  
    
  Reads four analog input pins, maps the result to a range from 0 to 255  
  and uses the result to set the pulsewidth modulation (PWM) of four output pins.  
  Also prints the results to the serial monitor.  
    
  The circuit:  
  * four potentiometers connected to analog pins 0, 1, 2 and 3.  
   Center pin of the potentiometer goes to the analog pin.  
   side pins of the potentiometer go to +5V and ground  
  * LED connected from digital pins 11, 10, 9, and 6 to ground  
    
 Modified by Omar Cherif from the original (single pin) version by Tom Igoe.  
    
  This example code is in the public domain.  
    
  */  
   
 // These constants won't change. They're used to give names  
 // to the pins used:  
 const int analogInPin0 = A0; // Analog input pin that the left vertical potentiometer is attached to  
 const int analogInPin1 = A1; // Analog input pin that the left horizontal potentiometer is attached to  
 const int analogInPin2 = A2; // Analog input pin that the right horizontal potentiometer is attached to  
 const int analogInPin3 = A3; // Analog input pin that the right vertical potentiometer is attached to  
 
 const int analogOutPin0 = 11; // Analog output pin that the LED0 is attached to  
 const int analogOutPin1 = 10; // Analog output pin that the LED1 is attached to  
 const int analogOutPin2 = 9; // Analog output pin that the LED2 is attached to  
 const int analogOutPin3 = 6; // Analog output pin that the LED3 is attached to  
   
 int sensorValue0 = 0;    // value read from the pot  
 int sensorValue1 = 0;    // value read from the pot  
 int sensorValue2 = 0;    // value read from the pot  
 int sensorValue3 = 0;    // value read from the pot  
 int outputValue0 = 0;    // value output to the PWM (analog out)  
 int outputValue1 = 0;    // value output to the PWM (analog out)  
 int outputValue2 = 0;    // value output to the PWM (analog out)  
 int outputValue3 = 0;    // value output to the PWM (analog out)  
   
 void setup() {  
  // initialize serial communications at 9600 bps:  
  Serial.begin(9600);   
 }  
   
 void loop() {  
  // read the analog in value:  
  sensorValue0 = analogRead(analogInPin0);        
  sensorValue1 = analogRead(analogInPin1);        
  sensorValue2 = analogRead(analogInPin2);        
  sensorValue3 = analogRead(analogInPin3);        
  // map it to the range of the analog out:  
  outputValue0 = map(sensorValue0, 0, 1023, 0, 255);   
  outputValue1 = map(sensorValue1, 0, 1023, 0, 255);   
  outputValue2 = map(sensorValue2, 0, 1023, 0, 255);   
  outputValue3 = map(sensorValue3, 0, 1023, 0, 255);   
  // change the analog out value:  
  analogWrite(analogOutPin0, outputValue0);        
  analogWrite(analogOutPin1, outputValue1);        
  analogWrite(analogOutPin2, outputValue2);        
  analogWrite(analogOutPin3, outputValue3);        
   
  // print the results to the serial monitor:  
  Serial.print("S1 = " );              
  Serial.print(sensorValue0);     
  Serial.print(" / out1 = ");     
  Serial.print(outputValue0);    
  Serial.print("\t");    
    
  Serial.print("S2 = " );              
  Serial.print(sensorValue1);     
  Serial.print(" / out2 = ");     
  Serial.print(outputValue1);    
  Serial.print("\t");    
   
  Serial.print("S3 = " );              
  Serial.print(sensorValue2);     
  Serial.print(" / out3 = ");     
  Serial.print(outputValue2);    
  Serial.print("\t");  
   
  Serial.print("S4 = " );              
  Serial.print(sensorValue3);     
  Serial.print(" / out4 = ");     
  Serial.println(outputValue3);    
   
  // wait 2 milliseconds before the next loop  
  // for the analog-to-digital converter to settle  
  // after the last reading:  
  delay(2);             
 }  

Now that the joysticks are working, I can easily do some experiments with X and Y axis. The controller also have two push buttons (Activated by pushing down the sticks), i didn't use them for now. They are connected to Pin 5 and 6.

Here are some videos of the joysticks working:


These joysticks enable me to do some further experiments on Servo and Stepper motors, which are just the beginning of a long journey inside the joyful world of robotics...

2 comments: