const int buzzer=5; //pin 5 is the buzzer output const int ir_led=4; //pin 4 is the ir_led output const int push_button=3; //pin 3 is the push_button input volatile byte alarm = LOW; //alarm state = on or off int noteArray[] = {740, 587}; void setup(){ Serial.begin(9600); pinMode(buzzer,OUTPUT); //configure pin 5 as OUTPUT pinMode(ir_led, OUTPUT); //configure pin 4 as OUTPUT //pinMode(push_button,INPUT); //configure pin 3 as INPUT attachInterrupt(digitalPinToInterrupt(push_button), on_off, RISING); } void loop(){ if (alarm){ int sensor0=analogRead(A0); int sensor1=analogRead(A1); Serial.println((String)"sensor0 = " + sensor0); Serial.println((String)"sensor1 = " + sensor1); if(sensor0 < 1000){ buzzer_sound(true); } else if (sensor1 < 1000) { buzzer_sound(false); } } } void on_off(){ static unsigned long last_interrupt_time = 0; unsigned long interrupt_time = millis(); // if interrupts come faster than 500ms, assume it's a bounce and ignore if (interrupt_time - last_interrupt_time > 500){ alarm = !alarm; //digitalWrite(ir_led, alarm); tone(ir_led, 38000); } last_interrupt_time = interrupt_time; } void buzzer_sound(bool in_out){ noTone(ir_led); delay(100); if(in_out){ for(int i = 0; i < 2; i++){ tone(buzzer, noteArray[i], 750); delay(500); } } else { for(int i = 1; i > -1; i--){ tone(buzzer, noteArray[i], 750); delay(500); } } noTone(buzzer); tone(ir_led, 38000); delay(2500); }