Need help with loops and switches

I have plugged in a relay and a switch in to a breadboard and I need help with my programming.
I have tested using serial monitor and i know that when the switch is not pressed down it is reading as 1. When it is pressed down there is a reading of 0. It is constantly looping, the relay turns of for 10 seconds and then turns off for ten seconds. I have also made it so when the my ARDUNIO UNO is turned on it runs through a sequence in void setup(). The final bit of code is not importanat.
I need some help.
My code:

int val = 0;
int switchPin = 4;
int relayPin = 7;
int readingPin = 2;

void setup() {
pinMode(switchPin, INPUT);
pinMode(relayPin, OUTPUT);

Serial.begin(9600);

digitalWrite(switchPin, HIGH);

delay(5000);
Serial.println("--------------------");
digitalWrite(relayPin, HIGH);
Serial.println("FLUSHING STARTED");
delay(5000);
Serial.println("FLUSHING");
delay(5000);
digitalWrite(relayPin, LOW);
Serial.println("FLUSHING STOPPED");
Serial.println("--------------------");
}

void loop() {
val = digitalRead(switchPin);
Serial.println(val);
}

void val(); {
 if (val == LOW);
{
 digitalWrite(relayPin, HIGH);
 delay(10000);
 digitalWrite(relayPin, LOW);
}
if (digitalRead(readingPin) == HIGH);      // This bit of code is not important
{
 delay(10000);
 digitalWrite(relayPin, LOW);
}
}

Moderator: Don´t cross post. Tags code added

Help with what?

What is it doing that it's not supposed to be doing? What is it supposed to be doing?

How have you wired up your things? What are your things (did you know there's more than one relay out there in the world?) and what's connected to those things?
You probably want to use INPUT_PULLUP if your button is wired to GND as it seems you've suggested it is. How on Earth did you manage to make it such that the Arduino runs through the setup function when you turn it on?

The switch is connected on kind of like this. The relay is constantly looping.

COM is the middle pin, NC is the left one.

The wiring is shown in the attachments.

I believe I understand your your confusion; let's walk through the journey-

You power up/ reset the arduino:
void setup runs only at boot up-
5 seconds pass - relay turns ON; Immediately, serial monitor prints ""--------------------"; Serial monitor prints "FLUSHING STARTED".

5 seconds later, Serial monitor prints "FLUSHING".
5 seconds later, relay turns OFF; Serial monitor prints "FLUSHING STOPPED".
Immediately, serial monitor prints ""--------------------".

void loop runs constantly after boot. However, you read the pin and print the value of the pin... constantly.

You have a function - "void val()"; however, it is never called to change the state of the relay.

solution:

void loop() {
  val = digitalRead(switchPin);
                       val();  // CALL THE FUNCTION
 
  Serial.println(val);

}

Note:
if (digitalRead(readingPin) == HIGH) will never be true; you never read the pin in void loop() or otherwise.

void loop() {
val = digitalRead(switchPin);
Serial.println(val);
}

That's all that is repeating in your program.

It doesn't go down to your val() function, because it never leaves loop(). You have to call val() somewhere within loop.

I tried this code because the compiler said val could not be used as a function.
This code still is looping. Are there any changes I could make?

int val = 0;
int switchPin = 4;
int relayPin = 7;
int readingPin = 2;

void setup() {
 pinMode(switchPin, INPUT_PULLUP);
 pinMode(relayPin, OUTPUT);

 Serial.begin(9600);
 
 digitalWrite(switchPin, HIGH);

 delay(5000);
 Serial.println("--------------------");
 digitalWrite(relayPin, HIGH);
 Serial.println("FLUSHING STARTED");
 delay(5000);
 Serial.println("FLUSHING");
 delay(5000);
 digitalWrite(relayPin, LOW);
 Serial.println("FLUSHING STOPPED");
 Serial.println("--------------------");
 }

void loop(); {
  val = digitalRead(switchPin)
  Serial.println(val);
  
    if (val == LOW);
  {
    digitalWrite(relayPin, HIGH);
    delay(10000);
    digitalWrite(relayPin, LOW);
  }
  if (digitalRead(readingPin) == HIGH);
  {
    delay(10000);
    digitalWrite(relayPin, LOW);
  }
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks... Tom.. :slight_smile:

Your code is nothing like your "wiring diagram". That has the relay on pin3, switch on pin2 and nothing on pins 4 or 7. Do you want to have another try, preferably a real diagram? A photo of a pencil on paper diagram would be fine.

What exactly are switchPin and readingPin connected to? What is the purpose of readingPin?

Steve

Dear Dim Daniel...
You obviously aren’t a fast learner - with code tags and cross posting.

Maybe it’s worth going back to Reply #6, reading Tom’s suggestion, then asking the mods* to kill one of your multiple topics.

Hi,
I am after some help. My setup wired like the one below.
My code is supposed to delay 5 seconds, turn the relay on for 10 seconds, then turn the relay off when it receives a LOW input on pin 4. The problem is that the relay sequence is constantly looping.
My code is below, along with the wiring setup. PLEASE HELP ME!!!

int val = 0;
int switchPin = 4;
int relayPin = 7;

void setup() {

pinMode(switchPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);

digitalWrite(switchPin, HIGH);
}

void loop() {
digitalWrite(8, HIGH);

if (digitalRead(switchPin) == LOW);
{
delay(5000);
digitalWrite(relayPin, HIGH);
delay(10000)
digitalWrite(relayPin, LOW);
}
}

Also, pin 8 is for testing, and ignore ‘val’

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Hi,
I am after some help. My setup wired like the one below.
My code is supposed to delay 5 seconds, turn the relay on for 10 seconds, then turn the relay off when it receives a LOW input on pin 4. The problem is that the relay sequence is constantly looping.
My code is below, along with the wiring setup. PLEASE HELP ME!!!

int val = 0;
int switchPin = 4;
int relayPin = 7;

void setup() {

pinMode(switchPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);

digitalWrite(switchPin, HIGH);
}

void loop() {
digitalWrite(8, HIGH);

if (digitalRead(switchPin) == LOW);
{
delay(5000);
digitalWrite(relayPin, HIGH);
delay(10000)
digitalWrite(relayPin, LOW);
}
}

Also, pin 8 is for testing, and ignore ‘val’

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Hi,
Please are you cross posting?

Tom... :slight_smile:

int switchPin = 4; // but you use pin 2

int relayPin = 7; // but you use pin 3

pinMode(switchPin, INPUT_PULLUP); // then you should use a switch between pin and ground (no resistor)
but you use the switch between pin and VCC, and use a pull down resistor.

Leo..

Thanks Leo,
The wiring is hooked up to 4, and 7, but the pic isnt.

pinMode(switchPin, INPUT_PULLUP); // connect the switch between pin and ground (no resistor)

Did you do that?

Leo..

@daniel_dim_15, stop cross-posting. This is your second warning. Threads merged.