Else/if regarding pin input

Hi all, I am in my first year of engineering and we have a coding project, and many classmates are struggling.

how would i get THIS code to run, ONLY when there is an input signal detected on one of the input pins?

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);
}

I have tried regular if/else statements but they have not seemed to work. thank you and apologizes if this is in the wrong spot!

Post the requirements that you were given.

Post the code that did not work. Describe what the code actually does and how that is different from what it should do.

There are no input pins in the posted code. What pins? To what are the pins connected?

Post a schematic.

Kudos for posting the code in code tags. Few new users bother to read the rules. Good job.

You’re in the right place, but you really need to look at the documentation for pinMode(), and digitalWrite()

Once that’s sorted, start playing with digitalRead() and then if / else statements.

It’s all there on the Arduino website.

Thanks for using code tags to format your example!

aight
code that failed:


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 file upload is taking a long time so i just threw it up on imgur Imgur: The magic of the Internet

got it, will also check this out. thank you for the response

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?

What does the square wave do?

That is wrong usage of pinMode. See the Arduino language reference for references for the other functions and structures that @lastchancename mentioned.

Welcome :slight_smile:

So you want to turn a relay ON for 1/2 second then OFF for 1/2 second.

And you want this to occur only when an input pin is HIGH. And continue to run (loop) as long as the input is HIGH. Then stop when the input is LOW.

Is this correct?

You must check my syntax for pinMode as I haven't written much code in a while and can easily confuse it with other code languages.

I suggest:

ADD in Setup:
pinMode(YourInput, input)
OR
pinMode(YourInput, pullup)

LOOP:

void loop() {
   if (YourInput){
      digitalWrite(Relay, On);
      delay (500);
      digitalWrite (Relay, Off);
      delay (500);
   }
}

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.

If the water level is below the float switch, is pin 5 HIGH or LOW?
Do you have a 10k pulldown resistor connected between pin 5 and ground?

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