#include <PS2X_lib.h> //for v1.6 PS2 Library
#define PS2_DAT 12 //14
#define PS2_CMD 11 //15
#define PS2_SEL 10 //16
#define PS2_CLK 9 //17
#define dirPin 7
#define stepPin 8
#define dirPin1 4
#define stepPin1 5
#define stepsPerRevolution 3200
//analog values read from PS2 joysticks
int LX = 0;
int LY = 0;
int RX = 0;
int RY = 0;
/******************************************************************
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 true
//#define pressures false
//#define rumble true
#define rumble false
PS2X ps2x; // create PS2 Controller Class
int error = 0;
byte type = 0;
byte vibrate = 0;
void setup() {
Serial.begin(9600);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(stepPin1, OUTPUT);
pinMode(dirPin1, OUTPUT);
delay(300); //added delay to give wireless ps2 module some time to startup, before configuring it
//setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
if (error == 0) {
Serial.print("Found Controller, configured successful ");
}
else if (error == 1)
Serial.print("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
else if (error == 2)
Serial.print("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
else if (error == 3)
Serial.print("Controller refusing to enter Pressures mode, may not support it. ");
Serial.print(ps2x.Analog(1), HEX);
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;
error = 0;
type = 1;
}
}
void loop() {
ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
delay(50);
if (ps2x.Button(PSB_L1))
{
LY = ps2x.Analog(PSS_LY); //receive values from p22 joystick
LX = ps2x.Analog(PSS_LX);
}
if (ps2x.Button(PSB_R1))
{
RY = ps2x.Analog(PSS_RY);
RX = ps2x.Analog(PSS_RX);
}
if (LY > 200 || RY > 200) //check if the joystick pushed up side
{
Lfast();
}
if (LY < 100 || RY < 100)
{
Lslow();
}
if (LX < 100 || RX < 100)
{
Rslow();
}
if (LX > 200 || RX > 200)
{
Rfast();
}
if (LX == 128 && LY == 128 && RX == 128 && RY == 128)
{
Stop();
}
LY = LX = 128; //return to default vlaues
RY = RX = 128; //return to default values
}
void Lfast()
{
//Serial.print("Left fast");
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(200);
digitalWrite(stepPin, LOW);
delayMicroseconds(200);
}
void Lslow()
{
//Serial.print("Left slow");
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
void Rfast()
{
//Serial.print("Right fast");
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
void Rslow()
{
//Serial.print("Right slow");
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(200);
digitalWrite(stepPin, LOW);
delayMicroseconds(200);
}
void Stop()
{
//Serial.print("Stop");
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
}
//**************END**************************
Hi All,
This is my very first post on the forum. I usually try to figure everything out myself, but this time i gut stuck. I have a PS2 controller (wireless) hooked up to a arduino UNO, TB6560 stepper driver en nema 23 stepper motor. The code attached compiles nicely en works.. kinda. I want to use 1 joystick to control the direction and speed of the motor. I made a setup without a ps2 controller and just a analog joystick and this works fine. but now i want to do this via the ps2 controller.
The main issue here is that the motor speed is not what it should be when i use the ps2 controller. I think this "delay" has something to do with the communication between the ps2 controller and the arduino or stepper driver. Or i made a mistake in de code. I would appreciate any help.
Regards,
Mark (NL)
