I have a program with some pretty repetitive blocks of code (i.e Loop A, Loop B, Loop C, then Loop A again... etc) , is it possible to define an entire loop as a single line/function?
Say I have this:
Code:
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Could I abbreviate that in some way so that I can call on it with a single line of code? i.e. Variable X=all of that in the code bracket, and if I want that to run I can just type an X in my main loop? I think that'd be defining a function but I'm not sure how to impliment it.
I've got a very messy program with lots of loops like the above that I'd like to neaten up a bit. Thanks for any advice/tips.