今天测试了一下无线手柄控制电机转向的改变

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

   2: #include <Stepper.h>

   3:  

   4: #define PS2_DAT        13  //14    

   5: #define PS2_CMD        11  //15

   6: #define PS2_SEL        10  //16

   7: #define PS2_CLK        12  //17

   8:  

   9: #define pressures   true

  10: //#define pressures   false

  11: #define rumble      true

  12: //#define rumble      false

  13:  

  14: PS2X ps2x; // create PS2 Controller Class

  15:  

  16:  

  17: int error = 0;

  18: byte type = 0;

  19: byte vibrate = 0;

  20: int stepsPerRevolution = 200;

  21:  

  22: Stepper myStepper(stepsPerRevolution,9,7,8,6);

  23:  

  24: void setup(){

  25:  

  26:   myStepper.setSpeed(60);

  27:   Serial.begin(57600);

  28:   

  29:   delay(300);  //added delay to give wireless ps2 module some time to startup, before configuring it

  30:  

  31:   error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);

  32:   

  33:   if(error == 0){

  34:     Serial.print("Found Controller, configured successful ");

  35:     Serial.print("pressures = ");

  36:     if (pressures)

  37:       Serial.println("true ");

  38:     else

  39:       Serial.println("false");

  40:     Serial.print("rumble = ");

  41:     if (rumble)

  42:       Serial.println("true)");

  43:     else

  44:       Serial.println("false");

  45:   }  

  46:   else if(error == 1)

  47:     Serial.println("No controller found, check wiring, see readme.txt to enable debug. ");

  48:    

  49:   else if(error == 2)

  50:     Serial.println("Controller found but not accepting commands.");

  51:  

  52:   else if(error == 3)

  53:     Serial.println("Controller refusing to enter Pressures mode, may not support it. ");

  54:   

  55: }

  56:  

  57: void loop() {

  58:   if(error == 1) //skip loop if no controller found

  59:     return; 

  60:     

  61:   ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed

  62:     

  63:     if(ps2x.Button(PSB_START))         //will be TRUE as long as button is pressed

  64:       Serial.println("Start is being held");

  65:     if(ps2x.Button(PSB_SELECT))

  66:       Serial.println("Select is being held");      

  67:  

  68:     if(ps2x.Button(PSB_PAD_UP)) {      //will be TRUE as long as button is pressed

  69:       Serial.print("Up held this hard: ");

  70:       Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);

  71:     }

  72:     if(ps2x.Button(PSB_PAD_RIGHT)){

  73:       stepsPerRevolution = -200;

  74:       Serial.println("clockwise");

  75:     }

  76:     if(ps2x.Button(PSB_PAD_LEFT)){

  77:       stepsPerRevolution = 200;

  78:       Serial.println("counterclockwise"); 

  79:     }

  80:     if(ps2x.Button(PSB_PAD_DOWN)){

  81:       Serial.print("DOWN held this hard: ");

  82:       Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);

  83:     }   

  84:  

  85:     vibrate = ps2x.Analog(PSAB_CROSS);  //this will set the large motor vibrate speed based on how hard you press the blue (X) button

  86:     if (ps2x.NewButtonState()) {        //will be TRUE if any button changes state (on to off, or off to on)

  87:       if(ps2x.Button(PSB_L3))

  88:         Serial.println("L3 pressed");

  89:       if(ps2x.Button(PSB_R3))

  90:         Serial.println("R3 pressed");

  91:       if(ps2x.Button(PSB_L2))

  92:         Serial.println("L2 pressed");

  93:       if(ps2x.Button(PSB_R2))

  94:         Serial.println("R2 pressed");

  95:       if(ps2x.Button(PSB_TRIANGLE))

  96:         Serial.println("Triangle pressed");        

  97:     }

  98:  

  99:     if(ps2x.NewButtonState(PSB_CIRCLE))               //will be TRUE if button was JUST pressed

 100:       Serial.println("Circle just pressed");

 101:     if(ps2x.NewButtonState(PSB_CROSS))               //will be TRUE if button was JUST pressed OR released

 102:       Serial.println("X just changed");

 103:     if(ps2x.ButtonReleased(PSB_SQUARE))              //will be TRUE if button was JUST released

 104:       Serial.println("Square just released");     

 105:  

 106:     if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) { //print stick values if either is TRUE

 107:       Serial.print("Stick Values:");

 108:       Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX  

 109:       Serial.print(",");

 110:       Serial.print(ps2x.Analog(PSS_LX), DEC); 

 111:       Serial.print(",");

 112:       Serial.print(ps2x.Analog(PSS_RY), DEC); 

 113:       Serial.print(",");

 114:       Serial.println(ps2x.Analog(PSS_RX), DEC); 

 115:     } 

 116:     

 117:   myStepper.step(stepsPerRevolution);  

 118: }

 

