Top Ad unit 728 × 90


PS2 Controller used to control DC motor

I. How to connect
                     

For robot control, just use the following wires: Clock, Data, Command, VCC & GND, Attention. Clock, Data, Command, Attention connected to any I / O pins. Data pins should be pulled by resistor from 1k-10k. Clock: pulse, synchronize the data transfer process. Data: data from gamepad on Microcontroller; Command: data from Microcontroller to gamepad.

Attention: Chip select VCC: 3-5V; GND: 0V

In this article, I connect as follows:
PlayStation 2Arduino UNO
DATA13
Command11
Attention10
Clock12
Vcc5V
GNDGND


III. Program
To work with PS2, you need to download the PS2X_lib library here: DOWNLOAD PS2 LIB
Code ( Control DC motor though module L298N ) :
#include <PS2X_lib.h>
#define PS2_DAT 13 // data
#define PS2_CMD 11 //command
#define PS2_SEL 10 // attention
#define PS2_CLK 12 //clock
//#define pressures   true
#define pressures false
//#define rumble      true
#define rumble false
PS2X ps2x; // create PS2 class
int error = 0;
byte type = 0;
byte vibrate = 0;
//variable for analog
int temp1;
int temp2;
int temp3;
int temp4;
int temp33;
int temp44;
void setup()
{
    //define motor control pin
    pinMode(3, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(9, OUTPUT);
    Serial.begin(57600);
    delay(300);
    error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
    if (error == 0) {
        Serial.print("Found controller ");
        Serial.print("pressures = ");
        if (pressures)
            Serial.println("true ");
        else
            Serial.println("false");
        Serial.print("rumble = ");
        if (rumble)
            Serial.println("true)");
        else
            Serial.println("false");
        Serial.println("Try all button;");
        Serial.println("KEEP L1 or R1 will print analog value.");
    }
    else if (error == 1)
        Serial.println("Cant connect, pls try again...");
    else if (error == 2)
        Serial.println("Found controller however cannot received cmd");
    else if (error == 3)
        Serial.println("Controller rejected Pressures ");
    //  Serial.print(ps2x.Analog(1), HEX);
    type = ps2x.readType();
    switch (type) {
    case 0:
        Serial.print("Controller not suitable ");
        break;
    case 1:
        Serial.print("Found DualShock ");
        break;
    case 2:
        Serial.print("Found GuitarHero ");
        break;
    case 3:
        Serial.print("Found wifi Sony DualShock");
        break;
    }
}
void loop()
{
    //Cho về 0 xét lại
    temp1 = 0;
    temp2 = 0;
    temp3 = 0;
    temp4 = 0;
    if (error == 1)
        return;
    if (type == 2) {
        ps2x.read_gamepad();
    }
    else { //DualShock Controller
        ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
        if (ps2x.Analog(PSS_LY) > 128) {
            temp1 = (ps2x.Analog(PSS_LY) - 128) * 2;
        }
        else if (ps2x.Analog(PSS_LY) < 128) {
            temp2 = abs(ps2x.Analog(PSS_LY) - 128) * 2;
            if (temp2 > 255)
                temp2 = 255;
        }
        else {
            temp2 = 0;
        }
        if (ps2x.Analog(PSS_RX) > 128) {
            temp3 = (ps2x.Analog(PSS_RX) - 128) * 2;
        }
        else if (ps2x.Analog(PSS_RX) < 128) {
            temp4 = abs(ps2x.Analog(PSS_RX) - 128) * 2;
            if (temp2 > 255)
                temp4 = 255;
        }
        else {
            temp4 = 0;
        }
        //    Serial.println (temp1);
        //    Serial.println (temp2);
        //    Serial.println (temp3);
        //    Serial.println (temp4);
    }
    //Quy định
    // + pin 3 - pin 5 1 động cơ bên trái quay tới
    // + pin 6 - pin 9 1 động cơ bên phải quay tới
    if ((temp2 > 0) && temp3 == 0 && temp4 == 0) {
        analogWrite(3, temp2);
        digitalWrite(5, LOW);
        analogWrite(6, temp2);
        digitalWrite(9, LOW);
    }
    else {
        if ((temp1 > 0) && temp3 == 0 && temp4 == 0) {
            digitalWrite(3, LOW);
            analogWrite(5, temp1);
            digitalWrite(6, LOW);
            analogWrite(9, temp1);
        }
        else {
            if ((temp2 > 0) && temp4 > 0) {
                temp44 = (temp2 - temp4);
                if (temp44 < 0) {
                    temp44 = 0;
                }
                analogWrite(3, temp44);
                digitalWrite(5, LOW);
                analogWrite(6, temp2);
                digitalWrite(9, LOW);
            }
            else {
                if ((temp2 > 0) && temp3 > 0) {
                    temp33 = (temp2 - temp3);
                    if (temp33 < 0) {
                        temp33 = 0;
                    }
                    analogWrite(3, temp2);
                    digitalWrite(5, LOW);
                    analogWrite(6, temp33);
                    digitalWrite(9, LOW);
                }
                else {
                    if ((temp1 > 0) && temp4 > 0) {
                        temp44 = (temp1 - temp4);
                        if (temp44 < 0) {
                            temp44 = 0;
                        }
                        analogWrite(3, LOW);
                        digitalWrite(5, temp44);
                        analogWrite(6, LOW);
                        digitalWrite(9, temp1);
                    }
                    else {
                        if ((temp1 > 0) && temp3 > 0) {
                            temp33 = (temp1 - temp3);
                            if (temp33 < 0) {
                                temp33 = 0;
                            }
                            analogWrite(3, LOW);
                            digitalWrite(5, temp33);
                            analogWrite(6, LOW);
                            digitalWrite(9, temp1);
                        }
                        else {
                            digitalWrite(3, LOW);
                            digitalWrite(5, LOW);
                            digitalWrite(6, LOW);
                            digitalWrite(9, LOW);
                        }
                    }
                }
            }
        }
    }
}
PS2 Controller used to control DC motor Reviewed by Jacky on September 16, 2017 Rating: 5

1 comment:

  1. Hello I have tried to run the example code using an third party controller, I made sure the wiring is correct, I added the register between data and vcc , everything is correct, I even changes the ctrl clk value to 20, even tested it for 2, But Iam getting the following error, I debugged it.
    OUT:IN Configure
    1:FF 43:FF 0:FF 1:FF 0:FF
    OUT:IN Configure
    1:FF 44:FF 0:FF 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 0:FF 5A:FF 5A:FF 5A:FF 5A:FF 5A:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 1:FF 0:FF
    OUT:IN Configure
    1:FF 44:FF 0:FF 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 0:FF 5A:FF 5A:FF 5A:FF 5A:FF 5A:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 1:FF 0:FF
    OUT:IN Configure
    1:FF 44:FF 0:FF 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 0:FF 5A:FF 5A:FF 5A:FF 5A:FF 5A:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 1:FF 0:FF
    OUT:IN Configure
    1:FF 44:FF 0:FF 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 0:FF 5A:FF 5A:FF 5A:FF 5A:FF 5A:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 1:FF 0:FF
    OUT:IN Configure
    1:FF 44:FF 0:FF 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 0:FF 5A:FF 5A:FF 5A:FF 5A:FF 5A:FF
    OUT:IN
    1:FF 42:FF 0:FF 0:FF 0:FF 0:FF 0:FF 0:FF 0:FF 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0
    OUT:IN Configure
    1:FF 43:FF 0:FF 1:FF 0:FF
    OUT:IN Configure
    1:FF 44:FF 0:FF 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 0:FF 5A:FF 5A:FF 5A:FF 5A:FF 5A:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 1:FF 0:FF
    OUT:IN Configure
    1:FF 44:FF 0:FF 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 0:FF 5A:FF 5A:FF 5A:FF 5A:FF 5A:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 1:FF 0:FF
    OUT:IN Configure
    1:FF 44:FF 0:FF 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 0:FF 5A:FF 5A:FF 5A:FF 5A:FF 5A:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 1:FF 0:FF
    OUT:IN Configure
    1:FF 44:FF 0:FF 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 0:FF 5A:FF 5A:FF 5A:FF 5A:FF 5A:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 1:FF 0:FF
    OUT:IN Configure
    1:FF 44:FF 0:FF 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
    OUT:IN Configure
    1:FF 43:FF 0:FF 0:FF 5A:FF 5A:FF 5A:FF 5A:FF 5A:FF
    OUT:IN
    1:FF 42:FF 0:FF 0:FF 0:FF 0:FF 0:FF 0:FF 0:FF 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0
    Controller mode not matched or no controller found
    Expected 0x41, 0x73 or 0x79, but got FF
    No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips
    Unknown Controller type found

    ReplyDelete

All Rights Reserved by JackyLe © 2018 © 2017
Edit bởi: Jacky Le | Youtube Channel: JACKY LE

Send email form

Name

Email *

Message *

Powered by Blogger.