一、实物连接
二、代码实现
#include <Servo.h>
const int ledPin = 8; //pin13 built-in led
const int soundPin = A0; //sound sensor attach to A0
Servo myservo;
void setup()
{
pinMode(ledPin,OUTPUT);//set ledPin as OUTPUT
Serial.begin(9600); //initialize the serial communication as 9600 bps
myservo.attach(9);//舵机引脚
}
void loop()
{
int value = analogRead(soundPin); //read the value of sound sensor
//delay(200);
Serial.println(value);//print it
if(value < 1000)//if the vallue of sound sensor is greater than 600
{
digitalWrite(ledPin,HIGH); //turn on the led
//myservo.write(90);
//delay(10000); //delay 200ms
}
else //elae
{
digitalWrite(ledPin,LOW); //turn off the led
}
}