美文网首页
Arduino_JoyStick

Arduino_JoyStick

作者: 范女青 | 来源:发表于2018-09-21 08:42 被阅读0次

    The joystick module is an extra module so requires a connection to the Arduino mainboard with wire jumpers. Three pins (pin number 9, 68 and 69) are used and 5V power should be supplied. The joystick module can detect which position the joystick faces (CENTER, LEFT, RIGHT, UP and DOWN).

    Predefined Functions in Library
    int analogRead(pin);
    Set the address/mode (output) and initialize the joystick module.

    • parameter :
      pin : pin number of the module
    • return value : 0~1023 analogue value
    /* joystick.ino */
    #define JOY_X 68
    #define JOY_Y 69
    #define JOY_SEL 9
    
    int xCenterPos = 0; 
    int yCenterPos = 0; 
    int xyTH = 100;
    
    void setup() 
    { 
      pinMode(JOY_X, INPUT); 
      pinMode(JOY_Y, INPUT); 
      pinMode(JOY_SEL, INPUT);
      xCenterPos = analogRead(JOY_X); 
      yCenterPos = analogRead(JOY_Y);
      Serial.begin(115200);
    }
    
    void loop() 
    {
      if(analogRead(JOY_X) < xCenterPos - xyTH)  { Serial.print("LEFT");} 
      else if(analogRead(JOY_X) > xCenterPos + xyTH) { Serial.print("RIGHT");}   
      else { Serial.print("CENTER");}
      Serial.print(" ");
      if(analogRead(JOY_Y) < yCenterPos - xyTH) { Serial.println("UP");} 
      else if(analogRead(JOY_Y) > yCenterPos + xyTH) { Serial.println("DOWN");} 
      else { Serial.println("CENTER");}
    
      if(digitalRead(JOY_SEL) == LOW) { Serial.println("Button Pushed");}
      delay(500);
    }
    
    

    相关文章

      网友评论

          本文标题:Arduino_JoyStick

          本文链接:https://www.haomeiwen.com/subject/jgaonftx.html