【问题标题】:Error: How to fix the digitalWrite() error? [closed]错误:如何修复 digitalWrite() 错误? [关闭]
【发布时间】:2019-10-02 06:28:40
【问题描述】:

我的这部分代码在执行时以红色突出显示。我是初学者,所以请帮我解决它。错误出现在代码 else 部分之后的行中。

尝试使用谷歌搜索。

const int ledPin=13;//ledpin,flamepin and buzpin are not changed throughout the process
const int flamepin=A2;
const int buzpin=11;
const int thresold=200;// sets threshold value for flame sensor
int flamesensvalue=0;//initialize flamesensor reading
void setup()
{
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
  pinMode(flamepin,INPUT);
  pinMode(buzpin,OUTPUT);
}
void loop()
{
  flamesensvalue=analogRead(flamepin); // reads analog data from flame sensor
  if (flamesensvalue<=thresold)
  { // compares reading from flame sensor with the threshold value
    digitalWrite(ledpin,HIGH);   //turns on led and buzzer
    tone(buzpin,100);
    delay(1000); //stops program for 1 second
  } else
  {
    digitalWrite(ledpin,LOW);//turns led off led and buzzer
    noTune(buzpin);
  }
}

【问题讨论】:

  • 请显示更多您的代码,尝试minimal reproducible example。你可以假设我们有相同的环境。
  • 查看构建日志,是否出现构建错误?什么错误?请花一些时间阅读有关 how to ask good questionsthis question checklist 的信息,并编辑您的问题以改进它。
  • @KenY-N setup 函数由 Arduino 框架自动调用。然后它在无限循环中调用loop
  • 请注意向我们展示minimal reproducible example 的建议。我们需要看看你包含了哪些头文件。
  • 对不起,特别是那些目光敏锐的回答者。我认为是时候将其作为错字关闭了。其实好几个错别字....

标签: c arduino embedded


【解决方案1】:

即使您的问题不完整:

const int ledPin=13;


digitalWrite(ledpin,LOW);//turns led off led and buzzer

“digitalWrite”错误可能来自这种差异。 C 和 C++ 区分大小写。 ledPinledpin 是不同的名称。

下次你应该显示真正的错误信息,因为编译器总是会准确地告诉你哪里出了问题。

【讨论】:

  • 敏锐的眼光,这对我来说已经足够了。虽然我相信如果这是答案,那么问题应该发生在 else 之前,而不是之后。
【解决方案2】:
noTune(buzpin);

应该是

noTone(buzpin);

没有名为noTune() Arduino 的函数。

【讨论】:

  • 还有敏锐的眼光。看来我需要新眼镜了。
  • @Yunnosch 哈哈!我们不需要敏锐的眼光,但对于这类问题,我们肯定需要编译器错误。 :D
  • 没错,基兰·比拉达尔。这就是为什么minimal reproducible example 一再被提及的原因。
猜你喜欢
  • 1970-01-01
  • 2014-12-11
  • 2019-04-07
  • 2012-03-24
  • 2020-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-08
相关资源
最近更新 更多