Two valves and a timer

Hey everyone

I'm trying to set up my Uno to open a Solenoid Valve for 3 minutes before closing and then do the same for a second valve

I am a little out of practice and I got one valve to work but I can't figure out how to set up the process to do the second valve

Here is the code I have so far:

int solenoidPin = 4;
void setup() {
// put your setup code here, to run once:
pinMode(solenoidPin1, OUTPUT);
pinMode(solenoidPin2, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(solenoidPin1, HIGH); //Switch Solenoid ON
delay(180000); //Wait 3 minutes
digitalWrite(solenoidPin1, LOW); //Switch Solenoid OFF
digitalWrite(solenoidPin2, HIGH); //Switch Solenoid ON
delay(180000); //Wait 3 minutes
digitalWrite(solenoidPin2, LOW); //Switch Solenoid OFF

For the future, read the first topics telling "How to use the Forum", "How to attach code" etc.

There's code missing……

What I can se is that You have not declared SolenoidPin2. Default, by compiler is zero…..

Hey everyone

I'm trying to set up my Uno to open a Solenoid Valve for 3 minutes before closing and then do the same for a second valve

I am a little out of practice and I got one valve to work but I can't figure out how to set up the process to do the second valve

Here is the code I have so far:

int solenoidPin = 4;   
void setup() {
  // put your setup code here, to run once:
  pinMode(solenoidPin1, OUTPUT);           
  pinMode(solenoidPin2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:  
  digitalWrite(solenoidPin1, HIGH);    //Switch Solenoid ON
  delay(180000);                      //Wait 3 minutes
  digitalWrite(solenoidPin1, LOW);     //Switch Solenoid OFF
  digitalWrite(solenoidPin2, HIGH);    //Switch Solenoid ON
  delay(180000);                      //Wait 3 minutes
  digitalWrite(solenoidPin2, LOW);     //Switch Solenoid OFF

Thanks for using code tags!

Now I se 2 undeclared variables. Both solenoidPin1 solenoidPin2 and are not declared. They will both be set to zero by the compiler. That means that Your code will send the I/O to the USB read, or write, port, not to the outputs You want.

I made a new post with the inserted code. Thank You

Railroader:
Thanks for using code tags!

Now I se 2 undeclared variables. Both solenoidPin1 solenoidPin2 and are not declared. They will both be set to zero by the compiler. That means that Your code will send the I/O to the USB read, or write, port, not to the outputs You want.

How do I declare? I haven't coded in a few months so I'm rusty on the basics.

would it be something like

input soleoidPin1

at the beginning?

What's is happening here? I did read Your properly attached code and replied to it. In this topic it is lost. Use the Reply box down to the right.

This was my recent reply:

Thanks for using code tags!

Now I se 2 undeclared variables. Both solenoidPin1 solenoidPin2 and are not declared. They will both be set to zero by the compiler. That means that Your code will send the I/O to the USB read, or write, port, not to the outputs You want.

I'll ask moderator to Clean up.

You need to declare and give values to solenoidPin1 and solenoidPin2!

#declare solenoidPin1 4
#declare solenoiPin2 5

Or

int solenoidPin1 4;
int soulenoidPin2 5;

Railroader:
You need to declare and give values to solenoidPin1 and solenoidPin2!

#declare solenoidPin1 4

#declare solenoiPin2 5




Or 


int solenoidPin1 4;
int soulenoidPin2 5;

not quite working. It still won't recognize the second one. Did I put it in the wrong place?

int solenoidPin1 4;
int soulenoidPin2 5;   
void setup() {
  // put your setup code here, to run once:
  pinMode(solenoidPin1, OUTPUT);           
  pinMode(solenoidPin2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:  
  digitalWrite(solenoidPin1, HIGH);    //Switch Solenoid ON
  delay(180000);                      //Wait 3 minutes
  digitalWrite(solenoidPin1, LOW);     //Switch Solenoid OFF
  digitalWrite(solenoidPin2, HIGH);    //Switch Solenoid ON
  delay(180000);                      //Wait 3 minutes
  digitalWrite(solenoidPin2, LOW);     //Switch Solenoid OFF
}

Oh, shit, You copied my misspelled reply! Check the spelling! Not int soulenoidPin2 5;
but int solenoidPin2 5;

My mistake. Late here, 1 Am and bed time....

Railroader:
Oh, shit, You copied my misspelled reply! Check the spelling! Not int soulenoidPin2 5;
but int solenoidPin2 5;

My mistake. Late here, 1 Am and bed time....

Still not working. the error says "expected initializer before numeric consent"

int solenoidPin1 = 4;
int solenoidPin2 = 5;

@dduvall

TOPIC MERGED.

Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.

const byte solenoids = 2;
const byte solenoidPin[ 2 ] = { 4, 5 };  // this is an array with 2 elements that have the same code name and a number.
byte solenoidActive; // default = 0
   
void setup() 
{
  for ( byte i = 0; i < 2; i++ ) // this is a for-loop. it has an ( index init, run-condition, do-code )
  {
    pinMode( solenoidPin[ i ], OUTPUT );           
  }
}

void runDevice( byte pin )
{
  digitalWrite( solenoidPin[ pin ], HIGH );    //Switch Solenoid ON
  delay( 180000 );                                        // Waste 3 minutes
  digitalWrite( solenoidPin[ pin ], LOW );    //Switch Solenoid OFF
}


void loop() 
{
  runDevice( solenoidActive );
 
  solenoidActive ^= 1;  // bit logic XOR makes 0 into 1 and 1 into 0. XOR is true if the bits are different. 0 ^ 1 = 1, 1 ^ 1 = 0. 
}

That delay() limits the sketch to do One Thing At A Time.

Learn to use the millis() counter and code won’t have to STOP in a delay or other execution-blocking code.

Then, the sketch can watch inputs and respond the whole time. Maybe monitor the physical process and make alarm on breakdowns.

Attach the complete code giving the compiler errors.

Railroader:
Attach the complete code giving the compiler errors.

I moved the close code tag to where it should be in my post above.
I selected all and put it in my IDE and it compiles fine.

.....
Sketch uses 966 bytes (2%) of program storage space. Maximum is 32256 bytes.
Global variables use 12 bytes (0%) of dynamic memory, leaving 2036 bytes for local variables. Maximum is 2048 bytes.
.....
avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: reading input file "/tmp/arduino_build_754592/two_pumps.ino.hex"
avrdude: writing flash (966 bytes):

Writing | ################################################## | 100% 0.19s

avrdude: 966 bytes of flash written
avrdude: verifying flash memory against /tmp/arduino_build_754592/two_pumps.ino.hex:
avrdude: load data flash data from input file /tmp/arduino_build_754592/two_pumps.ino.hex:
avrdude: input file /tmp/arduino_build_754592/two_pumps.ino.hex contains 966 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.14s

avrdude: verifying ...
avrdude: 966 bytes of flash verified

avrdude done.  Thank you.
int solenoidPin1 4;
int soulenoidPin2 5;

Won't compile and using an int for a pin number is wasteful. Needs = to assign a value.

Try what my previous sketch did making the pin number variables into an array.
It is very easy to add more.

Hi.
I'm just a self taught home gamer who makes loads of mistakes and writes dreadful code in comparison to many here, but here's my take.

GoForSmoke's suggestion may indeed work, but it introduces some coding techniques that may seem a bit daunting and will require some additional reading if you're not familiar with them.
I suggest going back to your original code and delete the first line and replace it with these two lines.

int solenoidPin1 = 4;
int solenoidPin2 = 5;

These declare the 2 variables and initialise them to 4 and 5.
This will work but a lot can be improved to increase efficiency. Of course, your sketch is simple and efficient code is not really necessary, but it's good practice and will become more significant as your programming skills develop.

These two pin numbers don't need to be variables as the wiring isn't going to change when running, so they can be constants, or const int instead of just int.
As the pin numbers are less than 255 and positive, they can be const byte rather than const int.

const byte solenoidPin1 = 4;
const byte solenoidPin2 = 5;

After this, I would suggest reading up on using millis() to replace delays and getting to grips with arrays.

Of course, the Arduino is not able to drive the solenoids directly, electronic switces or relays are required.

(deleted)

smarts-jb:
(Not that an Arduino can directly drive a relay either; same inductive issues as with a solenoid)

Oops! I've been driving the likes of these directly from Pro Minis (not this supplier. just a random google hit)