نمایش نتایج: از 1 به 2 از 2

موضوع: شیلد جوی استیک آردوینو (دو محوره)

  1. #1
    مدیر گروه
    تاریخ عضویت
    Nov 2013
    محل سکونت
    ایران
    نوشته ها
    4,064

    شیلد جوی استیک آردوینو (دو محوره)

    after opening the serial monitor, you should see different values printed on a line and refreshed very quickly. these are the state of the various buttons present on the joystick shield, as well as the
    .current position (x and y value) of the joystick

    کد:
    // Store the Arduino pin associated with each input
    
    
    // Select button is triggered when joystick is pressed
    const byte PIN_BUTTON_SELECT = 8; 
    
    
    const byte PIN_BUTTON_F = 7;
    const byte PIN_BUTTON_E = 6;
    
    
    const byte PIN_BUTTON_RIGHT = 3;
    const byte PIN_BUTTON_UP = 2;
    const byte PIN_BUTTON_DOWN = 4;
    const byte PIN_BUTTON_LEFT = 5;
    
    
    const byte PIN_ANALOG_X = 0;
    const byte PIN_ANALOG_Y = 1;
    
    
    void setup() {
      Serial.begin(9600);
    
    
      pinMode(PIN_BUTTON_RIGHT, INPUT);
      digitalWrite(PIN_BUTTON_RIGHT, HIGH);
    
    
      pinMode(PIN_BUTTON_LEFT, INPUT);
      digitalWrite(PIN_BUTTON_LEFT, HIGH);
    
    
      pinMode(PIN_BUTTON_UP, INPUT);
      digitalWrite(PIN_BUTTON_UP, HIGH);
    
    
      pinMode(PIN_BUTTON_DOWN, INPUT);
      digitalWrite(PIN_BUTTON_DOWN, HIGH);
    
    
      pinMode(PIN_BUTTON_SELECT, INPUT);
      digitalWrite(PIN_BUTTON_SELECT, HIGH);
      
      pinMode(PIN_BUTTON_E, INPUT);
      digitalWrite(PIN_BUTTON_E, HIGH);
      
      pinMode(PIN_BUTTON_F, INPUT);
      digitalWrite(PIN_BUTTON_F, HIGH);
    }
    
    
    void loop() {
      
      Serial.print("U:");
      Serial.print(digitalRead(PIN_BUTTON_UP));
      Serial.print(" ");
    
    
      Serial.print("R:");
      Serial.print(digitalRead(PIN_BUTTON_RIGHT));
      Serial.print(" ");
      
      Serial.print("D:");
      Serial.print(digitalRead(PIN_BUTTON_DOWN));
      Serial.print(" ");
      
      Serial.print("L:");
      Serial.print(digitalRead(PIN_BUTTON_LEFT));
      Serial.print(" ");
    
    
    
    
      Serial.print("x:");
      Serial.print(analogRead(PIN_ANALOG_X));
      Serial.print(" ");
    
    
      Serial.print("y:");
      Serial.print(analogRead(PIN_ANALOG_Y));
      Serial.print(" ");
    
    
    
    
      Serial.print("S:");
      Serial.print(digitalRead(PIN_BUTTON_SELECT));
      Serial.print(" ");
      
      
      Serial.print("F:");
      Serial.print(digitalRead(PIN_BUTTON_F));
      Serial.print(" ");
      
      Serial.print("E:");
      Serial.print(digitalRead(PIN_BUTTON_E));
      Serial.print(" ");
    
    
    
    
      Serial.println();
    }

  2. #2
    مدیر گروه
    تاریخ عضویت
    Nov 2013
    محل سکونت
    ایران
    نوشته ها
    4,064

    This code shows the direction when you push the buttons
    کد:
    const byte PIN_ANALOG_X = 0;
    const byte PIN_ANALOG_Y = 1;
    
    
    const int X_THRESHOLD_LOW = 300;
    const int X_THRESHOLD_HIGH = 380;    
    
    
    const int Y_THRESHOLD_LOW = 300;
    const int Y_THRESHOLD_HIGH = 380;    
    
    
    int x_position;
    int y_position;
    
    
    int x_direction;
    int y_direction;
    
    
    void setup() {
      Serial.begin(9600);
    }
    
    
    void loop () {
      x_direction = 0;
      y_direction = 0;
    
    
      x_position = analogRead(PIN_ANALOG_X);
      y_position = analogRead(PIN_ANALOG_Y);
    
    
      if (x_position > X_THRESHOLD_HIGH) {
        x_direction = 1;
      } else if (x_position < X_THRESHOLD_LOW) {
        x_direction = -1;
      }
    
    
      if (y_position > Y_THRESHOLD_HIGH) {
        y_direction = 1;
      } else if (y_position < Y_THRESHOLD_LOW) {
        y_direction = -1;
      }
    
    
      if (x_direction == -1) {
          if (y_direction == -1) {
            Serial.println("left-down");
          } else if (y_direction == 0) {
            Serial.println("left");
          } else {
            // y_direction == 1
            Serial.println("left-up");
          }
      } else if (x_direction == 0) {
          if (y_direction == -1) {
            Serial.println("down");
          } else if (y_direction == 0) {
            Serial.println("centered");
          } else {
            // y_direction == 1
            Serial.println("up");
          }
      } else {
          // x_direction == 1
          if (y_direction == -1) {
            Serial.println("right-down");
          } else if (y_direction == 0) {
            Serial.println("right");
          } else {
            // y_direction == 1
            Serial.println("right-up");
          }
      }
    }



مجوز های ارسال و ویرایش

  • شما نمیتوانید موضوع جدیدی ارسال کنید
  • شما امکان ارسال پاسخ را ندارید
  • شما نمیتوانید فایل پیوست کنید.
  • شما نمیتوانید پست های خود را ویرایش کنید
  •  

SEO by vBSEO