PS2手柄遥控控制灯开关(arduino)

PS2手柄遥控控制灯开关(arduino)PS2手柄遥控控制灯开关(arduino)使用的材料arduino板子:ps2遥控手柄;一个5V供电的LDE;连线首先按照遥控手柄底座上的英文,你会发现一共有六根线,分别是GND,VCC,DAT,CMD,CS,CL。GND连接arduino板子上的gnd,vcc连接5V或者3.3v,其他四根线可以按照代码中的引脚设置连接,也可以自己更改。代码部分#include<PS…

大家好,又见面了,我是你们的朋友全栈君。

PS2手柄遥控控制灯开关(arduino)

使用的材料

arduino板子:
ps2遥控手柄;
一个5V供电的LDE;
在这里插入图片描述

连线

首先按照遥控手柄底座上的英文,你会发现一共有六根线,分别是GND,VCC,DAT,CMD,CS,CL。GND连接arduino板子上的gnd,vcc连接5V或者3.3v,其他四根线可以按照代码中的引脚设置连接,也可以自己更改。
在这里插入图片描述

代码部分


#include <PS2X_lib.h>  //for v1.6

/******************************************************************
 * set pins connected to PS2 controller:
 *   - 1e column: original 
 *   - 2e colmun: Stef?
 * replace pin numbers by the ones you use
 ******************************************************************/
#define PS2_DAT        13  //14    
#define PS2_CMD        11  //15
#define PS2_SEL        10  //16
#define PS2_CLK        12  //17
const int led = 7;         //LED灯其中一根线接gnd,一根接引脚7
int buttonstate = 0 ;      //初始化灯不亮
/******************************************************************
 * select modes of PS2 controller:
 *   - pressures = analog reading of push-butttons 
 *   - rumble    = motor rumbling
 * uncomment 1 of the lines for each mode selection
 ******************************************************************/
//#define pressures   true
#define pressures   false
//#define rumble      true
#define rumble      false

PS2X ps2x; // create PS2 Controller Class

//right now, the library does NOT support hot pluggable controllers, meaning 
//you must always either restart your Arduino after you connect the controller, 
//or call config_gamepad(pins) again after connecting the controller.

int error = 0;
byte type = 0;
byte vibrate = 0;

void setup(){
  pinMode(led,OUTPUT);
  
  Serial.begin(57600);
  
  delay(300);  //added delay to give wireless ps2 module some time to startup, before configuring it
   
  //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
  
  //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
  
  if(error == 0){
    Serial.print("Found Controller, configured successful ");
    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 out all the buttons, X will vibrate the controller, faster as you press harder;");
    Serial.println("holding L1 or R1 will print out the analog stick values.");
    Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
  }  
  else if(error == 1)
    Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
   
  else if(error == 2)
    Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");

  else if(error == 3)
    Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
  
//  Serial.print(ps2x.Analog(1), HEX);
  
  type = ps2x.readType(); 
  switch(type) {
    case 0:
      Serial.print("Unknown Controller type found ");
      break;
    case 1:
      Serial.print("DualShock Controller found ");
      break;
    case 2:
      Serial.print("GuitarHero Controller found ");
      break;
  case 3:
      Serial.print("Wireless Sony DualShock Controller found ");
      break;
   }
}

