我们不仅销售产品,我们还传播知识
商品分类

PS2遥控四自由度控制方案

ps2连接arduino示意图

PS2遥控四自由度控制方案
PS2遥控四自由度控制方案
PS2遥控四自由度控制方案
https://www.haibucuo.com/product/12529/
https://www.haibucuo.com/download/ps2arduino%e4%bb%a3%e7%a0%81/embed#?secret=jA0F8lEtPP

代码特点:

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);

  }

}

发表回复

登录后才能评论
微信群
微信群
联系我们

联系我们

微信:13823392571

在线咨询:点击这里给我发消息

工作时间:周一至周五,9:30-18:30,节假日休息

微信客服
微信客服
分享本页
返回顶部