【问题标题】:Morse code in Arduino Mega 2560, using buttons and a buzzerArduino Mega 2560 中的摩尔斯电码,使用按钮和蜂鸣器
【发布时间】: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 感谢您的提示,我以后一定会尝试这样做。

标签: c++ c arduino


【解决方案1】:

buttonPin1 == truebuttonPin2 == truetrue 与引脚号进行比较,而不是引脚的状态。

您应该使用digitalRead() 函数来检查引脚的状态。

void loop() {
  // put your main code here, to run repeatedly:
  noTone(buzzPin);

  if (digitalRead(buttonPin1) == HIGH) { // check pin status instead of doing constant comparision
    Serial.println('.');
    tone(buzzPin, 1000);
    delay(200);
  }
  else if (digitalRead(buttonPin2) == HIGH) { // check pin status instead of doing constant comparision
    Serial.println('-');
    tone(buzzPin, 1000);
    delay(400);
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    相关资源
    最近更新 更多