Arduino with ps2 for 2-motor control

Thread Starter

beatsal

Joined Jan 21, 2018
392
I am trying to program a UNO to control a robot to move forward and back using a ps2 with joystick. I want to control the speed by moving the js on x-axis and direction by moving js on y axis. The foll. code works but without js controls. Now want to include js controls. Any help appreciated.
C:
/<
void setup() {
 
  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
    //Setup Channel B
  pinMode(13, OUTPUT); //Initiates Motor Channel B pin
  }
void loop(){
 
     //Motor A forward @ full speed
  digitalWrite(12, LOW);  //Establishes backward direction of Channel A
    analogWrite(3, 255);    //Spins the motor on Channel A at half speed
    //Motor B forward @ full speed
  digitalWrite(13, HIGH); //Establishes forward direction of Channel B
    analogWrite(11, 255);   //Spins the motor on Channel B at full speed
      delay(1000);}   >/
Moderatots note : please use code tags for pieces of code
 

KeithWalker

Joined Jul 10, 2017
3,063
Do you have a program on the PS2 that will output joystick X and Y positions?
How do you plan on communicating them between the PS2 and the UNO?
 

Thread Starter

beatsal

Joined Jan 21, 2018
392
I have a program for that which includes LCD display and other stuff. I will be using only the joystick movements of this program i.e. x-axis and y-axis. This has to blend with the program I posted earlier.
/<
#include <SoftwareSerial.h>
#include <Cytron_PS2Shield.h>
Cytron_PS2Shield ps2(2, 3);
#include <LiquidCrystal.h>
//LCD pin to Arduino
// tested on 26/6, only left motor runs continu
const int pin_RS = 8;
const int pin_EN = 9;
const int pin_d4 = 4;
const int pin_d5 = 5;
const int pin_d6 = 6;
const int pin_d7 = 7;
const int pin_BL = 10;
char line1[16];
char line2[16];
int x = 0;
int y = 0;
int dir = 1;
LiquidCrystal lcd( pin_RS, pin_EN, pin_d4, pin_d5, pin_d6, pin_d7);
void setup() {
// put your setup code here, to run once:
ps2.begin(9600);
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
}
void loop() {
x = ps2.readButton(PS2_JOYSTICK_LEFT_Y_AXIS) - 127;

if (x >= 0)
dir = -1;
else
dir = 1;
y = abs(x);
lcd.setCursor(0, 0);
sprintf(line1, "%d ", x);
lcd.print(line1);
//Serial.println(line1);
Serial.println(y);
Serial.println(dir);
lcd.setCursor(0, 1);
sprintf(line2, "%d" , y);
//lcd.print(line2);
Serial.println( ps2.readButton(PS2_JOYSTICK_LEFT_X_AXIS));
if ( ps2.readButton(PS2_JOYSTICK_LEFT_X_AXIS) < 120 ) {
Serial.println("LEFT");
}
if ( ps2.readButton(PS2_JOYSTICK_LEFT_X_AXIS) > 134 ) {
Serial.println("RIGHT");
}

if ( ps2.readButton(PS2_JOYSTICK_LEFT_Y_AXIS) < 120 ) {
Serial.println("forward");
}
if ( ps2.readButton(PS2_JOYSTICK_LEFT_Y_AXIS) > 134 ) {
Serial.println("reverse");
}
delay(1000);
}
>/
 

Thread Starter

beatsal

