Hi! I currently have a Seeeduino v2.21 ATmega328p board connected to a Seeedstudio 4-channel 9V relay shield. There are two solenoid valves involved in the set up. One is controlled by the seeeduino board, the other is not.
My switch is a DPDT Toggle Switch with On(left)-Off-On(right) positions. Currently, On(left) opens Solenoid Valve #1 which is not controlled by the seeeduino board.
Off shuts both Solenoid Valve #1 and #2. On(right) activates a relay on the shield which opens Solenoid Valve #2. I am attempting to make the relay (and thus Valve #2) stay open for 5 seconds before the seeeduino shuts it again.
My code is the problem at the moment:
// Relay Shield including a switch mechanism
int ledPin = 9; // Initialise the LED to pin 9
int switchPin = 8; // Initialise the switch to pin 8
int pinStatus; // Create a variable for the status of the switchPin.
void setup()
{ // Note Logic is inverted because of the need to use the INPUT PULLUP resistor.
pinMode(switchPin, INPUT_PULLUP); // sets the digital pin as input to read switch.
pinMode(ledPin, OUTPUT); //set led pin as output to monitor status of relays.
pinMode(4, OUTPUT);
}
// Loop to open relay 4 with a time delay.
void loop()
{
pinStatus = digitalRead(switchPin); // read input value and store it as the pinStatus
if (pinStatus == LOW) { // check if the button is pressed - Remember logic is inverted.
digitalWrite(4, HIGH);
digitalWrite(ledPin, HIGH); //Turn on the LED
delay(5000);
}
if (pinStatus == HIGH) {
digitalWrite(4, LOW); //For each pin in the array,set output to LOW
digitalWrite(ledPin, LOW); //Turn off the LED
delay(5000);
}
}
The delays in the loop do not postpone the relays time whilst on. Instead it actually takes 5 seconds for the relay to turn on (due to the delay), and I am not quite sure why...
Secondly, I realise my logic is flawed when it comes to the latter part of the loop. I am not sure how to make sure that the relay is turned off when the pinStatus is high (which corresponds to either the On-left or Off position on my switch), because otherwise this would end up in a continuous loop?
I see a couple of things pretty quickly. First your DPDT switch is actually two switches and will need two digital pins. A pin to each throw and ground off the center.
It's pausing every time through loop when the switch is not on because of the delay after turning the relay and led off. I doubt this delay is necessary. Try removing it.
Regarding the switch, the intent was to try and simplify things by having the one side of the switch not through the relay. That way regardless of whether the relay is on or not, as long as there is the 24v feed to the solenoids then Solenoid #1 will always function ( as its the more crucial one).
Solenoid #1 performs it's intended function completely fine at the moment. Is there no possible way to modify my Arduino sketch for what i require for Solenoid #2 with just the one input pin as it is currently?
Apologies I've taken a photo of the basic schematic I've drawn for clarity.
The solenoid that is not controlled through the arduino is controlled directly through the switch and is coloured in Red. (Valve #1). That one works fine and performs as expected.
The one I am having the coding issues with is Valve #2 which runs though the relay.
I'm no electronics guy but that just looks all wrong. I suggest that you post that picture with a better description of your issue in the project guidance forum.
I see you running 24v to one of your Arduino's pins. I'm worried about damage from what I see. I see driving the solenoid directly off pin 4. From your description I was expecting to see a relay switching power to the solenoid.
The Solenoids are 24v solenoids, they require that to work so there is no damage being done. Also, the 24v is going into COM4 on the relay, not the arduino.
I appreciate the input Jimmy but it's not the project guidance that I have an issue with. The circuit works, it is up and running.
As was my original query, the problem is the sketch code required to switch off the relay after 5 seconds when the switch has been pressed. I realise I have my logic wrong but can't seem to figure it out. The delay function is also ineffective.
Oh, okay. I don't see a relay detailed in the schematic. It's quite confusing. Are you sure the 24v is running through the contact side of the relay? Why would the relay contact be labelled com4? In your sketch you're using pin 4 for the relay. Is it a relay shield? Which one it is would be helpful.
So the relay turns on fine but won't turn off? Try some code without the switch. In loop just set the relay pin high then delay then set it low then delay again. That should have it switching on and off regularly. If that doesn't work then your problem isn't in your sketch.
I meant that it continuously cycles on and off which is not what im trying to achieve. Id like to be able to have a 5 second timer when the switch is activated and the solenoid stay off.
Not being funny but all I'm trying to do is get some assistance for a problem I'm having difficulty with. All you've done so far is criticise my electronics (which work fine), asked information about if I was using a relay shield (which I said I was in my first post) asked which one it was (which was also in my first post) yet not actually give any indication you know anymore about this problem than I do.
Why are you posting in this thread then? I'm just trying to get some help.