【发布时间】:2018-04-26 16:43:59
【问题描述】:
我最近使用 Rex Qualis Arduino Uno R3 进入了 Arduino,我正在尝试构建一个能够击败 Simon 记忆游戏(或重复节拍)的项目。
它通过四个按钮之一等待用户响应,然后将其添加到列表中,执行列表,然后在下一步等待用户输入。
一切都按预期进行,但最奇怪的事情发生在执行时:
- 在完全执行后的第一个循环中,Servo 1 将在未经授权的情况下执行其移动功能。
- 在完全执行后的第二个循环中,Servo 2 将执行其移动功能,依此类推。
- 在第四个循环、执行和伺服 4 执行其移动功能后,它不再发生。我不知道为什么它在前四个循环中一个接一个地循环通过所有伺服系统,然后就可以了,但这有点破坏了我的项目。
我的代码中是否存在重定向到移动功能或其他问题的问题?感谢所有帮助。以下是参考代码:
//Simon killer
//Da Cube
#include <Servo.h>
//Declare buttons
int button1 = 4;
int button2 = 5;
int button3 = 6;
int button4 = 7;
//Declare servos
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
int moves[100]; //Memory up to 100
int x = 0;
int y = 1;
void setup() {
pinMode(button1, INPUT_PULLUP); //Button setup
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
servo1.attach(8); //Servo setup
servo2.attach(9);
servo3.attach(10);
servo4.attach(11);
moveServo1();//System check
moveServo2();
moveServo3();
moveServo4();
}
//move functions
void moveServo1() {
servo1.write(5);
delay(500);
servo1.write(45);
delay(500);
}
void moveServo2() {
servo2.write(5);
delay(500);
servo2.write(45);
delay(500);
}
void moveServo3() {
servo3.write(175);
delay(500);
servo3.write(135);
delay(500);
}
void moveServo4() {
servo4.write(5);
delay(500);
servo4.write(45);
delay(500);
}
void loop() {
//Read Input by button
while (x < y) {
if (digitalRead(button1) == LOW) {
moves[x] = 1;
x++;
} else if (digitalRead(button2) == LOW) {
moves[x] = 2;
x++;
} else if (digitalRead(button3) == LOW) {
moves[x] = 3;
x++;
} else if (digitalRead(button4) == LOW) {
moves[x] = 4;
x++;
}
}
y++;
//Decode Memory Array
for (int i = 0; i < (sizeof(moves)); i++) {
switch (moves[i]) {
case 1:
moveServo1();
break;
case 2:
moveServo2();
break;
case 3:
moveServo3();
break;
case 4:
moveServo4();
break;
}
}
}
【问题讨论】:
标签: memory arduino arduino-uno servo