777funk 0 Posted October 6, 2012 Share Posted October 6, 2012 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 } } Quote Link to post Share on other sites
cde 334 Posted October 6, 2012 Share Posted October 6, 2012 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(); Quote Link to post Share on other sites
777funk 0 Posted October 6, 2012 Author Share Posted October 6, 2012 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(); } Quote Link to post Share on other sites
cubeberg 540 Posted October 6, 2012 Share Posted October 6, 2012 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(); } Quote Link to post Share on other sites
Rickta59 589 Posted October 6, 2012 Share Posted October 6, 2012 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 cubeberg 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.