Hi there, I'm new to programming and Arduino and am having some trouble figuring out why my simple on/off timer switch is not working when the switch pin is HIGH. by switch I mean a water level floating switch. I am using an Arduino Nano for this.
My project goal is to switch on a pump for 5 seconds when the switch momentarily triggers. At the moment I'm using an LED in place of the pump with a 220 ohm resistor in series.
The problem is I can get the circuit to work in the exact opposite manner I need it to (i.e. the output is HIGH when the input is LOW, and when the switch triggers HIGH - the LED turns off after 5 seconds. When I change the switch input expectancy to HIGH in the code (as it is in the code below), the LED pin remains HIGH regardless of the switch input.
here is the code:
//
#define LED_PIN 2
#define FLOAT_SWITCH 5
void setup()
{
pinMode(LED_PIN, OUTPUT);
pinMode(FLOAT_SWITCH, INPUT);
}
void loop()
{
if (digitalRead(FLOAT_SWITCH) == HIGH) //Changed to LOW and it works in the exact opposite manner
{
digitalWrite(LED_PIN, HIGH);
delay(5000);
}
else
{
digitalWrite(LED_PIN, LOW);
}
}
I'm not 100% sure what you mean by wiring the switch between Pin 5 and GND? I have 5v from the nano going to one side of the switch and the other switch wire going to pin 5 at the moment. Are you saying to wire the other wire to GND instead of pin 5?
thanks all in advance for you patience while I'm learning
One side of switch to PIN 5, the other side of the switch to GND.
When the switch is not pressed PIN 5 will be HIGH (because you pull it high with INPUT_PULLUP)
When the switch is pressed PIN 5 will be LOW (because you connect it to GND)
Awesome! your answer in conjuction with alto777 and red_car's explanations has walked me through it. Now I know that it works I'll have to wrap my head around it a bit better.
Thank you all for your help! I will no doubt be back at some point