Nested loop with multiple counters

Hi everyone,

I’m driving two solenoids with Arduino. I need to open (0.5 sec delay) and close (0.1 sec delay) first solenoid 20 times and then open second solenoid (5 sec delay) and then close it. I need to repeat this procedure 100 times. I want to use FOR loop but not sure how to use two counters in the nested loop.

Thank you very much in advance.

Alex

Something like;

for 100 times
for 20 times
open1
delay 0.5 sec
close1
delay 0.1 sec
loop
open 2
delay 5.0 sec
close 2
loop

I was actually wondering if it might be less complicated to write two separate functions. Something like..

void loop()
For...
Solinoid1(val)
For...
Solinoid2(val)

void Solenoid1(val?)
Delay x
Do stuff
Delay x

void Solenoid2(val?)
....

You can always separate segments into functions. Makes for cleaner looking code as well.

What I need is to switch Valve (1) then to switch valve (2 ) and then repeat this sequence 100 times.
The suggested code would run Valve (1) and then valve (2) but I don’t see how would it repeat this sequence 100 times.

Look again.

aiasnikov:
The suggested code would run Valve (1) and then valve (2) but I don’t see how would it repeat this sequence 100 times.

This suggests you should spend some time studying the for/next control structure.

Edit:

I will try write this up for you when I get home from a wedding.

Do you need to open and close the second solenoid 20 times like the first one, or only once for twenty of the first one?

CodeCruncher:
Edit:

I will try write this up for you when I get home from a wedding.

Do you need to open and close the second solenoid 20 times like the first one, or only once for twenty of the first one?

The second valve needs to be switched only once.

Would be even less complicated to just make one function for controlling the solenoids and just passing too the function a variable for which pin to act on.

In keeping with your nestled loop theory you would have a set selenoid 2 nested outside for 1 to 20 set solenoid 1 nestled inside for 1 to 5

Based on DKWatson's summary...

void loop() {
// Create an integer variable called "i" set value to 0, continue until i = 99 (100 loops), increase i by 1
for (int i=0; i <= 99; i++) {
// You are now inside the "i" loop
// Create an integer variable called "j" set value to 0, continue until j = 19 (20 loops), increase j by 1
for (int j=0; i <= 19; j++) {
// You are now inside the "j" loop
// use the correct code to open solenoid, not this
open solenoid 1;
// Delay .5 second
delay(500);
// use the correct code to close solenoid, not this
close solenoid 1;
// Delay .1 second
delay(100);
// "j" loop ends here and goes back to start of "j" loop
}
// After looping through "j" code 20 times it kicks out of "j" code back into "i" code and continues here
// use the correct code to open solenoid, not this
open solenoid 2;
// Delay 5 seconds
delay(5000);
// use the correct code to close solenoid, not this
close solenoid 2;
// "i" loop ends here and goes back to start of "i" loop
}
// Once it has looped through "i" 100 times it will go to the top of the "void loop" to start the
// process all over again unless you exit this loop.
}

You could however make it much cleaner to look at by doing something like this...

void loop() {
// Create an integer variable called "i" set value to 0, continue until i = 99 (100 loops), increase i by 1
for (int i=0; i <= 99; i++) {
// You are now inside the "i" loop
// Create an integer variable called "j" set value to 0, continue until j = 19 (20 loops), increase j by 1
for (int j=0; i <= 19; j++) {
// You are now inside the "j" loop
solenoidState(1, 500, 100)
// "j" loop ends here and goes back to start of "j" loop
}
// After looping through "j" code 20 times it kicks out of "j" code back into "i" code and continues here
solenoidState(2, 5000, 0)
// "i" loop ends here and goes back to start of "i" loop
}
// Once it has looped through "i" 100 times it will go to the top of the "void loop" to start the
// process all over again unless you exit this loop.
}

void solenoidState(int SolNumOrPin, int delay1, int delay2) {

// use the correct code to open solenoid, not this
open solenoid (SolNumorPin);
// Delay 1
delay(delay1);
// use the correct code to close solenoid, not this
close solenoid (SolNumorPin);
// Delay 2
delay(delay2);
}

He needs 5 sets so first for:“A” should be 1 to 5.
Then he needs 20 repetitions so nestled for:“B” is 1-20, do solenoid#1, next B
Then he needs just one set,do Solendoid #2
Then next “A”

Slumpert:
He needs 5 sets so first for:“A” should be 1 to 5.
Then he needs 20 repetitions so nestled for:“B” is 1-20, do solenoid#1, next B
Then he needs just one set,do Solendoid #2
Then next “A”

Ok you're thing he needs a 100 in total. I'm inclined to agree with DKWatson, that he wants...
20 times this below done 100 times.
open1
delay 0.5 sec
close1
delay 0.1 sec
loop
open 2
delay 5.0 sec
close 2

CodeCruncher:
Based on DKWatson's summary...

void loop() {
// Create an integer variable called "i" set value to 0, continue until i = 99 (100 loops), increase i by 1
for (int i=0; i <= 99; i++) {
// You are now inside the "i" loop
// Create an integer variable called "j" set value to 0, continue until j = 19 (20 loops), increase j by 1
for (int j=0; i <= 19; j++) {
// You are now inside the "j" loop
// use the correct code to open solenoid, not this
open solenoid 1;
// Delay .5 second
delay(500);
// use the correct code to close solenoid, not this
close solenoid 1;
// Delay .1 second
delay(100);
// "j" loop ends here and goes back to start of "j" loop
}
// After looping through "j" code 20 times it kicks out of "j" code back into "i" code and continues here
// use the correct code to open solenoid, not this
open solenoid 2;
// Delay 5 seconds
delay(5000);
// use the correct code to close solenoid, not this
close solenoid 2;
// "i" loop ends here and goes back to start of "i" loop
}
// Once it has looped through "i" 100 times it will go to the top of the "void loop" to start the
// process all over again unless you exit this loop.
}

I got the idea. Thank you very much.

aiasnikov:
I got the idea. Thank you very much.

You're welcome.