Arduino
const int ledPin = 13; //built-in led
const int soundPin = A0; //sound sensor
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
int value = analogRead(soundPin); //read the value of value sensor
Serial.println(value);
if (value > 58) //if the value of sound sensor is greater than 600
{
printf('turn on');
digitalWrite(ledPin, HIGH); //turn on
delay(200);
digitalWrite(ledPin, LOW); //turn off
}
}
树莓派
C
#include <stdio.h>
#include <wiringPi.h>
#include <pcf8591.h>
#define PCF 120
int main (void)
{
int value;
int count = 0;
wiringPiSetup();
//setup pcf8591 on base pin 120, and address 0x48
pcf8591Setup(PCF, 0x48);
while(1) //loop forever
{
value = analogRead(PCF + 0);
//printf("%d\n", value);
if (value > 70){
count ++;
printf("Voice In!! %d\n", count);
}
}
return 0;
}
Python
#!/usr/bin/env python
import PCF8591 as ADC
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
def setup():
ADC.setup(0x48)
def loop():
count = 1
while True:
voiceValue = ADC.read(0)
if voiceValue:
print('Value:{}'.format(voiceValue))
if voiceValue > 65:
print("Voice detected! {}".format(count))
count += 1
time.sleep(0.2)
if __name__ == '__main__':
try:
setup()
loop()
except KeyboardInterrupt:
pass
网友评论