Problem with nested for loops and servos

Someone please help I am inexperienced with C and cant seem to solve this problem.
I have built a machine with three Servos connected to a Uno board.
What i am trying to achieve is for Servo s2 to activate 3 times (inner loop) then loop around and activate servo s7 and then back to the inner loop to activate s2 3 times etc until servo s7 has been activated 4 times and then jump out of the loops to activate servo s5 then go back to the beginning.
What is happening is:
s7 once
s2 3times
s5 once and then back to the beginning
I would be so appreciative of any help with this.
Cliff
Code below

#include <Servo.h>

Servo servos7; //creates a servo object to control servo s7 which is the minute ball lift
Servo servos2; //creates a servo object to control servo s2 which releases all minute balls at the botom of the minute ramp
Servo servos5; //creates a servo object to control servo s5 which releases all hour balls at the botom of the hour ramp

//the following provide the starting positions for Servos
int poss7 = 0; // this is the starting position for Servo S7
int poss2 = 0; // this is the starting position for Servo S2
int poss5 = 0; // this is the starting position for Servo S5

void setup() {
servos7.attach(12); // attaches the servo S7 to pin 12 to the servo object
servos2.attach(5); // attaches the servo s2 to pin 5 to the servo object
servos5.attach(10); // attaches the servo s5 to pin 10 to the servo object

// the next steps set the servos up to their start positions.

for (poss7 =160; poss7 >= 20; poss7 -= 1) { // this makes Lift Servo S7 go from 130 degrees to 20 degrees in steps of one degree
servos7.write(poss7); // tell servo to go to position in variable 'poss7'
delay(10); // waits 10m for the servo to reach the position during each 1 degree step
}
for (poss2 = 130; poss2 >= 35; poss2 -= 1) { // this makes Servo S2 go from 130 degrees to 35 degrees in steps of one degree
servos2.write(poss2); // tell servo to go to position in variable 'poss2'
delay(10); // waits 10ms for the servo to reach the position during each 1 degree step
}
for (poss5 = 130; poss5 >= 35; poss5 -= 1) { // this makes Servo S2 go from 130 degrees to 35 degrees in steps of one degree
servos5.write(poss5); // tell servo to go to position in variable 'poss2'
delay(10); // waits 10ms for the servo to reach the position during each 1 degree step
}
}
void loop()
{
for (int i = 0; i < 4; i++) { //S7 activates once then S2 activates 3 times and then S5 activates and it circles back to S7 it looks like i is not incrementing
s7 () ;
for (int j = 0; j < 3; j++) {
s2 () ;
delay(1000); // This delay is to allow for
}
s5 () ;
}

}

//FUNCTIONS

void s7 ()
// SrvoS7
{
for (poss7 = 20; poss7 <= 160; poss7 += 1) { // this makes S7 go from 20 degrees to 160 degrees in steps of 1 degree
servos7.write(poss7);
delay(10); // waits 10m for the servo to reach the position during each 1 degree step
}
delay(2000); // introduce a delay

 for (poss7 =160; poss7 >= 20; poss7 -= 1) { // this makes Servo S7 go from 160 degrees to 20 degrees in steps of one degree  
 servos7.write(poss7);             
 delay(10);                       //  waits 10m for the servo to reach the position during each 1 degree step
 }
} 

void s2 ()
// Servo S2
{
for (poss2 = 35; poss2 <= 130; poss2 += 1) { // this makes Servo S2 go from 35 degrees to 130 degrees in steps of 1 degree
servos2.write(poss2);
delay(10); // waits 10ms for the servo to reach the position during each 1 degree step
}
delay(3000); // introduce a delay

 for (poss2 = 130; poss2 >= 35; poss2 -= 1) { // this makes Servo S2 go from 130 degrees to 35 degrees in steps of one degree  
 servos2.write(poss2);            
 delay(10);                       //  waits 10ms for the servo to reach the position during each 1 degree step
 }

}