void open() {
  
}
void loop() {
  /* You must Read Gamepad to get new values and set vibration values
     ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
     if you don't enable the rumble, use ps2x.read_gamepad(); with no values
     You should call this at least once a second
   */  
  if(error == 1) //skip loop if no controller found
    return; 
  
  if(type == 2){ //Guitar Hero Controller
    ps2x.read_gamepad();          //read controller 
   
    if(ps2x.ButtonPressed(GREEN_FRET))
      Serial.println("Green Fret Pressed");
    if(ps2x.ButtonPressed(RED_FRET))
      Serial.println("Red Fret Pressed");
    if(ps2x.ButtonPressed(YELLOW_FRET))
      Serial.println("Yellow Fret Pressed");
    if(ps2x.ButtonPressed(BLUE_FRET))
      Serial.println("Blue Fret Pressed");
    if(ps2x.ButtonPressed(ORANGE_FRET))
      Serial.println("Orange Fret Pressed"); 

    if(ps2x.ButtonPressed(STAR_POWER))
      Serial.println("Star Power Command");
    
    if(ps2x.Button(UP_STRUM))          //will be TRUE as long as button is pressed
      Serial.println("Up Strum");
    if(ps2x.Button(DOWN_STRUM))
      Serial.println("DOWN Strum");
 
    if(ps2x.Button(PSB_START))         //will be TRUE as long as button is pressed
      Serial.println("Start is being held");
    if(ps2x.Button(PSB_SELECT))
      Serial.println("Select is being held");
    
    if(ps2x.Button(ORANGE_FRET)) {     // print stick value IF TRUE
      Serial.print("Wammy Bar Position:");
      Serial.println(ps2x.Analog(WHAMMY_BAR), DEC); 
    } 
  }
  else { //DualShock Controller
    ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
    
    if(ps2x.Button(PSB_START))         //will be TRUE as long as button is pressed
      Serial.println("Start is being held");
    if(ps2x.Button(PSB_SELECT))
      Serial.println("Select is being held");      

    if(ps2x.Button(PSB_PAD_UP)) {      //will be TRUE as long as button is pressed
      Serial.print("Up held this hard: ");
      Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
    }
    if(ps2x.Button(PSB_PAD_RIGHT)){
      Serial.print("Right held this hard: ");
      Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
    }
    if(ps2x.Button(PSB_PAD_LEFT)){
      Serial.print("LEFT held this hard: ");
      Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
    }
    if(ps2x.Button(PSB_PAD_DOWN)){
      Serial.print("DOWN held this hard: ");
      Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
    }   

    vibrate = ps2x.Analog(PSAB_CROSS);  //this will set the large motor vibrate speed based on how hard you press the blue (X) button
    if (ps2x.NewButtonState()) {        //will be TRUE if any button changes state (on to off, or off to on)
      if(ps2x.Button(PSB_L3))
        Serial.println("L3 pressed");
      if(ps2x.Button(PSB_R3))
        Serial.println("R3 pressed");
      if(ps2x.Button(PSB_L2))
        Serial.println("L2 pressed");
      if(ps2x.Button(PSB_R2))
        Serial.println("R2 pressed");
      if(ps2x.Button(PSB_TRIANGLE))
        Serial.println("Triangle pressed");        
    }

    if(ps2x.ButtonPressed(PSB_CIRCLE)) {              //will be TRUE if button was JUST pressed
      Serial.println("Circle just pressed");
      buttonstate=!buttonstate;      //按下圆圈键状态改变
      if (buttonstate) {             //如果状态为1,亮
        digitalWrite(led,HIGH);
       }
       else{                        //为0则不亮
         digitalWrite(led,LOW);
       }
    }
    if(ps2x.NewButtonState(PSB_CROSS))               //will be TRUE if button was JUST pressed OR released
      Serial.println("X just changed");
    if(ps2x.ButtonReleased(PSB_SQUARE))              //will be TRUE if button was JUST released
      Serial.println("Square just released");     

    if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) { //print stick values if either is TRUE
      Serial.print("Stick Values:");
      Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX  
      Serial.print(",");
      Serial.print(ps2x.Analog(PSS_LX), DEC); 
      Serial.print(",");
      Serial.print(ps2x.Analog(PSS_RY), DEC); 
      Serial.print(",");
      Serial.println(ps2x.Analog(PSS_RX), DEC); 
    }     
  }
  delay(50);  
}

注意:需要将PS2X_lib提前放置arduino安装目录下的library,可以直接试该库内教程文件PS2X_Example,将串口监视器中的波特率记得改为57600,否则按下按键反馈为一串乱码。PS2X_lib网上可以找得到,也可以通过我博客资源下载。
在这里插入图片描述

最终效果

这里录了一个简单的视频,初始时候LED灯不亮,按下圆键时候亮起,再次按即关闭。
几秒钟的视频附在链接里。-

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/136284.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 视频编解码算法面试总结

    视频编解码算法面试总结H.264与H.265的主要差异H.265仍然采用混合编解码,编解码结构域H.264基本一致, 主要的不同在于: 1.编码块划分结构:采用CU(CodingUnit)、PU(PredictionUnit)和TU(TransformUnit)的递归结构。 2.基本细节:各功能块的内部细节有很多差异 3.并行工具:增加了Tile以及WPP等并行工具集以提高编码速度

  • [黑苹果系列] M910x完美黑苹果系统安装教程 – 2 制作系统U盘-USB Creation

    [黑苹果系列] M910x完美黑苹果系统安装教程 – 2 制作系统U盘-USB Creation目前主流的苹果系统的安装方法有两种:1.U盘安装2.Windows下使用镜像恢复软件安装的方式目前,U盘安装是主流选择,这样安装调试好的黑苹果macOS问题最少,也较为稳定。之前的文章由于采用的是镜像恢复的方法,此次安装采取U盘安装系统的方式,因此重新写一篇。1.前期准备U盘一个,大于16G 安装工具软件包 镜像文件 制作合适的EFI文件 需要准备40GB以上独立固态硬盘空间2.下载macOs镜像3.制作安装U盘可以用TransMac或者balenaEt.

  • 用Java实现HTTP Multipart的服务端和客户端

    用Java实现HTTP Multipart的服务端和客户端

  • c++ stl map实现_JAVA map

    c++ stl map实现_JAVA mapC++STL库map详解

    2022年10月15日
  • spssχ2检验_spss交叉表分析方法与步骤 + SPSS卡方检验结果的阅读

    spssχ2检验_spss交叉表分析方法与步骤 + SPSS卡方检验结果的阅读spss中交叉分析主要用来检验两个变量之间是否存在关系,或者说是否独立,其零假设为两个变量之间没有关系。在实际工作中,经常用交叉表来分析比例是否相等。例如分析不同的性别对不同的报纸的选择有什么不同。spss交叉表分析方法与步骤:1、在spss中打开数据,然后依次打开:analyze–descriptive–crosstabs,打开交叉表对话框2、将性别放到行列表,将对读物的选择变量放到列,这样…

  • 业务测试用例模版与大数据测试用例模板

    业务测试用例模版与大数据测试用例模板前言:分享下业务测试用例模版与大数据测试用例模板业务测试用例模版与大数据测试用例模板一、业务测试用例模板二、业务测试用例模板一、业务测试用例模板需要下载模板请点击:业务测试用例模板下载二、业务测试用例模板需要下载模板请点击:大数据测试用例模板下载…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号