拇指杆位置只有一次

时间:2012-07-11 21:19:07

标签: c++ arduino

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

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上运行得很好,但是我希望只能获得位置 ONCE 而不是每一秒都在那里。

如何将代码更改为仅将一个值再次置于其中心(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 and Down但不适用于Left和Right。有时它会起作用,更常见的是它不起作用。

3 个答案:

答案 0 :(得分:1)

记住最后一次迭代的位置并与之比较以查看它是否已更改:

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);
}

答案 1 :(得分: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);
   }
 }

答案 2 :(得分:0)

查看这个谷歌代码项目,作者真的在这里做了一些很棒的工作。我在我的一个项目中使用它,它需要非常小的定制(如果有的话)。

这是一个操纵杆小部件,已经非常可靠,可以轻松定制。我总是对使用其他人的代码犹豫不决,但是我需要花很长时间才能制作出像这样令人敬畏的东西。

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

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