These constants describe the pins. They won't change: */ int i; // Declare Variables int analog0; int CurrentValue; int Senstivity = 10; // Change in light level adjustment // Lower numbers make device more sensitive void setup() { Serial.begin(9600); // initialize the serial communications: pinMode(13, OUTPUT); // Buzzer alarm on Pin 13 for(i=0; i < 5; i++) // Take i readings of A0 pin { delay (100); // Place a small delay between reading analog0=analog0+analogRead(A0); }
analog0=analog0/i; // Divide by i to get average integer A0 value CurrentValue=analog0; // set initial reading and analog value the same
}
void loop() { analog0=0; for(i=0; i < 5; i++) // Take i readings of A0 pin { delay (10); // Small delay between readings analog0=analog0+analogRead(A0); // Add up total of i readings }
analog0=analog0/i; // Divide by i to get average integer A0 value
Serial.println(analog0); // Display current light levels in serial monitor delay(10);
if(abs(analog0-CurrentValue)>Senstivity) { // If change in light level >5, sound alarm Serial.print(analog0); // Display readings in Serial monitor Serial.print("t"); Serial.print(CurrentValue); Serial.print("t"); Serial.println("ALARM"); CurrentValue=analog0; digitalWrite(13, HIGH); // Turn on alarm buzzer on pin 13 delay(100); } else{ CurrentValue=analog0; // if change is <5, then take the new reading as current light level. digitalWrite(13, LOW); // Turn off buzzer on pin 13