0% found this document useful (0 votes)
74 views6 pages

Robot Firefighter Code

This document describes code for a robot firefighter that can be controlled using a PS2 controller. The robot uses 4 motors - 2 for movement and 2 for rotation. The right joystick controls rotation and the left joystick controls movement in the forward, backward, and side to side directions. Buttons on the controller allow for additional controls like changing direction or moving forward and backward. The code reads input from the controller and uses if/else statements to determine which motors to run and in which direction based on the joystick and button positions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views6 pages

Robot Firefighter Code

This document describes code for a robot firefighter that can be controlled using a PS2 controller. The robot uses 4 motors - 2 for movement and 2 for rotation. The right joystick controls rotation and the left joystick controls movement in the forward, backward, and side to side directions. Buttons on the controller allow for additional controls like changing direction or moving forward and backward. The code reads input from the controller and uses if/else statements to determine which motors to run and in which direction based on the joystick and button positions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

វ.ិច.ប.ហ.

ស ជម្ពូវន័ កូដ

Robot Firefighter
#include <PS2X_lib.h>
#include <AFMotor.h>
#define speed2 190
#define speed3 190
#define speed_M1 15
#define speed_M4 150
PS2X ps2x;

AF_DCMotor motor2(2);
AF_DCMotor motor3(3);

AF_DCMotor motor1(1);//rotation
AF_DCMotor motor4(4);

int error = 0;
byte type = 0;
byte vibrate = 0;
int RY = 0;
int RX = 0;
int LY = 0;
int LX = 0;
int temp;
bool change = false;

រូប៉ ូត
៊ ពន្លត់អគ្គភ
ិ ័យ ទំ ព័ រ | 1
វ.ិច.ប.ហ.ស ជម្ពូវន័ កូដ

void setup()
{
Serial.begin(57600);
motor2.setSpeed(speed2);
motor3.setSpeed(speed3);
motor1.setSpeed(speed_M1);
motor4.setSpeed(speed_M4);
error = ps2x.config_gamepad(A3, A1, A0, A2, true, true); //(clock, command,
attention, data)
}

void loop()
{
ps2x.read_gamepad(false, vibrate);
if (ps2x.Button(PSB_R1))
{
RY = ps2x.Analog(PSS_RY);
RX = ps2x.Analog(PSS_RX);
//Serial.println(RY);
//Serial.println(RX);
}
if (RY > 200)
{
motor4.run(BACKWARD);
}
else if (RY < 100)
រូប៉ ូត
៊ ពន្លត់អគ្គភ
ិ ័យ ទំ ព័ រ | 2
វ.ិច.ប.ហ.ស ជម្ពូវន័ កូដ

{
motor4.run(FORWARD);
}
else if (RX < 100)
{
motor1.run(BACKWARD);
}
else if (RX > 200)
{
motor1.run(FORWARD);
}
else
{
motor1.run(RELEASE);
motor4.run(RELEASE);
RY = RX = 128; //return to default values
}
RY = RX = 128; //return to default values
//=======Left JoyStick======
if (ps2x.Button(PSB_L1))
{
change = true;
LY = ps2x.Analog(PSS_LY); //receive values from ps2 joystick
LX = ps2x.Analog(PSS_LX);
}

រូប៉ ូត
៊ ពន្លត់អគ្គភ
ិ ័យ ទំ ព័ រ | 3
វ.ិច.ប.ហ.ស ជម្ពូវន័ កូដ

else {
change = false;
}
//======Backward======
if (LY > 200)
{
motor2.run(BACKWARD);
motor3.run(BACKWARD);
}
//======Forward======
else if (LY < 100)
{
motor2.run(FORWARD);
motor3.run(FORWARD);
}
//======Turn Left======
else if (LX < 100)
{
motor2.run(BACKWARD);
motor3.run(FORWARD);
}
//======Turn Right======
else if (LX > 200)
{
motor2.run(FORWARD);

រូប៉ ូត
៊ ពន្លត់អគ្គភ
ិ ័យ ទំ ព័ រ | 4
វ.ិច.ប.ហ.ស ជម្ពូវន័ កូដ

motor3.run(BACKWARD);
}
else
{
motor2.run(RELEASE);
motor3.run(RELEASE);
LY = LX = 128; //return to default vlaues
}
LY = LX = 128; //return to default vlaues
//=========== End ==========
if (change == false)
{
if (ps2x.Button(PSB_PAD_UP))
{
temp = ps2x.Analog(PSAB_PAD_UP), DEC;
motor2.run(FORWARD);
motor3.run(FORWARD);
}

else if (ps2x.Button(PSB_PAD_RIGHT))
{
temp = ps2x.Analog(PSAB_PAD_RIGHT), DEC;
motor2.run(FORWARD);
motor3.run(BACKWARD);
}

រូប៉ ូត
៊ ពន្លត់អគ្គភ
ិ ័យ ទំ ព័ រ | 5
វ.ិច.ប.ហ.ស ជម្ពូវន័ កូដ

else if (ps2x.Button(PSB_PAD_LEFT))
{
temp = ps2x.Analog(PSAB_PAD_LEFT), DEC;
motor2.run(BACKWARD);
motor3.run(FORWARD);
}

else if (ps2x.Button(PSB_PAD_DOWN))
{
temp = ps2x.Analog(PSAB_PAD_DOWN), DEC;
motor2.run(BACKWARD);
motor3.run(BACKWARD);
}

else
{
motor2.run(RELEASE);
motor3.run(RELEASE);

}
}
delay(50);
}

រូប៉ ូត
៊ ពន្លត់អគ្គភ
ិ ័យ ទំ ព័ រ | 6

You might also like