Skip to content

Instantly share code, notes, and snippets.

@akru
Created February 29, 2016 09:25
Show Gist options
  • Save akru/21a7ed7510dab06878d8 to your computer and use it in GitHub Desktop.
Save akru/21a7ed7510dab06878d8 to your computer and use it in GitHub Desktop.
PS2 Controller to RC PWM signal converter based on Arduino Pro Mini
#include <PS2X_lib.h>
#include <Servo.h>
#define CLK_PIN A3
#define CMD_PIN A2
#define DAT_PIN A1
#define ATT_PIN A0
#define CH1_PIN 3
#define CH2_PIN 5
#define CH3_PIN 6
#define CH4_PIN 9
#define CH5_PIN 10
#define CH6_PIN 11
#define CHANNEL_COUNT 6
static const int pwm_out_pin[CHANNEL_COUNT] =
{CH1_PIN, CH2_PIN, CH3_PIN, CH4_PIN, CH5_PIN, CH6_PIN};
static Servo pwm_out[CHANNEL_COUNT];
static int ch_in[CHANNEL_COUNT];
static int ch_out[CHANNEL_COUNT];
static bool ch_reverse[CHANNEL_COUNT];
static enum { START, LAND } mode;
static PS2X ps2x; // create PS2 Controller Class
static const bool DEBUG = false;
void setup() {
Serial.begin(115200);
// Init channels
for (short i = 0; i < CHANNEL_COUNT; ++i) {
ch_out[i] = 900;
ch_reverse[i] = false;
pwm_out[i].attach(pwm_out_pin[i]);
}
// Init reverse channels
ch_reverse[1] = ch_reverse[2] = true;
// Init gamepad
// Setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
int error = ps2x.config_gamepad(CLK_PIN,CMD_PIN,ATT_PIN,DAT_PIN, true, true);
if (error)
error_description(error);
else
Serial.println("Found Controller, configured successful");
}
void loop() {
// Rkead controller and set large motor to spin at 'vibrate' speed
ps2x.read_gamepad(false, 0);
// Get channel values
// YPR axes
ch_in[0] = ps2x.Analog(PSS_RX);
ch_in[1] = ps2x.Analog(PSS_RY);
ch_in[2] = ps2x.Analog(PSS_LY);
ch_in[3] = ps2x.Analog(PSS_LX);
// Mode switch
if (ps2x.Button(PSB_START))
mode = START;
else if (ps2x.Button(PSB_SELECT))
mode = LAND;
switch (mode) {
case START:
ch_in[4] = 255;
break;
case LAND:
ch_in[4] = 0;
}
// Fire button
ch_in[5] = ps2x.Button(PSB_R1) ? 255 : 0;
// Write channels
for(short i = 0; i < CHANNEL_COUNT; ++i) {
// Mapping from PS2 input to microseconds
ch_out[i] = map(ch_reverse[i] ? 255 - ch_in[i] : ch_in[i], 0, 255, 1000, 2000);
// Write PWM
pwm_out[i].writeMicroseconds(ch_out[i]);
// Debug output
if (DEBUG) {
Serial.print(" CH");
Serial.print(i+1);
Serial.print(": ");
Serial.print(ch_out[i]);
}
}
if (DEBUG)
Serial.print("\n");
delay(50);
}
void error_description(int error) {
switch (error) {
case 1:
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
break;
case 2:
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
break;
case 3:
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
break;
default:
Serial.println("Unknown error!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment