Buzzer Stays On Even if Condition is Not True

I was trying to create a burglar alarm using the following circuit I created. Schematic:


Code:

#define alarm 11 // pin with buzzer connected
#define multiplierone 7
#define multipliertwo 2
int analogValue = 0;
float voltage = 0;
int ledDelay = 1000000000;
void setup(){
pinMode(alarm, OUTPUT);
pinMode(multiplierone, OUTPUT);
pinMode (multipliertwo, OUTPUT);
}

void loop(){
analogValue = analogRead(0);
voltage = 0.0048*analogValue;
if(voltage > 0){
	tone(alarm,700);
	delay(333);
	tone(multiplierone,700);
	delay(333);
	tone(multipliertwo,700);
}
}

The clip switch is a custom switch I made using a clothespin with two aluminum pads. There is an insulator between the pads, which is tethered to the hinge-side of the door with a string. As soon as the door is kicked in, the buzzer sounds. However, the buzzer is still sounding even if the insulator was always there since the arduino was powered up. How do I fix the problem?

Image of clip-switch:

New Diagram:

if(voltage > 0){

Use something like 2v, not 0.

Always show us a good proper schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

tone(alarm,2000);

You need to turn this off when not in alarm.

noTone(alarm);

A pulldown resistor is required on A0 so that the voltage can ever go down to 0V.

A digital input with the built-in pullup resistor would be easier to handle. Unless you want to add protection against short circuit or break of the sensor wires, then an analog input is required.

I just connected a tiny resistor I scrounged across A0 and GND. See images for differences.

2 Ohm is much too low, will overload the 3.3V regulator. 10k will be sufficient.

10K does not work. I tried it and only the tiny resistor I used before worked. By the way, the tiny one is 22Ω, not 2Ω. I also paralleled the buzzer pin with some other pin using staples (I jumped pins using staples) to make the current stronger, therefore making the speaker louder.

10 k should work and 22 is way too low.
220 would be safe...

You have an integer value for analog read and then multiply by a fraction - why ?
Just analog read and see if greater than say 60 .

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.