I am trying to use an Arduino to control solenoid valves but I want to open and close the valves with different delays between them and not just loop the same delay over and over. Is there any way to do this? I've attached code of what I thought would be able to work but does not. I also do not want it to loop endlessly just one time with the delays specified which why I put it in the void setup instead of the void loop.
If you could please tell me if this is even possible or if there is some kind of work around it would be greatly appreciated!
Thank you in advance!
const int Y = 3; //initiate pins
const int X = 4;
void setup() {
// put your setup code here, to run once:
pinMode(Y, OUTPUT);
pinMode(X, OUTPUT);
Serial.begin(9600);
digitalWrite(Y, LOW); // The relay is active low, so it turns on when the pin is set to LOW
digitalWrite(X, LOW);
delay(5000);
digitalWrite(X, HIGH);
digitalWrite(Y, HIGH);
delay(7500);
digitalWrite(Y, LOW); // The relay is active low, so it turns on when the pin is set to LOW
digitalWrite(X, LOW);
delay(10000);
digitalWrite(Y, HIGH);
digitalWrite(X, HIGH);
delay(5000);
}
void loop() {
// put your main code here, to run repeatedly:
}
What do you mean by "doesn't work"? The code works just fine. How are your solenoids wired up? Are you trying to drive them directly from the Arduino? A circuit diagram would be useful.
A common mistake is not having ground between the arduino and whatever is powering your solenoids but I don't know without a circuit.
The valve only works for the first on/off and then doesn't turn back on after it only iterates once and then ignores the rest of the code. The picture I have attached is when I only had one solenoid valve connected to the circuit but now I am using two. The solenoid valve is connected to a relay module which is connected to a 12V power supply in order to power the valve. I have attached a picture of the circuit and below is a link to the tutorial I used in order to create the Arduino relay module.
Zardof:
You have your code in setup. Move to loop where the comment says to run repeatedly.
G
Even when I move it to loop, it runs the first ON then OFF and continues running the same delay of 5000 then 7500 but doesnt switch to 10000 then 5000 before turning off it stays at 5000 and 7500
How would I go about the GND connection? Since I'm using a 12V external power supply through the relay module is it a ground connection from the relay to the arduino?
Also I want the code to only run once I dont want it to loop but I want it to turn on with a 5000 delay then off for 7500 then back on for 10000 then off again
It is not obvious how the Arduino is being powered.
If it is your attention to power the Arduino from the voltage regulator PCB on the wireless breadboard, the GND of the Arduino should go to the GND of the breadboard.
Also, there would be a connection from the breadboard +5v to the 5v pin on the Arduino.
The solenoid power supply should be completely isolated from the Arduino!
Edit:
Leaving the code in setup() will run your code once but if you ever need to run it a second time, you will have to reset the Arduino.
It may be that you need a third power supply being dedicated to the Arduino only.