#include <PS2X_lib.h> // Import the PS2 library
// L298N motor drive board
// Declare MotorA to the right
// Declare MotorB to the left
// L298N motor drive board
#define MotorA_I1 8 // Define I1 pin
#define MotorA_I2 9 // Define the I2 pin
#define MotorB_I3 10 // Define I3 pin
#define MotorB_I4 11 // Define I4 pin
#define MotorA_PWMA 5 // Define the ENA (PWM speed) pin
#define MotorB_PWMB 6 // Define the ENB (PWM speed) pin
// PS2 joystick
#define PS2_DAT A3 // Define the DATA pin
#define PS2_CMD A4 // define the COMMAND pin
#define PS2_ATT 7 // Define the ATTENTION pin
#define PS2_CLK 4 // define the CLOCK pin
PS2X ps2x; // Create a class entity for the PS2 controller
Unsigned long ps2x_tick = 0; // Declare timing parameters
Int ps2x_error = 0; // Declare the error code parameter
Void setup ()
{
Serial.begin (9600); / / open Serial port, communication rate of 9600 bps
// Set the motor control pin mode
PinMode (MotorA_I1, OUTPUT);
PinMode (MotorA_I2, OUTPUT);
PinMode (MotorB_I3, OUTPUT);
PinMode (MotorB_I4, OUTPUT);
PinMode (MotorA_PWMA, OUTPUT);
PinMode (MotorB_PWMB, OUTPUT);
/ / Ps2 controller pin settings; config_gamepad (clock pin, command pin, select the pin, data pin, whether to support analog keys, whether to support vibration)
Ps2x_error = ps2x.config_gamepad (PS2_CLK, PS2_CMD, PS2_ATT, PS2_DAT, false, false);
Switch (ps2x_error)
{
Case 0: // If there is no problem with the controller connection, the message is displayed.
Serial.print ("Found Controller, configured successful");
Break
Case 1: // The controller was not found and the error message was displayed.
Serial.print ("No controller found, check wiring, see readme.txt to enable debug.");
Break
Case 2: // found the controller, but did not accept the command, please refer to the program author's website debugging instructions.
Serial.print ("Controller found but not accepting commands. See readme.txt to enable debug.");
Break
Case 3: // The controller refuses to enter the analog sensing pressure mode, perhaps the reason the controller does not support.
Serial.print ("Controller refusing to enter pressures mode, may not support it.");
Break
}
AnalogWrite (MotorA_PWMA, 200); // set the motor (right) speed
AnalogWrite (MotorB_PWMB, 200); // set the motor (left) speed
}
Void loop ()
{
If (ps2x_error == 1) return; / / If you do not find any controller, then return.
If (millis () - ps2x_tick> 50) // Read the button every 50ms
{
Ps2x_tick = millis (); // record the current time
Ps2x.read_gamepad (); // read the controller
// ------------------------------------------------ --------------
// key control: "up", "down", "left" left turn, right turn right
// ------------------------------------------------ --------------
If (ps2x.ButtonPressed (PSB_PAD_UP)) // If the "Up" button is pressed
{
Serial.print ("\ r \ nPS2 button [UP] pressed");
Car_Advance (0);
Return;
}
If (ps2x.ButtonReleased (PSB_PAD_UP)) // If the "on" button is released
{
Delay (1); / / delay 1ms to prevent miscarriage of justice
Ps2x.read_gamepad (); // read the controller again
If (ps2x.Button (PSB_PAD_UP) == false) // confirm that the "on" button is released
{
Serial.print ("\ r \ nPS2 button [UP] released");
Car_Stop (0);
}
Return;
}
If (ps2x.ButtonPressed (PSB_PAD_DOWN)) // If the "Down" button is pressed
{
Serial.print ("\ r \ nPS2 button [DOWN] pressed");
Car_Back (0);
Return;
}
If (ps2x.ButtonReleased (PSB_PAD_DOWN)) // If the "down" button is released
{
Delay (1); / / delay 1ms to prevent miscarriage of justice
Ps2x.read_gamepad (); // read the controller again
If (ps2x.Button (PSB_PAD_DOWN) == false) // confirm that the "down" button is released
{
Serial.print ("\ r \ nPS2 button [DOWN] released");
Car_Stop (0);
}
Return;
}
If (ps2x.ButtonPressed (PSB_PAD_LEFT)) // If the "Left" button is pressed
{
Serial.print ("\ r \ nPS2 button [LEFT] pressed");
Car_turnL (0);
Return;
}
If (ps2x.ButtonReleased (PSB_PAD_LEFT)) // If the "left" button is released
{
Delay (1); / / delay 1ms to prevent miscarriage of justice
Ps2x.read_gamepad (); // read the controller again
If (ps2x.Button (PSB_PAD_LEFT) == false) // confirm that the "left" button is released
{
Serial.print ("\ r \ nPS2 button [LEFT] released");
Car_Stop (0);
}
Return;
}
If (ps2x.ButtonPressed (PSB_PAD_RIGHT)) // If the "right" button is pressed
{
Serial.print ("\ r \ nPS2 button [RIGHT] pressed");
Car_turnR (0);
Return;
}
If (ps2x.ButtonReleased (PSB_PAD_RIGHT)) // If the "right" button is released
{
Delay (1); / / delay 1ms to prevent miscarriage of justice
Ps2x.read_gamepad (); // read the controller again
If (ps2x.Button (PSB_PAD_RIGHT) == false) // confirm that the "right" button is released
{
Serial.print ("\ r \ nPS2 button [RIGHT] released");
Car_Stop (0);
}
Return;
}
// ------------------------------------------------ -------
// Joystick control: left and right joystick control the left and right motors
// ------------------------------------------------ -------
If (ps2x.Button (0xFFFF) == false) // If there is no button, detect the joystick
{
Static byte stick_ly = 128; // left joystick Y axis numerical variable
Static byte stick_ry = 128; // right joystick Y axis value variable
If (stick_ly! = Ps2x.Analog (PSS_LY)) // if the left joystick Y axis has changed
{
If ((ps2x.Analog (PSS_LY) <250)) || (ps2x.Analog (PSS_LY)> 250))
{
Delay (1); / / delay 1ms to prevent miscarriage of justice
Ps2x.read_gamepad (); // read the controller again
}
If (stick_ly! = Ps2x.Analog (PSS_LY)) // Make sure the left joystick Y axis has changed
{
Stick_ly = ps2x.Analog (PSS_LY); // record the left joystick Y axis value
Serial.print ("PS2 [LEFT] stick Y value:");
Serial.println (stick_ly, DEC);
If (stick_ly <126) // push the left joystick forward
{
AnticlockwiseL ();
}
Else if (stick_ly> 128) // left joystick push backwards
{
ClockwiseL ();
}
Else // left joystick
{
StopL ();
}
}
}
If (stick_ry! = Ps2x.Analog (PSS_RY)) // if the right joystick Y axis has changed
{
If ((ps2x.Analog (PSS_RY) <b) || (ps2x.Analog (PSS_RY)> 250))
{
Delay (1); / / delay 1ms to prevent miscarriage of justice
Ps2x.read_gamepad (); // read the controller again
}
If (stick_ry! = Ps2x.Analog (PSS_RY)) // confirm that the right joystick Y axis has changed
{
Stick_ry = ps2x.Analog (PSS_RY); // record the right joystick Y axis value
Serial.print ("PS2 [RIGHT] stick Y value:");
Serial.println (stick_ry, DEC);
If (stick_ry <126) // push the right joystick forward
{
ClockwiseR ();
}
Else if (stick_ry> 128) // push the right joystick backwards
{
AnticlockwiseR ();
}
Else // right joystick set
{
StopR ();
}
}
}
}
}
}
/ / Car forward
Void Car_Advance (int t)
{
ClockwiseR ();
AnticlockwiseL ();
Delay (t * 100);
}
/ / Car back
Void Car_Back (int t)
{
AnticlockwiseR ();
ClockwiseL ();
Delay (t * 100);
}
// The car turns left
Void Car_turnL (int t)
{
ClockwiseR ();
ClockwiseL ();
Delay (t * 100);
}
/ / Car right rotation
Void Car_turnR (int t)
{
AnticlockwiseR ();
AnticlockwiseL ();
Delay (t * 100);
}
// The car stops
Void Car_Stop (int t)
{
StopR ();
StopL ();
Delay (t * 100);
}
Void clockwiseR () // The right motor turns clockwise
{
DigitalWrite (MotorA_I1, HIGH);
DigitalWrite (MotorA_I2, LOW);
}
The anticlockwiseR () / / right motor turns counterclockwise
{
DigitalWrite (MotorA_I1, LOW);
DigitalWrite (MotorA_I2, HIGH);
}
Void stopR () // the right motor stops
{
DigitalWrite (MotorA_I1, HIGH);
DigitalWrite (MotorA_I2, HIGH);
}
Void clockwiseL () // left motor clockwise
{
DigitalWrite (MotorB_I3, LOW);
DigitalWrite (MotorB_I4, HIGH);
}
Void anticlockwiseL () // left motor turn counterclockwise
{
DigitalWrite (MotorB_I3, HIGH);
DigitalWrite (MotorB_I4, LOW);
}
Void stopL () // left motor stop
{
DigitalWrite (MotorB_I3, HIGH);
DigitalWrite (MotorB_I4, HIGH);
}