Joined Jan 21, 2018
392
I have tried to combine the above 2 programs and came up with this. Any comments appreciated - thanks.
/< #include <SoftwareSerial.h>
#include <Cytron_PS2Shield.h>
Cytron_PS2Shield ps2(2, 3);
void loop(){
//Motor A forward @ full speed
digitalWrite(12, LOW); //Establishes backward direction of Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
//Motor B forward @ full speed
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
analogWrite(11, 255); //Spins the motor on Channel B at full speed
delay(1000);}
int x = 0;
int y = 0;
int dir = 1;
void setup() {
// put your setup code here, to run once:
ps2.begin(9600);
Serial.begin(9600);
//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
//Setup Channel B
pinMode(13, OUTPUT); //Initiates Motor Channel B pin }
}
void loop() {
x = ps2.readButton(PS2_JOYSTICK_LEFT_Y_AXIS) - 127;
if (x >= 0)
dir = -1;
else
dir = 1;
y = abs(x);
Serial.println(y);
Serial.println(dir);
Serial.println( ps2.readButton(PS2_JOYSTICK_LEFT_X_AXIS));
if ( ps2.readButton(PS2_JOYSTICK_LEFT_X_AXIS) < 120 ) {?????
Serial.println("LEFT");
}
>/
if ( ps2.readButton(PS2_JOYSTICK_LEFT_X_AXIS) > 134 ) {?????
Serial.println("RIGHT");
}
if ( ps2.readButton(PS2_JOYSTICK_LEFT_Y_AXIS) < 120 ) {
Serial.println("forward");
}
 

woozycactus

Joined Jan 4, 2021
104
I am trying to program a UNO to control a robot to move forward and back using a ps2 with joystick. I want to control the speed by moving the js on x-axis and direction by moving js on y axis. The foll. code works but without js controls. Now want to include js controls. Any help appreciated.
C:
/<
void setup() {

  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
    //Setup Channel B
  pinMode(13, OUTPUT); //Initiates Motor Channel B pin
  }
void loop(){

     //Motor A forward @ full speed
  digitalWrite(12, LOW);  //Establishes backward direction of Channel A
    analogWrite(3, 255);    //Spins the motor on Channel A at half speed
    //Motor B forward @ full speed
  digitalWrite(13, HIGH); //Establishes forward direction of Channel B
    analogWrite(11, 255);   //Spins the motor on Channel B at full speed
      delay(1000);}   >/
Moderatots note : please use code tags for pieces of code
is this what your looking for?

int speedPin=5;
int dir1=4;
int dir2=3;
int mSpeed;
int jPin=A1;
int jVal;

void setup() {
// put your setup code here, to run once:
pinMode(speedPin,OUTPUT);
pinMode(dir1,OUTPUT);
pinMode(dir2,OUTPUT);
pinMode(jPin,INPUT);
Serial.begin(9600);

}

void loop() {
// put your main code here, to run repeatedly:
jVal=analogRead(jPin);

Serial.println(jVal);
if (jVal<512){
digitalWrite(dir1,LOW);
digitalWrite(dir2,HIGH);
mSpeed=-255./512.*jVal+255.;
analogWrite(speedPin,mSpeed);
}
if(jVal>=512){
digitalWrite(dir1,HIGH);
digitalWrite(dir2,LOW);
mSpeed=(255./512.)*jVal-255.;
analogWrite(speedPin,mSpeed);
}
}
 

Thread Starter

beatsal

Joined Jan 21, 2018
392
is this what your looking for?

int speedPin=5;
int dir1=4;
int dir2=3;
int mSpeed;
int jPin=A1;
int jVal;

void setup() {
// put your setup code here, to run once:
pinMode(speedPin,OUTPUT);
pinMode(dir1,OUTPUT);
pinMode(dir2,OUTPUT);
pinMode(jPin,INPUT);
Serial.begin(9600);

}

void loop() {
// put your main code here, to run repeatedly:
jVal=analogRead(jPin);

Serial.println(jVal);
if (jVal<512){
digitalWrite(dir1,LOW);
digitalWrite(dir2,HIGH);
mSpeed=-255./512.*jVal+255.;
analogWrite(speedPin,mSpeed);
}
if(jVal>=512){
digitalWrite(dir1,HIGH);
digitalWrite(dir2,LOW);
mSpeed=(255./512.)*jVal-255.;
analogWrite(speedPin,mSpeed);
}
}
Yes, thanks, just what I'm looking for. Will try it out if it works.
 

Thread Starter

beatsal

Joined Jan 21, 2018
392
I tried it and it compiled and uploaded fine but when I tried to run the motors using the ps2 joystick, nothing happened. Not sure what the problem is.
 

Thread Starter

beatsal

Joined Jan 21, 2018
392
I know this program below works directly (without ps2 or joystick) as it turns both motors. Pins 12, 13 establish the dir. and pins 3, 11 the speed {PWM). I use a motor shield as I drive 12 VDC motors.

/< pinMode(12, OUTPUT); //Initiates Motor Channel A pin
//Setup Channel B
pinMode(13, OUTPUT); //Initiates Motor Channel B pin
}
void loop(){
//Motor A forward @ full speed
digitalWrite(12, LOW); //Establishes backward direction of Channel A
analogWrite(3, 255); //Spins the motor on Channel A
//Motor B forward @ full speed
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
analogWrite(11, 255); //Spins the motor on Channel B at full speed
delay(1000);} >/
 

KeithWalker

