Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Arduino Mega 2560中的摩尔斯电码,使用按钮和蜂鸣器_C++_C_Arduino - Fatal编程技术网

C++ Arduino Mega 2560中的摩尔斯电码,使用按钮和蜂鸣器

C++ Arduino Mega 2560中的摩尔斯电码,使用按钮和蜂鸣器,c++,c,arduino,C++,C,Arduino,我试图用Arduino制作一个简单的摩尔斯电码,使用一个试验板、一个蜂鸣器和两个按钮。按下按钮1时,蜂鸣器的输出应为200毫秒的声音信号。如果按下另一个按钮(按钮N2),蜂鸣器的输出应为400毫秒的声音信号 此外,按下按钮1时,程序应在屏幕上打印“.”。同样,对于较长的输出,请打印“-” 这是我的代码: const int buttonPin1 = 10; const int buttonPin2 = 8; const int buzzPin = 13; void setup() { /

我试图用Arduino制作一个简单的摩尔斯电码,使用一个试验板、一个蜂鸣器和两个按钮。按下按钮1时,蜂鸣器的输出应为200毫秒的声音信号。如果按下另一个按钮(按钮N2),蜂鸣器的输出应为400毫秒的声音信号

此外,按下按钮1时,程序应在屏幕上打印“.”。同样,对于较长的输出,请打印“-”

这是我的代码:

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

如果有人能引导我走上正确的道路,我将不胜感激


谢谢。

buttonPin1==true
buttonPin2==true
正在将
true
与pin号进行比较,而不是与pin的状态进行比较

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);
  }
}
您应该使用该功能检查引脚的状态

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);
  }
}

目前,它不起作用,--请比仅仅说“它不起作用”更详细一点。我不确定是我的代码还是电路错了。独立测试它们。也可以独立测试软件的各个部分——分小步构建。然后,当任何特定步骤不起作用时,它会限制您需要查找问题的位置。假设您构建了整个代码,并且有三个问题?这更难解决,因为解决一个问题不会有结果。@WeatherVane谢谢你的提示,我以后一定会这样做。