我对Arduino非常陌生(因为这个周末我必须在一个学校项目中学习它),并且我很难让压电扬声器发出闹钟的声音。运行草图时,一切正常。 LED闪烁,但蜂鸣器不发声,这很奇怪,因为在草图中LED和蜂鸣器的命令相互交错。如果有人可以帮助我解决这个小问题,将不胜感激!
我仅包含void循环以供参考,但已经设置了:
int buzzerPin = 1;
并在
setup()
函数中:pinMode(buzzerPin, OUTPUT);
void loop() {
//MAKE LCD BLUE
setBacklight(0, 0, 255);
digitalClockDisplay(); // time displays on LCD
for (int k = 0; k < count; k++) {
if (hour() == h[k] && minute() == m[k] && second() == 00) {
Serial.println(amount[k]);
Serial.print(" ");
Serial.print(med[k]);
setBacklight(0, 0, 255);
lcd.setCursor(0, 0);
lcd.print(amount[k]);
lcd.print(" ");
lcd.print(med[k]);
lcd.print(" ");
for (int m = 0; m < 1000; m++) {
setBacklight(0, 0, 255);
lcd.setCursor(0, 1);
lcd.print(hour()); //prints real time
printDigits(minute());
printDigits(second());
digitalWrite (led, HIGH);
tone(buzzerPin, buzzerFrequency);
delay(buzzerInterval);
noTone(buzzerPin);
delay(buzzerInterval);
tone(buzzerPin, buzzerFrequency);
delay(buzzerInterval);
noTone(buzzerPin);
digitalWrite (led, LOW);
delay(buzzerInterval);
// Snooze and Stop
if (digitalRead(stopButton) == HIGH) {
digitalWrite(led, LOW); // turn the LED off by making the
voltage LOW
Serial.print("Alarm Stopped");
noTone(buzzerPin);
setBacklight(0, 255, 0); // set background to green
delay(5000); // delay for 5 seconds
break;
}
if (digitalRead(snoozeButton) == HIGH) {
digitalWrite(led, LOW);
Serial.print("Snooze for 5 seconds");
noTone(buzzerPin);
setBacklight(255, 0, 0); // set background to red
delay(snoozeTime);
}
}
} //if hour and min match
} // k loop
} // void loop
最佳答案
尝试将buzzerPin
连接到PWM引脚(例如:Arduino Uno上的引脚3、5、9、10、11)。
因为tone()
功能仅支持PWM引脚(在您的情况下,引脚1不是PWM引脚)。
关于c++ - Tone()不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49883319/