ps2连接arduino示意图
代码特点:
a0 a1 两个舵机会自动归位,a2 a3 不会自动归位,根据自己的需求可以调整
/******************************************************************
* Arduino四自由度机器人手臂遥控程序源代码-薛成编译通过
* 请将库文件解压后以文件夹格式拖至Arduino文件夹下的Libraries文件夹里
******************************************************************/
#define PS2_DAT 12
#define PS2_CMD 11
#define PS2_CS 10
#define PS2_CLK 13
#include "PS2X_lib.h"
#include <Servo.h>
Servo servo_A0;
Servo servo_A1;
Servo servo_A2;
Servo servo_A3;
PS2X ps2x;
void setup(){
ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_CS, PS2_DAT, true, true);
servo_A0.attach(A0);
servo_A1.attach(A1);
servo_A2.attach(A2);
servo_A3.attach(A3);
Serial.begin(9600);
servo_A0.write(90);
delay(10);
servo_A1.write(90);
delay(10);
servo_A2.write(90);
delay(10);
servo_A3.write(90);
delay(10);
}
void loop(){
ps2x.read_gamepad(false,0);
if (ps2x.Analog(PSS_LX) > 130) {
servo_A0.write((servo_A0.read() + 1));
delay(10);
}
if (ps2x.Analog(PSS_LX) < 120) {
servo_A0.write((servo_A0.read() - 1));
delay(10);
}
if (ps2x.Analog(PSS_LY) > 130) {
servo_A1.write((servo_A1.read() + 1));
delay(10);
}
if (ps2x.Analog(PSS_LY) < 120) {
servo_A1.write((servo_A1.read() - 1));
delay(10);
}
if (ps2x.Analog(PSS_RX) > 130) {
servo_A2.write((servo_A2.read() + 1));
delay(10);
}
if (ps2x.Analog(PSS_RX) < 120) {
servo_A2.write((servo_A2.read() - 1));
delay(10);
}
if (ps2x.Analog(PSS_RY) > 130) {
servo_A3.write((servo_A3.read() + 1));
delay(10);
}
if (ps2x.Analog(PSS_RY) < 120) {
servo_A3.write((servo_A3.read() - 1));
delay(10);
}
}