当没有加入电机的代码的时候手柄的反应很快,能马上识别按钮,但是当我加入

   1: myStepper.step(stepsPerRevolution); 

以后,必须长按按钮才能改变方向,这个要好好研究一下

 

另外,当我打开串口监视器时电机才转,搞不清为什么,不知道如果用电池给Arduino供电时会出现什么情况。

无线手柄+步进电机——控制方向的更多相关文章

  1. 玩转X-CTR100 l STM32F4 l DRV8825 A4988 步进电机控制

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      本文介绍X-CTR100控制器控制步进电机 ...

  2. 玩转X-CTR100 l STM32F4 l PS2无线手柄-4WD智能小车

    我造轮子,你造车,创客一起造起来!更多塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 前面已介绍X-CTR100控制器解码PS2无线手 ...

  3. 玩转X-CTR100 l STM32F4 l PS2无线手柄

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] SONY的PS2无线手柄是索尼游戏机的遥控手柄,控制 ...

  4. SteamVR手柄震动控制实现

    SteamVR手柄震动控制实现 public class handCtrl : MonoBehaviour { public SteamVR_TrackedObject _TrackedObject; ...

  5. 基于MSP430G2系列实现的步进电机控制

    基于MSP430G2系列实现的步进电机控制 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 系列博客说明:此系列博客属于作者在大三大四阶段所储备的关于电子电路 ...

  6. STM32定时器输出PWM频率和步进电机控制速度计算

    1.STM32F4系列定时器输出PWM频率计算 第一步,了解定时器的时钟多少: 我们知道AHP总线是168Mhz的频率,而APB1和APB2都是挂在AHP总线上的. (1)高级定时器timer1, t ...

  7. 基于视觉反馈的步进电机X-Y平台控制

    关键词:步进电机.XY平台.视觉反馈 用途:工业自动化 文章类型:原理介绍.随笔纪念 @Author:VShawn(singlex@foxmail.com) @Date:2017-05-01 @Lab ...

  8. UI创意求助:手机贪吃蛇游戏方向控制键设计

    继上一片博文<做梦想起来的C#简单实现贪吃蛇程序(LinQ + Entity)>之后,尝试做一个手机版本,大部分的功能都已经实现了.但在贪吃蛇的方向控制设计上一直不太满意.由于手机界面的大 ...

  9. 关于Arduino 步进电机Stepper库的一些想法

    官方提供了一些库,使Arduino入门起来更加快速,我们连原理都不用懂,就能通过函数控制终端.但是,这样也带来了很多的缺陷,比如,库函数的功能有限,有些无法实现.然后还有库函数因为要考虑其他的情况,你 ...

随机推荐

  1. 对于Netty的十一个疑问

    [说明]本文原载于码农 IO(manong.io)官方微信 developerWorks,转载.引用请注明出处及作者. 1.Netty 是什么? Netty 是一个基于 JAVA NIO 类库的异步通 ...

  2. 转 delphi SelText,GetText,SetText用法

    转自:http://blog.163.com/wll_009/blog/static/1173731172009102452632968/ 这几个都跟选区有关的,就是选中一串字符串,选中的会变蓝色Se ...

  3. 关于如何用Jquery监听鼠标滚轮改变横向滚动条

    $(function(){ if ((navigator.userAgent.indexOf('MSIE') >= 0)){/*判断是否是IE浏览器*/ var scroll_width = 1 ...

  4. NSBundle、UIImageView和UIButton对比、Xcode文档安装路径、Xcode模拟器安装路径

    1.NSBundle1> 一个NSBundle代表一个文件夹,利用NSBundle能访问对应的文件夹2> 利用mainBundle就可以访问软件资源包中的任何资源3> 模拟器应用程序 ...

  5. 【strtok()】——分割字符串

    对字符串进行分割: 在使用前需要先初始化例如: char * p=strtok(Str," ");/*初始化以" "(以空格字符来分割字符串),即把" ...

  6. matlab里的nargin

    nargin是用来判断输入变量个数的函数,这样就可以针对不同的情况执行不同的功能.

  7. linker command failed with exit code 1 (use -v to see

    转自 :http://www.reader8.cn/jiaocheng/20131022/2003334.html Undefined symbols for architecture http:// ...

  8. subnetting and the subnet mask

    原文:https://www.techopedia.com/6/28587/internet/8-steps-to-understanding-ip-subnetting/5 Step 4 - Sub ...

  9. 利用socket模块检查端口存活并邮件警报

    可以用来监控一些端口. import socket def check_server(addr, port): s = socket.socket() try: s.connect((addr, po ...

  10. hdu_5776_sum(前缀和维护)

    题目链接:hdu_5776_sum 题意: 给你一串数,问你是否有一个连续的子序列的和为m的倍数 题解: 维护一个前缀和%m的值,如果前缀和%m的值为0或者有两个前缀和%m的值相同,那么就有一个连续区 ...