電路圖:
零件:CH552、EF92-A micro:servo 180、KS0035 Microphone Sound Sensor with Potentiometer
程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <Servo.h> const int analogInPin = 11; const int servoPin = 15; int threshold = 60; int sensorValue = 0; // value read from the pot void setup() { pinMode(analogInPin,INPUT); Servo_init(); pinMode(servoPin, OUTPUT); Servo_attach(servoPin); } void loop() { // read the analog in value: sensorValue = analogRead(analogInPin); USBSerial_print("sensor = "); USBSerial_print(sensorValue); USBSerial_print("\n"); if (sensorValue>=threshold){ Servo_write(servoPin,90); delay(1000); Servo_write(servoPin,0); delay(1000); Servo_write(servoPin,180); delay(1000); Servo_write(servoPin,0); delay(1000); Servo_write(servoPin,90); } delay(2); } |
沒有留言:
張貼留言