Not sure the right wording but can I DEFINE an entire block of code with 1 Line?

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:

{ 
  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. W=all of that in the code bracket, and if I want that to run I can just type a W in my main loop? I think that'd be defining a function but I'm not sure how to implement 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.

void loop()
{ if (button2 == button1)go servo1   // this is where you are calling the function
}

int servo1                              // the name of your function here
{ {
  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 
  } 
}}

I also have to int the function at the beginning with all the other variables or I get errors during compilation. I am sure someone can explain why, I don't know myself.

basically,

function_name
{stuff to do}

You can do that but it's pretty bad coding practice. Make a function of it

void myFunc() { 
   int pos;
  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 
  } 
}

then call with

myFunc();

But if you have a lot of these there's a good chance you only need a single function with 1 or 2 parameters.

How about showing all the code?


Rob

@Giland
That's not valid C code, what are you trying to do?

Something like

void loop() { 
    if (button2 == button1) servo1();   // this is where you are calling the function
}

void servo1 () {                            // the name of your function here
   int pos;
   
  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 
  } 
}

is better.

return data type function_name ()
{stuff to do}

Are you guys working on the same assignment :slight_smile:


Rob

Something like that? :

#define W \
{\
  for(pos = 0; pos < 180; pos += 1)\
  {\
    myservowrite(pos);\
    delay(15);\
  }\
  for(pos = 180; pos>=1; pos-=1)\
  {\
    myservowrite(pos);\
    delay(15);\
  }\
}

//With parameters it's a little harder, you have to create new variables and copy the parameters into it, in order to work with them. Example:
#define PrintIntPlus2(value) \
{\
  int v = value;\
  v += 2;\
  printf("%d", v);\
}


void setup()
{
  W;
  PrintIntPlus2(10);
}

But I agree it's bad practice, but sometimes very useful to create "functions" that are very hard to write the normal way.

guix got it.

I would also put it inside of a do {...} while (0) loop, just to be safe.

As others have pointed out, this is really a bad programming habit. It makes the code difficult to read and difficult to debug.

guix:

...

void setup()
{
 W;
 PrintIntPlus2(10);
}

Don't do that unless you are planning to enter the The International Obfuscated C Code Contest. And for no other reason.

guix:
Something like that?

Don't do that. It is more or less never the right approach, and for people with the level of programming knowledge being demonstrated here encouraging them to think along those lines is fundamentally wrong. Functions are the way to do what they're trying to do, and macros are not.

Thanks to all who've replied. btw This program is from the Arduino examples (called Sweep).

Related to my question regarding Functions... can I include a "break" command inside of a function definition? What about an "if" statement?

You can put what you like in a function as long as it's valid code.


Rob

Related to my question regarding Functions... can I include a "break" command inside of a function definition? What about an "if" statement?

Both "setup" and "loop" are simply functions; if you can put it in one of those, you can put it in your own.

777funk:
Thanks to all who've replied. btw This program is from the Arduino examples (called Sweep).

Related to my question regarding Functions... can I include a "break" command inside of a function definition? What about an "if" statement?

Don't expect break to leave the function though, it's for getting out of a loop. To get out of a function before it normally finishes anyway use return.

Good info here: http://cplusplus.com/doc/tutorial/functions/