Arduino Nano和模拟操纵杆

时间:2018-05-24 17:34:34

标签: arduino

我有一个由4个运动方向组成的游戏(左上和右下) 使用Arduino Nano和模拟操纵杆,看起来像其他帖子之前的代码是正确的。

这是Arduino代码:

byte x_axis = A3;
byte y_axis = A1;
byte btn1 = 8;
byte btn2 = 9;
byte btn3 = 10;
byte btn4 = 11;
byte btn5 = 12;
byte led = 13;

void setup(){
  pinMode(x_axis, INPUT);
  pinMode(y_axis, INPUT);

  pinMode(btn1, INPUT);
  pinMode(btn2, INPUT);
  pinMode(btn3, INPUT);
   pinMode(btn4, INPUT);
   pinMode(btn5, INPUT);

  digitalWrite(btn1, HIGH);
  digitalWrite(btn2, HIGH);
  digitalWrite(btn3, HIGH);
  digitalWrite(btn4, HIGH);
  digitalWrite(btn5, HIGH);

  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);

  Serial.begin(9600);
}
void loop(){

  Int read_x = analogRead(x_axis);
  int read_y = analogRead(y_axis);

  if(read_x > 600){
    Serial.println("R");
    digitalWrite(led, HIGH);
  }
  if(read_x < 400){
    Serial.println("L");
    digitalWrite(led, HIGH);
  }
  if(read_y > 600){
    Serial.println("D");
    digitalWrite(led, HIGH);
  }
  if(read_y < 400){
    Serial.println("U");
   digitalWrite(led, HIGH);
  }

  if(digitalRead(btn1) == LOW){
    Serial.println("1");
    digitalWrite(led, HIGH);
  }
  if(digitalRead(btn2) == LOW){
    Serial.println("2");
    digitalWrite(led, HIGH);
   }
  if(digitalRead(btn3) == LOW){
    Serial.println("3");
    digitalWrite(led, HIGH);
   }
  if(digitalRead(btn4) == LOW){
    Serial.println("4");
    digitalWrite(led, HIGH);
  }
   if(digitalRead(btn5) == LOW){
    Serial.println("5");
    digitalWrite(led, HIGH);
   }

   delay(10);
   digitalWrite(led, LOW); 
}

但是当我使用串行监视器进行检查时,即使不触摸操纵杆,它也会不停地显示UL

如何解决此问题?

2 个答案:

答案 0 :(得分:0)

我感觉很慷慨,所以你在代码中有一些抖动问题,你真的可以使用一些清理。这段代码编译。好吧,现在当操纵杆在x / y的物理位置为0,0时,它会在代码中抖动。您可以做的一件事是重新映射抖动以提供更宽的中心。将原始模拟值打印到串行监视器,然后使用一点填充将它们映射到0点。参考:https://www.arduino.cc/reference/en/language/functions/math/map/

byte x_axis = A3;
byte y_axis = A1;
byte btn[] = {8, 9, 10, 11, 12}; // 2,3,4,5,6
byte stat[] = {1, 2, 3, 4, 5};
byte led = 13;
int dval = 50;

void setup() {
  pinMode(x_axis, INPUT);
  pinMode(y_axis, INPUT);

  for (int i = 0; i < 5; i++) {
    pinMode(btn[i], INPUT);
    digitalWrite(btn[i], HIGH);
  }
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
  Serial.begin(9600);
}

void loop() {

  int read_x = analogRead(x_axis);
  delay(dval);
  int read_y = analogRead(y_axis);
  delay(dval);

  if (read_x > 600) {
    Serial.println("R");
    digitalWrite(led, HIGH);
  }
  if (read_x < 400) {
    Serial.println("L");
    digitalWrite(led, HIGH);
  }
  if (read_y > 600) {
    Serial.println("D");
    digitalWrite(led, HIGH);
  }
  if (read_y < 400) {
    Serial.println("U");
    digitalWrite(led, HIGH);
  }

  for (int i = 0; i < 5; i++) {
    if (digitalRead(btn[i]) == LOW) {
      Serial.println(stat[i]);
      digitalWrite(led, HIGH);
    }
  }
  delay(10);
  digitalWrite(led, LOW);
}

答案 1 :(得分:-1)

Emad joon: 1-检查操纵杆的地面和Vdd是否与您的arduino连接。

2-将操纵杆的x和y连接到arduino模拟输入。

将此代码用作启动器:

#define X_AXIS A1
#define Y_AXIS A3


void setup() {
 Serial.begin(9600);

}

void loop() {
  Serial.print("X = ");
  Serial.println(analogRead(X_AXIS);
  Serial.print("Y = ");
  Serial.println(analogRead(Y_AXIS);
  delay(150);

}

查看Joy Stick处于初始位置时的值。您可以在此代码上构建按钮。 此外,不要忘记使用去抖动按钮读数:

if(digitalRead(Button1)==0){
  delay(40);
  if(digitalRead(Button1==0){
    buttonPressed=true;
  }
}