Controlling Solenoid Valve

Hello All,

I am very new to Arduino. I am from Mechanical background. I don't know much about Electronics.

I would like to control a solenoid valve. Solenoid has to SWITCH ON with a ON Press Button. After some time period it has to get SWITCHED OFF. Also I would like to have two more button for increasing and decreasing time period of Valve opening.

Can anyone please help me to write program for it. I know for simply switch ON and OFF. But please guide me for Varying time period

With Regards
Vinayak S Matur

(deleted)

You could Google 'Arduino Programmable Timer'. I did. - Scotty

you didn't quite specify how long you want it to wait for, anything more than a minute should use timer interrupts or a real time clock (rtc) module, which is a lot more complicated. If its less than a minute its relatively simple with delays and variables.

I was feeling nice today, and pretty bored, so I wrote this for you. You may have to debug since I haven't tested it but it shouldn't have too many mistakes.

you can use a relay to turn off and on the valve, which i what i used in this sketch, just copy and paste and feel free to change the wait times, how much it increases and decreases, pins, or anything else you want.
good luck! :slight_smile:

int relaypin = 1;
int RightButtonPin = 2;
int LeftButtonPin = 3;
boolean relay = LOW;
boolean rightButton = LOW;
boolean leftButton = LOW;
boolean bothButtonsLast = LOW;

boolean on = LOW;

int wait = 5000;
boolean rightButtonlast = LOW;
boolean leftButtonlast = LOW;


void setup() {
pinMode(relaypin, OUTPUT);
pinMode(RightButtonPin, INPUT);
digitalWrite(RightButtonPin, LOW);
pinMode(LeftButtonPin, INPUT);
digitalWrite(LeftButtonPin, LOW);
digitalWrite(relay, LOW);

}

void loop() {
rightButton = digitalRead(RightButtonPin);
relay = digitalRead(relaypin);
if ((rightButtonlast == LOW) && (rightButton == HIGH))
{
  wait = wait + 1000; //adds a second to the wait time if the right button is pressed
  rightButtonlast = HIGH; 
}
if ((rightButton == LOW) && (rightButtonlast == HIGH))
{
  rightButtonlast = LOW; // ensures it will not add another second till you let go and press it again
}

//and now the same thing for the left button

if ((leftButtonlast == LOW) && (leftButton == HIGH))
{
  wait = wait - 1000; //subtracts a second to the wait time if the left button is pressed
  leftButtonlast = HIGH; 
}
if ((leftButton == LOW) && (leftButtonlast == HIGH))
{
  leftButtonlast = LOW; // ensures it will not remove another second till you let go and press it again
}


if ((leftButton == HIGH) && (rightButton == HIGH) && (bothButtonslast == LOW)) //when both buttons are pressed at the same time it will turn it off or on)
{
on = !on; //changes it from on to off or off to on.
onButtonLast = HIGH;
}

if ((leftButton == HIGH) && (rightButton == HIGH) && (bothButtonslast == HIGH))
{
  bothButtonslast = LOW; // ensures it will not keep switching between off and on if you dont let go and press it again
}

if (on == HIGH)
{
  digitalWrite(relay, HIGH); //turns the valve on
  delay(wait); // waits for the amount of time chosen
  digitalWrite(relay, LOW); //turns the valve off
  on = LOW;
}



}

ps. don't just upright copy and paste the code, try and understand it. I left some notes so it's easier. It will pay off for debugging if you have to and change it to how YOU want it. It will also certainly help for future projects and this is pretty basic stuff so it's good to know.