1

我正在使用以下代码来确定拇指操纵杆的位置:

const int Left = 1;
const int Right = 2;
const int Up = 3;
const int Down = 4;
const int Center = 0;
int lastButton = -1;

int xpin = 4;
int ypin = 5;

int xAxis;
int yAxis;
char* myStrings[]={"Left","Right","Up","Down"};
int button;

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

void loop() {
    xAxis=map(analogRead(xpin), 0, 1023, 0, 10);
    yAxis=map(analogRead(ypin), 0, 1023, 0, 10);

    if (button == lastButton) {
        //Serial.println(0);
        //button = 0;
        delay(50);
    } else {
        if (xAxis < 4 ) {
            button = Left;
        }
        else if (xAxis > 6 ) {
            button = Right;
        }

        if (yAxis < 4 ) {
            button = Down;
        }
        else if (yAxis > 6 ) {
            button = Up;
        }

        Serial.println(myStrings[button-1]);
        button = 0;
        delay(50);
    }

    lastButton = button;
}

上面的代码在我的 arduino 上工作得很好,但我希望只获得一次位置,而不是每秒钟都获得一次。

如何将代码更改为仅取一个值,直到再次居中(0)

例子:

如果我将操纵杆向左移动,它会显示:

Left
Left
Left
Left
etc etc until i release it.

我想做的是:

Left
then it stops until i release it.

任何帮助都会很棒!

更新

 if (xAxis ==5 ) { 
   button = Center; 
 }

 if (yAxis ==5 ) { 
   button = Center; 
 }

似乎与 Up 和 Down 配合得很好,但对 Left 和 Right 却不是。有时它会起作用,但更多时候却不起作用。

4

3 回答 3

2

记住上次迭代时的位置,并与之进行比较以查看它是否已更改:

int lastButton = 0;
...
void loop() {
    button = ...// Read button state
    ...
    if (button != lastButton) {
        ... // New keypress
        lastButton = button;
    }
}

完整的清理代码:

enum ButtonState { Neutral=0, Left=1, Right=2, Up=3, Down=4 };

ButtonState lastButton = Neutral;

const int XPin = 4;
const int YPin = 5;

char* myStrings[]={"Neutral", "Left","Right","Up","Down"};

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

ButtonState readButtonState() {
    int xAxis=map(analogRead(XPin), 0, 1023, 0, 10);
    int yAxis=map(analogRead(YPin), 0, 1023, 0, 10);
    if (xAxis < 4 ) return Left;
    if (xAxis > 6 ) return Right;
    if (yAxis < 4 ) return Down;
    if (yAxis > 6 ) return Up;
    return Neutral;
}

void loop() {
    ButtonState button = readButtonState();
    if (button != lastButton) {
        Serial.println(myStrings[button]);
        lastButton = button;
    }
    delay(50);
}
于 2012-07-11T21:23:30.040 回答
0

它应该更像这样:

const int Left = 1;
const int Right = 2;
const int Up = 3;
const int Down = 4;

int xpin = 4;
int ypin = 5;

int xAxis;
int yAxis;
char* myStrings[]={"Left","Right","Up","Down"};
int button;


void setup() {
  Serial.begin(9600);
}
int lastButton = -1; // init lastButton to a never occurring value

void loop() {

   xAxis=map(analogRead(xpin), 0, 1023, 0, 10);
   yAxis=map(analogRead(ypin), 0, 1023, 0, 10);

   if (xAxis < 4 ) { 
     button = Left; 
   }
   else if (xAxis > 6 ) { 
     button = Right; 
   }

   if (yAxis < 4 ) { 
     button = Down; 
   }
   else if (yAxis > 6 ) { 
     button = Up;
   }

   if (button == lastButton){
       //Serial.println(0);
       //button = 0;
       delay(50); // wait more
   }else{
       lastButton = button;
       Serial.println(myStrings[button-1]);
       button = 0;
       delay(50);
   }
 }
于 2012-07-11T21:57:17.073 回答
0

Check out this google code project, the author really did some great work here. I'm using this in a project of mine and it requires VERY little customization (if any at all).

This is a joystick widget that is already very solid and is easily customizable. I'm always hesitant to use other people's code, but it would have taken me quite a while to make anything as awesome as this.

Demo: http://code.google.com/p/mobile-anarchy-widgets/wiki/JoystickView

Source: http://code.google.com/p/mobile-anarchy-widgets/source/browse/trunk/Widgets/src/com/MobileAnarchy/Android/Widgets/Joystick/JoystickView.java

于 2012-07-13T14:10:01.073 回答