Joined Jul 10, 2017
3,063
Do it in small steps. First get the results from the PS2 to see what is happening. What is the communications link between the PS2 and the Arduino?
Make a simple program to run on the Arduino that will receive the joystick signals from the PS2 and display the values on the computer monitor. Once you have that working, it will be easy to combine with the motor program.
 

Thread Starter

beatsal

Joined Jan 21, 2018
392
I am not able to write the js/ps2 position, it jumps all over the place. Trying to write the js current position to 2 output pins to control 2 motors vis a arduino motor shield.
C:
/> //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin dir
  pinMode(13, OUTPUT); //Initiates Motor Channel B pin dir
  digitalWrite(12, LOW);  //Establishes backward direction of Channel A
  digitalWrite(13, HIGH); //Establishes forward direction of Channel B
      //Setup Channel B
         }
void loop(){
    //Motor A forward @ full speed
  //if ( ps2.readButton(PS2_JOYSTICK_LEFT_Y_AXIS)  < 200 ) {
    //digitalWrite(12, LOW);  //Establishes backward direction of Channel A
  //digitalWrite(13, HIGH);  //Establishes backward direction of Channel B
x = ps2.readButton(PS2_JOYSTICK_LEFT_Y_AXIS) - 127;
    Serial.println(ps2.readButton(PS2_JOYSTICK_LEFT_Y_AXIS));
   //Serial.println(x);
     if (x > 220)
    dir = 1;
    else
    dir = -1;
    //Serial.println(dir);
  analogWrite(3, x);    //Spins the motor on Channel A
    //Motor B forward @ full speed
      analogWrite(11, x);   //Spins the motor on Channel B
      delay(1000);
}  >/
Moderators note : please use code tags for pieces of code.
 
Last edited by a moderator:

KeithWalker

Joined Jul 10, 2017
3,063
Until you get steady readings from the PS2 there is no point in trying to use them to control the motors. What results are you getting, and once again, what is the communications link between the PS2 an Arduino? I can't help you if I don't know what is going on.
 

Thread Starter

beatsal

Joined Jan 21, 2018
392
These are readings I get from serial monitor without changing js position. I would expect 128 steady as js is in center position.
1
224
254
128
240
 

Thread Starter

beatsal

Joined Jan 21, 2018
392
In this program, if I add the 2 lines below, I get erratic readings. If I remove them readings are steady.
//analogWrite(3, y); //Spins the motor on Channel A
//analogWrite(11, y); //Spins the motor on Channel B

#include <SoftwareSerial.h>
#include <Cytron_PS2Shield.h>
Cytron_PS2Shield ps2(2, 3);
#include <LiquidCrystal.h>
int x = 0;
int y = 0;
int dir = 1;
void setup() {
// put your setup code here, to run once:
ps2.begin(9600);
Serial.begin(9600);
//pinMode(12, OUTPUT); //Initiates Motor Channel A pin
//Setup Channel B
//pinMode(13, OUTPUT); //Initiates Motor Channel B pin

//digitalWrite(12, LOW); //Establishes backward direction of Channel A
//digitalWrite(13, HIGH); //Establishes forward direction of Channel B
//pinMode(3, OUTPUT);
//pinMode(11, OUTPUT);
}
void loop()
{
//pinMode(12, OUTPUT); //Initiates Motor Channel A pin
//Setup Channel B
//pinMode(13, OUTPUT); //Initiates Motor Channel B pin
//digitalWrite(12, LOW); //Establishes backward direction of Channel A
//digitalWrite(13, HIGH); //Establishes forward direction of Channel B
y = ps2.readButton(PS2_JOYSTICK_LEFT_X_AXIS);
//analogWrite(3, y); //Spins the motor on Channel A
//analogWrite(11, y); //Spins the motor on Channel B
Serial.println(y);
//digitalWrite(12, LOW); //Establishes backward direction of Channel A

delay(1000);
}
 

KeithWalker

Joined Jul 10, 2017
3,063
If I understand correctly, you are getting valid data from the joysticks on the PS2 as long as no motors are running.
What supply are you using to drive the motors? Is it the same supply that is connected to the Arduino?
I would suspect that the motors are causing intererence on the supply that is affecting the Arduino.
Can you give us a wiring diagram please so that we can see how it is connected.
 

Thread Starter

beatsal

Joined Jan 21, 2018
392
Thanks for your help. The problem was with the SW interrupt which was set at ps2(2,3) but pin 3 was used for the motor. Changed to 8,9 and works OK. Still not clear about the SW interrupt. Now, will have to work on controlled steering. If you have any ideas, please help.
 
Top