【发布时间】:2020-10-26 18:46:50
【问题描述】:
我正在尝试使用面包板、一个蜂鸣器和两个按钮在 Arduino 中制作一个简单的莫尔斯电码。按下按钮 1 时,蜂鸣器的输出应为 200ms 的声音信号。如果按下另一个按钮(button2),蜂鸣器的输出应该是400ms的声音信号。
此外,当按下 button1 时,程序应打印“.”。到屏幕。同样,打印“-”以获得更长的输出。
这是我的代码:
const int buttonPin1 = 10;
const int buttonPin2 = 8;
const int buzzPin = 13;
void setup() {
// put your setup code here, to run once:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buzzPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
noTone(buzzPin);
if (buttonPin1 == true) {
Serial.println('.');
tone(buzzPin, 1000);
delay(200);
}
else if (buttonPin2 == true) {
Serial.println('-');
tone(buzzPin, 1000);
delay(400);
}
}
目前,它不起作用,我不确定是我的代码还是电路有什么问题。我没有收到任何来自蜂鸣器或 Arduino 的输出。
如果有人能引导我走上正确的轨道,我将不胜感激。
谢谢。
【问题讨论】:
-
目前不工作, -- 请比只说“不工作”更详细一点。
-
我不确定是我的代码还是电路有什么问题。 独立测试它们。也独立测试软件的各个部分 - 以小步骤构建它。然后,当任何特定步骤不起作用时,它会限制您需要查找问题的位置。假设您构建了整个代码并且存在三个问题?这更难解决,因为解决一个问题不会显示结果。
-
@WeatherVane 感谢您的提示,我以后一定会尝试这样做。