In this lesson we create an audible signal where the tone of the signal is proportional to the brightness of the light in the room. We use a photoresistor to measure the light in the room, and then use a passive buzzer to create the audible signal. The video shows you how to hook everything up, and the code we use is below. Your code might be a little different, as the math will depend on the light values where you are.
If you want to follow along at home, an official Arduino Uno R3 is available HERE. In this new series of lessons, I will be using the sensor and other components found in this KIT. The nice digital voltmeter used in the lesson is available HERE.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | int lightPin=A0; int buzzPin=8; int lightVal; int delayT; void setup() { // put your setup code here, to run once: pinMode(A0, INPUT); pinMode(buzzPin,OUTPUT); Serial.begin(9600); } void loop() { lightVal=analogRead(lightPin); delayT=(9./550.)*lightVal-(9.*200./550.)+1.; Serial.println(delayT); digitalWrite(buzzPin, HIGH); delay(delayT); digitalWrite(buzzPin, LOW); delay(delayT); } |