Jump to content
43oh

Can I define an entire block of code?


Recommended Posts

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? For example say A=the below 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 
 } 
} 

Link to post
Share on other sites

That's called a function. Just google "c function" and you'll get a ton of information about it.

 

void loopyloopA()
{
  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 in your main code or wherever you need the loop, you call it as

loopyloopA();

Link to post
Share on other sites

If this is redculous forgive me. Still learning C programming. But here's what I just tried and it wouldn't compile:

 

   
#include 

Servo myservo;  // create servo object to control a servo 
               // a maximum of eight servo objects can be created 

int pos = 0;    // variable to store the servo position 

void setup() 
{ 
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 

Void loop () 
{
 void loopyloopA()
   {
      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
     }
   }

loopyloopA();
}

Link to post
Share on other sites

You've got your function nested inside of another. It needs to be outside. It also needs to be above the calling function, or declared in a function prototype.

I'd suggest finding a tutorial on beginning C programming - it might make things a little less frustrating (although I'm glad to answer any questions).

 

   
#include 

Servo myservo;  // create servo object to control a servo 
               // a maximum of eight servo objects can be created 

int pos = 0;    // variable to store the servo position 

void setup() 
{ 
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 

void loopyloopA()
   {
      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
     }
   }

Void loop () 
{    
loopyloopA();
}

Link to post
Share on other sites
If this is redculous forgive me. Still learning C programming. But here's what I just tried and it wouldn't compile:...

You can't write a function within another function. You might want to spend some time on a tutorial site first. Here is the tutorial on functions:

 

http://www.cprogramming.com/tutorial/c/lesson4.html

 

-rick

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...