New here, sorry If my formatting is bad.
int button = 2;
int relay = 3;
int speaker = 4;
void setup()
{
pinMode(button, INPUT_PULLUP);
pinMode(relay, OUTPUT);
pinMode(speaker, OUTPUT);
}
void loop()
{
if(digitalRead(button) == 0)
{
digitalWrite(relay, HIGH);
tone (speaker, 100, 300);
delay (3000);
}
else
{
digitalWrite(relay, LOW);
}
}
I want the relay to trigger and stay open only while the button is pressed. However for some reason touching pin 2 triggers the relay and sound, even if I'm not connecting it to the ground.
If i remove the tone (speaker, 100, 300);
it only triggers when the button connecting pin 2 to ground is pressed, but obviously doesn't play the tone.