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