Spikes in PS2 joystick readings
Posted: Tue Jun 08, 2021 1:48 pm
I just received a wireless PS2 controller from Cytron and have begun to run some tests on it with the SHIELD-MDD10 driving 2 motors and the PS2X library. What I found was that even with no input to the joysticks, the readings in the serial monitor will spike to maximum occasionally. This causes the motors to jerk when it happens. Below is the code I am using for the test.
Serial monitor output.
What is causing these spikes? Is this normal or could the fault lie with the controller? What else can I try to check? Would Cytron be able to test out my code with a PS2 controller to see if output behaviour is the same?
Thanks,
Alan
- CODE: SELECT_ALL_CODE
#include <PS2X_lib.h> // library for PS2 controller
#include "CytronMotorDriver.h" // library for Cytron SHIELD-MDD10 driver
/******************************************************************
* set pins connected to PS2 controller:
******************************************************************/
#define PS2_DAT 13
#define PS2_CMD 12
#define PS2_SEL 11
#define PS2_CLK 10
/******************************************************************
* select modes of PS2 controller:
* - pressures = analog reading of push-butttons
* - rumble = motor rumbling
* uncomment 1 of the lines for each mode selection
******************************************************************/
#define pressures false
#define rumble false
PS2X ps2x; // create PS2 Controller Class
//right now, the library does NOT support hot pluggable controllers, meaning
//you must always either restart your Arduino after you connect the controller,
//or call config_gamepad(pins) again after connecting the controller.
CytronMD motor1(PWM_DIR, 3, 2); // PWM 1 = Pin 3, DIR 1 = Pin 2
CytronMD motor2(PWM_DIR, 6, 7); // PWM 2 = Pin 6, DIR 2 = Pin 7
int error = 0;
byte type = 0;
byte vibrate = 0;
void setup(){
Serial.begin(57600);
delay(300); //added delay to give wireless ps2 module some time to startup, before configuring it
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
if(error == 0){
Serial.print("Found Controller, configured successful ");
Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
Serial.println("holding L1 or R1 will print out the analog stick values.");
Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
}
else if(error == 1)
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
else if(error == 2)
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
else if(error == 3)
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
type = ps2x.readType();
switch(type) {
case 0:
Serial.print("Unknown Controller type found ");
break;
case 1:
Serial.print("DualShock Controller found ");
break;
case 2:
Serial.print("GuitarHero Controller found ");
break;
case 3:
Serial.print("Wireless Sony DualShock Controller found ");
break;
}
}
void loop() {
if(error == 1) //skip loop if no controller found
return;
ps2x.read_gamepad(false, vibrate); // disable vibration of the controller
int nJoyL = ps2x.Analog(PSS_LY); // read left stick; 0 - 255 value
int nJoyR = ps2x.Analog(PSS_RY); // read right stick
// nJoyL = map(nJoyL, 0, 255, 255, -255); // 0 = stop; 255 = full forward; -255 = full reverse
// nJoyR = map(nJoyR, 0, 255, 255, -255);
Serial.print("L : "); Serial.print(nJoyL);
Serial.print(" R : "); Serial.println(nJoyR);
motor1.setSpeed(nJoyR); // Motor 1
motor2.setSpeed(nJoyL); // Motor 2
delay(50);
}
Serial monitor output.
- CODE: SELECT_ALL_CODE
11:39:07.042 -> L : 128 R : 128
11:39:07.088 -> L : 128 R : 128
11:39:07.134 -> L : 128 R : 128
11:39:07.228 -> L : 128 R : 128
11:39:07.274 -> L : 128 R : 128
11:39:07.320 -> L : 128 R : 128
11:39:07.367 -> L : 128 R : 128
11:39:07.413 -> L : 128 R : 128
11:39:07.460 -> L : 128 R : 128
11:39:07.553 -> L : 128 R : 128
11:39:07.599 -> L : 128 R : 128
11:39:07.646 -> L : 128 R : 128
11:39:07.692 -> L : 128 R : 128
11:39:07.739 -> L : 128 R : 128
11:39:07.786 -> L : 128 R : 128
11:39:07.833 -> L : 128 R : 128
11:39:07.925 -> L : 128 R : 128
11:39:07.972 -> L : 128 R : 128
11:39:08.018 -> L : 255 R : 255
11:39:08.066 -> L : 128 R : 128
11:39:08.111 -> L : 128 R : 128
11:39:08.158 -> L : 128 R : 128
11:39:08.253 -> L : 128 R : 128
11:39:08.299 -> L : 128 R : 128
11:39:08.345 -> L : 128 R : 128
11:39:08.392 -> L : 128 R : 128
11:39:08.439 -> L : 128 R : 128
11:39:08.485 -> L : 128 R : 128
11:39:08.532 -> L : 128 R : 128
11:39:08.625 -> L : 128 R : 128
11:39:08.672 -> L : 128 R : 128
11:39:08.718 -> L : 128 R : 128
11:39:08.764 -> L : 128 R : 128
11:39:08.811 -> L : 128 R : 128
11:39:08.858 -> L : 128 R : 128
11:39:08.951 -> L : 128 R : 128
11:39:08.999 -> L : 128 R : 128
11:39:09.045 -> L : 128 R : 128
11:39:09.092 -> L : 128 R : 128
11:39:09.138 -> L : 128 R : 128
11:39:09.185 -> L : 128 R : 128
11:39:09.278 -> L : 255 R : 255
11:39:09.324 -> L : 128 R : 128
11:39:09.370 -> L : 128 R : 128
11:39:09.417 -> L : 128 R : 128
11:39:09.464 -> L : 128 R : 128
11:39:09.511 -> L : 128 R : 128
11:39:09.558 -> L : 128 R : 128
11:39:09.651 -> L : 128 R : 128
11:39:09.698 -> L : 128 R : 128
11:39:09.745 -> L : 128 R : 128
11:39:09.792 -> L : 128 R : 128
11:39:09.839 -> L : 128 R : 128
11:39:09.885 -> L : 128 R : 128
11:39:09.979 -> L : 128 R : 128
11:39:10.028 -> L : 128 R : 128
11:39:10.074 -> L : 128 R : 128
11:39:10.121 -> L : 128 R : 128
11:39:10.166 -> L : 128 R : 128
11:39:10.214 -> L : 128 R : 128
11:39:10.308 -> L : 128 R : 128
11:39:10.353 -> L : 128 R : 128
11:39:10.400 -> L : 128 R : 128
11:39:10.446 -> L : 128 R : 128
11:39:10.493 -> L : 128 R : 128
11:39:10.539 -> L : 128 R : 128
11:39:10.586 -> L : 128 R : 128
11:39:10.679 -> L : 128 R : 128
11:39:10.726 -> L : 128 R : 128
11:39:10.772 -> L : 128 R : 128
11:39:10.819 -> L : 128 R : 128
11:39:10.865 -> L : 128 R : 128
11:39:10.912 -> L : 128 R : 128
11:39:11.005 -> L : 128 R : 128
11:39:11.052 -> L : 128 R : 128
11:39:11.100 -> L : 128 R : 128
11:39:11.146 -> L : 128 R : 128
11:39:11.193 -> L : 128 R : 128
11:39:11.240 -> L : 128 R : 128
11:39:11.287 -> L : 128 R : 128
11:39:11.379 -> L : 255 R : 255
11:39:11.426 -> L : 255 R : 255
11:39:11.473 -> L : 128 R : 128
11:39:11.520 -> L : 128 R : 128
11:39:11.566 -> L : 128 R : 128
11:39:11.614 -> L : 128 R : 128
11:39:11.707 -> L : 128 R : 128
11:39:11.753 -> L : 128 R : 128
11:39:11.799 -> L : 128 R : 128
11:39:11.846 -> L : 128 R : 128
11:39:11.892 -> L : 128 R : 128
11:39:11.939 -> L : 128 R : 128
11:39:11.986 -> L : 128 R : 128
11:39:12.078 -> L : 128 R : 128
11:39:12.126 -> L : 128 R : 128
11:39:12.173 -> L : 128 R : 128
11:39:12.221 -> L : 128 R : 128
11:39:12.267 -> L : 128 R : 128
11:39:12.315 -> L : 128 R : 128
11:39:12.363 -> L : 128 R : 128
11:39:12.458 -> L : 128 R : 128
11:39:12.505 -> L : 128 R : 128
11:39:12.551 -> L : 128 R : 128
11:39:12.599 -> L : 128 R : 128
11:39:12.645 -> L : 128 R : 128
11:39:12.692 -> L : 128 R : 128
11:39:12.739 -> L : 128 R : 128
11:39:12.832 -> L : 128 R : 128
11:39:12.878 -> L : 128 R : 128
11:39:12.925 -> L : 128 R : 128
11:39:12.972 -> L : 128 R : 128
What is causing these spikes? Is this normal or could the fault lie with the controller? What else can I try to check? Would Cytron be able to test out my code with a PS2 controller to see if output behaviour is the same?
Thanks,
Alan