无线手柄+步进电机——控制方向

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

   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供电时会出现什么情况。

posted @ 2013-12-15 21:59  此去经年  阅读(1150)  评论(0)    收藏  举报