The servo cannot rotate 180* for the stick to be moved
#include <PS2X_lib.h> //for v1.6 PS2 Library
#include <Servo.h>
Servo myservo;
int pos = 0;
/******************************************************************
set pins connected to PS2 controller:
- 1e column: original
- 2e colmun: Stef?
replace pin numbers by the ones you use
******************************************************************/
#define PS2_DAT 12
#define PS2_CMD 11
#define PS2_SEL 10
#define PS2_CLK 9
//pin for L298N module driver
#define leftm1 3
#define leftm2 4
#define rightm1 5
#define rightm2 6
#define left_EN 2
#define right_EN 7
//analog values read from PS2 joysticks start 0
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;
int val;
void setup() {
Serial.begin(9600);
myservo.attach(8); //Pin servo
//It requires naming the output so that it can be processed by the L298N driver
pinMode(leftm1, OUTPUT);
pinMode(leftm2, OUTPUT);
pinMode(rightm1, OUTPUT);
pinMode(rightm2, OUTPUT);
pinMode(left_EN, OUTPUT);
pinMode(right_EN, OUTPUT);
digitalWrite(left_EN, LOW);
digitalWrite(right_EN, LOW);
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;
}
}
void loop() {
ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
delay(50);
//PSB_L1 is the L1 button on the joystick
if (ps2x.Button(PSB_L1)) {
//PSS LY and PSS LX are codes for actuation. LEFT
LY = ps2x.Analog(PSS_LY); //receive values from p22 joystick
LX = ps2x.Analog(PSS_LX);
}
//PSB_R1 is the R1 button on the joystick
if (ps2x.Button(PSB_R1)) {
//PSS RY and PSS RX are codes for actuation. RIGHT
RY = ps2x.Analog(PSS_RY);
RX = ps2x.Analog(PSS_RX);
}
//Motor driver
if (LY > 200) //check if the joystick pushed up side
{
REV();
}
if (LY < 100) {
forward();
}
if (LX < 100) {
left();
}
if (LX > 200) {
right();
}
if (LX == 128 && LY == 128) {
waithere();
}
//Servo
if (RY > 200) //check if the joystick pushed up side
{
val = RY;
val = map(val, 0, 1023, 0, 180); // scale it to use it with the myservo (val between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15);
}
if (RY < 100) {
val = RY;
val = map(val, 0, 1023, 0, 180); // scale it to use it with the myservo (val between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15);
}
if (RX < 100) {
val = RX;
val = map(val, 0, 1023, 0, 180); // scale it to use it with the myservo (val between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15);
}
if (RX > 200) {
val = RX;
val = map(val, 0, 1023, 0, 180); // scale it to use it with the myservo (val between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(150);
}
if (RX == 128 && RY == 128) {
val = RX;
val = map(val, 0, 1023, 0, 180); // scale it to use it with the myservo (val between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(150);
}
LY = LX = 128; //return to default vlaues
RY = RX = 128; //return to default values
}
void forward() {
//Serial.print("forward");
digitalWrite(left_EN, HIGH);
digitalWrite(right_EN, HIGH);
digitalWrite(leftm1, HIGH);
digitalWrite(leftm2, LOW);
digitalWrite(rightm1, HIGH);
digitalWrite(rightm2, LOW);
}
void REV() {
//Serial.print("rev");
digitalWrite(left_EN, HIGH);
digitalWrite(right_EN, HIGH);
digitalWrite(leftm1, LOW);
digitalWrite(leftm2, HIGH);
digitalWrite(rightm1, LOW);
digitalWrite(rightm2, HIGH);
}
void left() {
//Serial.print("left");
digitalWrite(left_EN, LOW);
digitalWrite(right_EN, HIGH);
digitalWrite(leftm1, LOW);
digitalWrite(leftm2, LOW);
digitalWrite(rightm1, HIGH);
digitalWrite(rightm2, LOW);
}
void right() {
//Serial.print("right");
digitalWrite(left_EN, HIGH);
digitalWrite(right_EN, LOW);
digitalWrite(rightm1, LOW);
digitalWrite(rightm2, LOW);
digitalWrite(leftm1, HIGH);
digitalWrite(leftm2, LOW);
}
void waithere() {
//Serial.print("stop");
digitalWrite(left_EN, LOW);
digitalWrite(right_EN, LOW);
digitalWrite(rightm1, LOW);
digitalWrite(rightm2, LOW);
digitalWrite(leftm1, LOW);
digitalWrite(leftm2, LOW);
}
2 posts - 2 participants