Programming/Arduino
소리를 이용한 LED를 ON/OFF하기
김구조
2013. 1. 22. 20:25
마이크와 스피커는 같은 구조로 이어져있습니다.
용도에 따라 어떤식으로 좀더 특성에 맞게 구성되어있는것이지 구성자체는 진동을 주느냐 받느냐의 차이에 있습니다.
그래서 스피커를 마이크처럼 이용하여 진동을 받을 시 LED를 on/off 하는 소스를 구현했습니다,.
회로도는 간단합니다. 위처럼 가변저항같이 값을 가져오면 그값을 해석하여 LED를 추가적으로 설치하여 LED를 ON/OFF를 하는 소스입니다.
아래는 소스입니다.
시리얼모니터를 켜시고 받아오는 값을 측정하면서 소스를 조절하시면됩니다.
const int ledPin = 13; // led connected to digital pin 13 const int knockSensor = A0; // the piezo is connected to analog pin 0 const int threshold = 20; // threshold value to decide when the detected sound is a knock or not // these variables will change: int sensorReading = 0; // variable to store the value read from the sensor pin int ledState = LOW; // variable used to store the last LED status, to toggle the light void setup() { pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT Serial.begin(9600); // use the serial port } void loop() { // read the sensor and store it in the variable sensorReading: sensorReading = analogRead(knockSensor); Serial.println(sensorReading); // if the sensor reading is greater than the threshold: if (sensorReading >= threshold) { // toggle the status of the ledPin: ledState = !ledState; // update the LED pin itself: digitalWrite(ledPin, ledState); // send the string "Knock!" back to the computer, followed by newline Serial.println("Knock!"); } delay(100); // delay to avoid overloading the serial port buffer }