Devices & Components
Arduino Mega 2560 Rev3
Photo resistor
Buzzer
Resistor 10k ohm
Project description
Code
Code
arduino
1int buzzer = 11; 2 3 4void setup(){ 5 pinMode(buzzer,OUTPUT); 6} 7 8 9 10void alarm(){ 11 int i; 12 int j = 0; 13 while(j < 10){ 14 //output a frequency 15 for(i=0;i<80;i++){ 16 digitalWrite(buzzer,HIGH); 17 delay(1);//wait for 1ms 18 digitalWrite(buzzer,LOW); 19 delay(1);//wait for 1ms 20 } 21 22 //output another frequency 23 for(i=0;i<100;i++){ 24 digitalWrite(buzzer,HIGH); 25 delay(0.5); 26 digitalWrite(buzzer,LOW); 27 delay(0.5); 28 } 29 30 ++j; 31 } 32} 33 34 35 36void loop(){ 37 int x = analogRead(A0); 38 delay(10); 39 40 if(abs(analogRead(A0) - x) > 2){ 41 alarm(); 42 } 43}
Downloadable files
Schematics
Use a 10k resistor.
Schematics

Schematics
Use a 10k resistor.
Schematics

Comments
Only logged in users can leave comments