我正在使用以下代码来确定拇指操纵杆的位置:
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 却不是。有时它会起作用,但更多时候却不起作用。