首先感謝MakerPRO歐先生拿給我一套Grove感測器組合。
拿出Grove聲音感測器。
#define SOUND_SENSOR_PIN A0
#define LED_PIN 13
#define THRESHOLD 100
unsigned long t_extended;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
t_extended = millis();
}
void loop() {
int v = analogRead(SOUND_SENSOR_PIN);
Serial.println(v);
unsigned long t = millis();
if(v > THRESHOLD){
t_extended = t + 3000;
digitalWrite(LED_PIN, HIGH);
}
if(t > t_extended){
digitalWrite(LED_PIN, LOW);
}
}
功能很簡單,若音量大於THRESHOLD,就會點亮LED,持續3秒鐘,然後熄滅。
收工。
No comments:
Post a Comment