void s5 ()
// Servo S5
{
for (poss5 = 35; poss5 <= 130; poss5 += 1) { // this makes Servo S5 go from 35 degrees to 130 degrees in steps of 1 degree
servos5.write(poss5);
delay(10); // waits 10ms for the servo to reach the position during each 1 degree step
}
delay(3000); // introduce a delay for all the balls to pass over the stop (THIS WILL HAVE TO BE ADJUSTED FOR ALL BALLS TO PASS)

 for (poss5 = 130; poss5 >= 35; poss5 -= 1) { // this makes Servo S2 go from 130 degrees to 35 degrees in steps of one degree  
 servos5.write(poss5);              // tell servo to go to position in variable 'poss2'
 delay(10);                       //  waits 10ms for the servo to reach the position during each 1 degree step
 }
}

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Thank you for your help i really appreciate it
Cliff

I looked at your original post of your code, hoping to see where you had attempted to debug you program by using serial print() to show where the logic was advancing and the values being used in your "for" statements.
But nothing there.
Paul

Very hard to read your code... see note from @groundFungus

And as per the note from @Paul_KD7HB Serial.println() is very useful for debugging.

Your loops should be structured like this...

  
while (true)
{
  Serial.println ("Start");
  
  for (int x = 0; x < 4; x++)
  {

    for (int y = 0; y < 3; y++)
    {
      Serial.println("Servo S2");
      delay(500);
    }

    Serial.println("Servo S7");
    delay(500);
  }

  Serial.println("Servo S5");
  delay(500);
  
}

Output

Start
Servo S2
Servo S2
Servo S2
Servo S7
Servo S2
Servo S2
Servo S2
Servo S7
Servo S2
Servo S2
Servo S2
Servo S7
Servo S2
Servo S2
Servo S2
Servo S7
Servo S5
Start

if you want the sequence
s2 s2 s2 s7 s2 s2 s2 s7 s2 s2 s2 s7 s2 s2 s2 s7 s5
use the following code:

void loop()
{
  for (int i = 0; i < 4; i++) { //S7 activates once then S2 activates 3 times and then S5 activates and it circles back to S7 it looks like i is not incrementing
    for (int j = 0; j < 3; j++) {
      s2 () ;
      delay(1000); // This delay is to allow for
    }
    s7 () ;
    delay(1000); // This delay is to allow for
  }
  delay(1000); // This delay is to allow for
  s5 () ;
}

Red_car

I am very appreciative of your help I will give it a try and forgive me of my inexperience I will use the correct method of posting code in the future.

Cliff

Your specific problem is that the S5() call is inside the outer for loop (it needs to be outside that loop).

The problem is much easier to see if you indent your code... that's a good learning. I personally also like to keep the { and } in line so it is very clear what is being executed in that block/loop.

You could go back and edit your first post to put the code in code tags. Highlight the code and click the </> in the toolbar above. It is that easy.

Ok I will do that for my practice

However in the meantime I have had a couple of people help me and with their kind help I have solved the problem.

Thanks everyone.

Cliff

Red Car Thank you for your advice.

I appreciate your help and I am tidying up the program with indents.

I got some advice from gerivega and fixed the problem its working well.

Getting back to your earlier email where you suggested serial printing for debugging.

I totally agree and thanks for the tip.

I tried to load that bit of code that you gave me into my program and could not work out where to put it.

Also how would I connect a printer ? I have printer connected to my computer but …

I also note that there is a serial monitor as part of the IDU screen (if that is what it’s called) can I use that ? is that what I could use?

I feel much better now things seem to be working

Cliff

You need to open the Serial connection at the start of your program with :

Serial.begin(9600);

Full Program

void setup() 
{
 Serial.begin(9600);

 Serial.println("In setup");
}

void loop() 
{
  Serial.println("In loop");

  delay(2000);
}

You can then view the output in the Serial Monitor in the Arduino IDE (>Tools > Serial Monitor). 9600 is the baud rate and needs to match what is set in your monitor.

Brilliant thanks

For what its worth I am a telecommunications engineer so if your ever looking for advice in my discipline don’t hesitate to message me

Cliff

Thanks for sharing. your intended nesting was not obvious to me as an experienced C, Pascal programmer. You were not helped by C++ not mandating a NEXT command for each FOR let alone the even "more verbose" syntax option for NEXT which allows the compiler to to trap most of these nesting problems especially if it only has the braces to go on.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.