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.
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.
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
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
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.
}