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

使用以下代码
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".我的问题是蜂鸣器什么也没检测到。我不知道这是因为蜂鸣器不正确还是别的什么原因。
有什么想法吗?
发布于 2020-05-02 11:50:20
如果问题是蜂鸣器没有发出任何噪音,那么您应该在代码中使用tone()函数。(https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/)
您的代码编译成功,所以如果这不是您的问题,那么可能是您的项目的连接有问题。
发布于 2020-05-03 07:46:51
要检测敲击,您需要使用不带驱动器电路的压电。根据Arduino tutorial所说,避免用塑料外壳压电。你应该使用像这样看起来像金属盘的压电陶瓷

(图片来自sparkfun)
注:带塑料外壳的压电陶瓷可能含有驱动电路,也可能根本不含压电陶瓷(可能是磁性蜂鸣器)。
https://stackoverflow.com/questions/50278025
复制相似问题