help needed understanding loops

hello folks

im after some help understanding loops.

if i want to run this loop two times then changes to another loop how do i go about doing that?

#include <MegaServo.h>

MegaServo digger ;  
MegaServo dig ;
MegaServo up ;
MegaServo down ;

int pos = 0;                                           
void setup() 

{ 
digger.attach(8); 
up.attach(9);
down.attach(10);
dig.attach(11);
} 
 
void loop() 
{ 
  

  for(pos = 75; pos < 120; pos += 1)                 
 {                                 
   digger.write(pos);              
 delay(40);                     
  } 
  
 
 for(pos = 130; pos>= 90; pos-= 1)  //up  
 {                                
    up.write(pos);    
    delay(100);                     
 } 
  
}

if i want to run this loop two times then changes to another loop how do i go about doing that?

I for one, would need a bit more information.

Are you talking about the void loop(), or the for( ; ; ) loops?

sorry alphabeta

i meant to say the void loop()

jason

No need to apologize :slight_smile:

Anyways. One way you could do it:

byte currentLoop = 0;
byte loop1Counter = 0;
byte loop2Counter = 0;

void loop(){
  switch (currentLoop){
    case 0: loop1(); break;
    case 1: loop2(); break;
  }
}

void loop1(){
  loop1Counter++;
  if (loop1Counter==2){loop1Counter=0; currentLoop = 1;}
  //do code
}

void loop2(){
  loop2Counter++;
  if (loop2Counter==2){loop2Counter=0; currentLoop = 0;}
  //do code
}

many thanks

i will have a play.
do i place this at the top of my code like this ?.

#include <MegaServo.h>

MegaServo digger ;
MegaServo dig ;
MegaServo up ;
MegaServo down ;

int pos = 0;
void setup()

{
digger.attach(8);
up.attach(9);
down.attach(10);
dig.attach(11);
}


byte currentLoop = 0;
byte loop1Counter = 0;
byte loop2Counter = 0;

void loop(){
  switch (currentLoop){
    case 0: loop1(); break;
    case 1: loop2(); break;
  }
}

void loop1(){
  loop1Counter++;
  if (loop1Counter==2){loop1Counter=0; currentLoop = 1;}
  //do code
}

void loop2(){
  loop2Counter++;
  if (loop2Counter==2){loop2Counter=0; currentLoop = 0;}
  //do code
} 



void loop1()
{


  for(pos = 75; pos < 120; pos += 1)
 {
   digger.write(pos);
 delay(40);
  }


 for(pos = 130; pos>= 90; pos-= 1)  //up
 {
    up.write(pos);
    delay(100);
 }

}

jason = newbie

You have two cases of loop1(), remove both, and replace with this: :slight_smile:

void loop1()
{
  loop1Counter++;
  if (loop1Counter==2){loop1Counter=0; currentLoop = 1;}

  //do code

  for(pos = 75; pos < 120; pos += 1)
  {
    digger.write(pos);
    delay(40);
  }
  for(pos = 130; pos>= 90; pos-= 1)  //up
  {
    up.write(pos);
    delay(100);
  }
}

[edit]What are you trying to do? Or rather… What are you making?[/edit]

i c

i understand now, not sure what you meant by // do code
but i do now

many thanks jason

im making a model digger for my oo scale railway

Sweet!

An Arduino controlled digger! Coooooolll !! Please post a video once you get it work :slight_smile: