音()不工作(Tone() not working)

编程入门 行业动态 更新时间:2024-10-28 06:36:33
音()不工作(Tone() not working)

我对Arduino非常陌生(因为在这个周末我必须学习它来学校项目),并且我无法让我的压电扬声器发出闹钟的声音。 当草图运行时,一切正常。 LED闪烁,但蜂鸣器不响,这是奇怪的,因为LED和蜂鸣器命令在草图中彼此交错。 如果有人能帮我解决这个小问题,那将不胜感激!

我已经包含了空循环供参考,但已经设置:

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

I'm very new to Arduino (as in I had to learn it this weekend for a school project) and I'm having trouble getting my piezo speaker to make a sound for an alarm clock. When the sketch runs, everything works just fine. The LED blinks, but the buzzer doesn't sound, which is weird since the LED and buzzer commands are interspersed between each other in the sketch. If someone could help me out with this small issue, that would be greatly appreciated!

I've included just the void loop for reference but have already set:

int buzzerPin = 1;

And in the setup() function:

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上的引脚buzzerPin )。

Arduino PWM引脚图像

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

Try to connect buzzerPin to a PWM pin (ex: pin 3, 5, 9, 10, 11 on Arduino Uno).

Arduino PWM pins image

Because the tone() function is just support for PWM pins (in your case pin 1 is not a PWM pin).

更多推荐

本文发布于:2023-07-07 15:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1065187.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   Tone   working

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!