【问题标题】:Tone() not working音调()不工作
【发布时间】:2018-04-17 16:37:17
【问题描述】:

我对 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

【问题讨论】:

  • 请仅标记为相关语言,C 或 C++

标签: c++ arduino arduino-uno


【解决方案1】:

尝试将buzzerPin 连接到 PWM 引脚(例如:Arduino Uno 上的引脚 3、5、9、10、11)。

因为tone() 函数仅支持 PWM 引脚(在您的情况下,引脚 1 不是 PWM 引脚)。

【讨论】:

  • 问题是我有一个 RGB LCD 连接到 arduino,所以所有其他引脚都用完了。当我在 LCD 的其他草图中使用相同的引脚(引脚 1)时,它可以工作:/
  • 但正如我在arduino.cc/reference/en/language/functions/advanced-io/tone 看到的那样,函数tone() 只支持PWM 引脚。
  • 先说一句:你可以用tone(pin, frequency, duration);代替tone(pin, frequency); delay(buzzerInterval); noTone(buzzerPin);。其次,引脚 1 是 Serial 的 TX。不要使用它。
  • 感谢 dda 和 Nguyen Thanh Binh 对 Pin 1 的见解和清理我的代码!我将引脚更改为 A0,现在可以正常工作了
  • :) 恭喜
猜你喜欢
  • 1970-01-01
  • 2013-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-07
  • 2015-10-28
  • 1970-01-01
相关资源
最近更新 更多