我在Arduino上编写C语言,而且我是一名初学者。我在这个程序中的目标是在LED上显示一个二进制数,然后让玩家猜出二进制数。然后播放器按下等于显示二进制数的十进制值的按钮。当玩家开始按下按钮时,每次按下都会有两秒钟。如果他们在这两秒钟内没有再次点击,他们就输了,或者他们猜对了,赢了。
我凌乱的程序唯一不做的就是按下每个按钮两秒钟。即使经过大量阅读,我也对milli();感到困惑。除了糟糕的编码之外,我可以在程序中更改某些内容,以便在按下按钮时为玩家提供两秒钟的窗口吗?我确信我会忽略一个细节。谢谢你的时间。
const int buttonPin = 2;
const int ledPinValue1 = 3;
const int ledPinValue2 = 4;
const int ledPinValue4 = 5;
const int ledPinValue8 = 6;
int teller = 0;
int buttonStatus = 0;
int lastButtonStatus;
unsigned long interval=5000; // the time we need to wait
unsigned long previousMillis = 0; // millis() returns an unsigned long.
unsigned long currentMillis = 0;
unsigned long elapsed;
unsigned long midElapsed;
unsigned long minus=5000;
int secondLoop = 0;
boolean allowTimer = false;
boolean allowMilliAfterFirstRound; //without this, when the second loop commences milli = milli - milli
void setup() {
Serial.begin(9600);
pinMode(ledPinValue1, OUTPUT);
pinMode(ledPinValue2, OUTPUT);
pinMode(ledPinValue4, OUTPUT);
pinMode(ledPinValue8, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
boolean initiateLoss = true; // ++++
int randomNumber = 5; //Replace with randomcode
currentMillis = millis();
knopStatus = digitalRead(buttonPin);
if (randomNumber == 1 || randomNumber == 3 || randomNumber == 5 || randomNumber == 7 || randomNumber == 9 || randomNumber == 11 || randomNumber == 13 || randomNumber == 15){
digitalWrite(ledPinValue1, HIGH);
}
else {
digitalWrite(ledPinValue1, LOW);
}
if (randomNumber == 2 || randomNumber == 3 || randomNumber == 6 || randomNumber == 7 || randomNumber == 10 || randomNumber == 11 || randomNumber == 14 || randomNumber == 15){
digitalWrite(ledPinValue2, HIGH);
}
else {
digitalWrite(ledPinValue2, LOW);
}
if (randomNumber == 4 || randomNumber == 5 || randomNumber == 6 || randomNumber == 7 || randomNumber == 12 || randomNumber == 13 || randomNumber == 14 || randomNumber == 15){
digitalWrite(ledPinValue4, HIGH);
}
else {
digitalWrite(ledPinValue4, LOW);
}
if (randomNumber == 8 || randomNumber == 9 || randomNumber == 10 || randomNumber == 11 || randomNumber == 12 || randomNumber == 13 || randomNumber == 14 || randomNumber == 15){
digitalWrite(ledPinValue8, HIGH);
}
else {
digitalWrite(ledPinValue8, LOW);
}
//------------------------------------------------------------------------------------
if (knopStatus != lastButtonStatus) {
if (knopStatus == HIGH) {
teller++;
Serial.print("number of button pushes: ");
Serial.println(teller, DEC);
allowTimer = true;
}
delay(50);
}
//------------------
if (allowTimer == true){
if (allowMilliAfterFirstRound == true) {
currentMillis = currentMillis - currentMillis;
allowMilliAfterFirstRound = false;
}
if (teller == randomNumber){ // WIN
Serial.print("You won!");
Serial.println();
initiateLoss = false;
randomNumber = 3; // remove when randomnumber is added
teller = 0;
previousMillis = millis();
}
if ((unsigned long)(currentMillis - previousMillis) > interval && initiateLoss == true) { // LOST!
elapsed = currentMillis - previousMillis;
Serial.print("Time elapsed at time of failure: ");
Serial.print(elapsed);
Serial.println();
Serial.print("currentMillis is now: ");
Serial.print(currentMillis); // 5000, 10000, 15000, 20000 etc
Serial.println();
Serial.print("previousMillis is now: ");
Serial.print(previousMillis); // 5000, 10000, 15000, 20000 etc
Serial.println();
previousMillis = millis();
Serial.println();
Serial.print("You lost!");
Serial.println();
Serial.print("Begin!");
Serial.println();
teller = 0;
}
}
lastButtonStatus = knopStatus; // save the current state as the last state, for next time through the loop
}
答案 0 :(得分:0)
我会做这样的事情:
void loop() {
if (not yet displayed) {
[all the code to display the number on leds]
}
if (button pressed) {
allowTimer = true; // Start!
}
if (allowTimer) /* If timer started */ {
while (millis() - previousMillis <= 2000) /* The famous 2 sec */ {
if (button pressed) {
previousMillis = millis(); /* Update timing */
teller++;
}
}
if(teller == randomNumber) {
[Hooray, you won or whatever message and stuff you like]
}
else {
[Sorry, you lost]
}
}
当然你应该用你的代码替换我的作品。
提示:对于任何语句中的单个指令,您不需要括号:
if (condition) {
onlyOneInstruction;
}
与
相同if (condition) onlyOneInstruction;
编辑:此外,您尝试将knopStatus
(未声明,也许您的意思是buttonStatus
)与lastButtonStatus
进行比较,但未初始化。这会给你一个错误。
答案 1 :(得分:0)
有错误,例如:
if (allowMilliAfterFirstRound == true) {
currentMillis = currentMillis - currentMillis;
allowMilliAfterFirstRound = false;
}
并且allowMilliAfterFirstRound
未初始化,也未分配到其他地方......
更一般地说,代码显示您正确理解millis
。缺少的是对该计划所处的2个州的管理:
previousMillis
。