Pages: [1]   Go Down
Author Topic: Not sure the right wording but can I DEFINE an entire block of code with 1 Line?  (Read 1061 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 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.
« Last Edit: October 05, 2012, 10:24:26 pm by 777funk » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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}

Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8472
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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

Code:
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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8472
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Something like

Code:
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 smiley


______
Rob
« Last Edit: October 05, 2012, 11:25:22 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

France
Offline Offline
God Member
*****
Karma: 34
Posts: 987
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something like that? :

Code:
#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.
« Last Edit: October 06, 2012, 04:57:18 am by guix » Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
...
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.

http://www.ioccc.org/
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote from: 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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8472
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

_____
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25770
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 362
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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/
Logged

Pages: [1]   Go Up
Jump to: