if (pinFiveInput = HIGH)
{const int Relay = 4;
const int On = LOW;
const int Off = HIGH;
void setup() {
pinMode (Relay, Off);
}
void loop() {
digitalWrite(Relay, On);
delay (500);
digitalWrite (Relay, Off);
delay (500);
}}
it can be any input pin.
what the code does: it is a pulse wave that oscillates at half a second. when the output from pin 4 is high, it sends a signal to a relay, which then activates a pump that pumps water out of a box. however, this pump must only be active when water is inside the box.
so, we have a float switch inside the box that would (in theory) send a signal to one of the digital pin inputs, telling the code that there is water in the box. once pin 5 receives the signal, it activates the loop UNTIL there is no more water (AKA when the float switch stops sending a signal to pin 5).
i dont have a schematic but im at the library and drew one up. i attached the picture. this is roughly how it should work, but i havent tested this exact design in the real life. ignore the calc i didnt feel like tearing out notebook paper
The posted code will not compile because the if is outside of a function. All executable code must be in a function. setup() and loop() are functions.
To add to the suggestions of @lastchancename, the more accepted way to wire a switch is one terminal to ground and the other to an input set to pinMode INPUT_PULLUP. That way the switch input is always at a known state. The input will digitalRead HIGH when open and LOW when closed.
What is the state of the switch (open, closed) when water is present?
Here the line "YourInput" will be true only when your input is high.
For extra credit, look at the Blink without delay example in the Arduino IDE examples folder. Consider modifying your code so you don't have any delays.
Why would you want to do this? If in the future you have a display or control a servo, the delay(500) stops the processor for 1/2 second during which the servo could go out of bounds. By using the approach in Blink without delay, the processor can do other things during that 1/2 second.