首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >敲门项目的Arduino蜂鸣器不工作

敲门项目的Arduino蜂鸣器不工作
EN

Stack Overflow用户
提问于 2018-05-11 01:04:44
回答 2查看 139关注 0票数 0

我正在尝试使用Arduino和压电蜂鸣器生成一个敲击检测器。此项目中使用的那个与此图片中使用的相同

使用以下代码

代码语言:javascript
复制
const int outputPin = 8;    // led indicator connected to digital pin
const int knockSensor = A0; // the piezo is connected to an analog pin
const int thresholdHIGH =150;  // threshold value to decide when the detected knock is hard (HIGH)
const int thresholdLOW = 120;  // threshold value to decide when the detected knock is gentle (LOW)


const int secretKnockLength = 4; //How many knocks are in your secret knock

/* This is the secret knock sequence
 * 0 represents a LOW or quiet knock
 * 1 represents a HIGH or loud knock
 * The sequence can be as long as you like, but longer codes increase the difficulty of matching */
const int secretKnock[secretKnockLength] = {0, 0, 1, 0};

int secretCounter = 0; //this tracks the correct knocks and allows you to move through the sequence
int sensorReading = 0; // variable to store the value read from the sensor pin

void setup() {

  //Set the output pin as an OUTPUT
  pinMode(outputPin, OUTPUT);

  //analogWrite(knockSensor, LOW);  

  //Begin Serial Communication.
  Serial.begin(9600);

}

void loop() {

  // read the piezo sensor and store the value in the variable sensorReading:
  sensorReading = analogRead(knockSensor);
  Serial.print ("Valor del Sensor: ");
  Serial.println(sensorReading);

  // First determine is knock if Hard (HIGH) or Gentle (LOW)

  //Hard knock (HIGH) is detected
  if (sensorReading >= thresholdHIGH) {

    //Check to see if a Hard Knock matches the Secret Knock in the correct sequence.
    if (secretKnock[secretCounter] == 1) {

      //The Knock was correct, iterate the counter.
      secretCounter++;
      Serial.println("Correct");


    } else {

      //The Knock was incorrect, reset the counter
      secretCounter = 0;
      Serial.println("Fail");
      digitalWrite(outputPin, LOW);
    }//close if

    //Allow some time to pass before sampling again to ensure a clear signal.
    delay(100);

    //Gentle knock (LOW) is detected
  } else if (sensorReading >= thresholdLOW) {

    //Check to see if a Gentle Knock matches the Secret Knock in the correct sequence.
    if (secretKnock[secretCounter] == 0) {

      //The Knock was correct, iterate the counter.
      secretCounter++;
      Serial.println("Correct");

    } else {

      //The Knock was incorrect, reset the counter.
      secretCounter = 0;
      Serial.println("Fail");

    }//close if

    //Allow some time to pass before sampling again to ensure a clear signal.
    delay(100);

  }//close if else

  //Check for successful entry of the code, by seeing if the entire array has been walked through.
  if (secretCounter == (secretKnockLength) ) {

    Serial.println("Welcome");

    //if the sececret knock is correct, illuminate the LED for a couple seconds
    digitalWrite(outputPin, HIGH);


    //Reset the secret counter to 0.
    secretCounter = 0;

  }//close success check

}//close loop".

我的问题是蜂鸣器什么也没检测到。我不知道这是因为蜂鸣器不正确还是别的什么原因。

有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2020-05-02 11:50:20

如果问题是蜂鸣器没有发出任何噪音,那么您应该在代码中使用tone()函数。(https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/)

您的代码编译成功,所以如果这不是您的问题,那么可能是您的项目的连接有问题。

票数 0
EN

Stack Overflow用户

发布于 2020-05-03 07:46:51

要检测敲击,您需要使用不带驱动器电路的压电。根据Arduino tutorial所说,避免用塑料外壳压电。你应该使用像这样看起来像金属盘的压电陶瓷

(图片来自sparkfun)

注:带塑料外壳的压电陶瓷可能含有驱动电路,也可能根本不含压电陶瓷(可能是磁性蜂鸣器)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50278025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档