0
請幫助我。當我按下按鈕Arduino不會隨機化任何數字?我找不到解決方案,我希望你們能幫助我。 該項目是一個公平的我的學校每年舉辦。如果你們需要更多的信息,隨時提問!ARDUINO按鈕不會隨機化我的號碼
const int buttonPin = 1;
const int ledPin = 12;
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
int counter = 0;
int randNumber;
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
Serial.begin(1200);
}
void loop() {
if(counter==0 && buttonState==HIGH){
randNumber = random(1,7);
Serial.println(randNumber);
Serial.println("counter" + counter);
counter=2;
}
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}
非常感謝您的回答!它是9600,沒有計數器== 0等等。我改變他們只是因爲我需要測試它是如何工作的。但非常感謝!祝你今天愉快! –
很高興工作;) – frarugi87