Let's Make Robots!

Simple Question about Programming the Arduino

Having built many robots and programmed in several languages, I am new to Arduino and C.  I wish to make a subroutine and place it in a working program.

I have two programs that are running perfectly right now at this moment. I want to combine them together and have the second short program be able to be called many times as a subroutine and be placed in the larger program. I can't for the life of me make it happen.  I usually get some error when I trie to compile it.  I just want to know how to make a subroutine.

 In Pbasic on the BasicStampII, I would simply have my program running and say GoSub either the line number or the name of the subroutine.  When it was finished, I would put a RETURN and I could call it a hundred times if I wanted to. If you could explain this to an ignorant about C programmer, I can get on with my project.

thanks!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

C programs can only have 1 void main() function and 1 void setup() function.

void main() // first seperate program

{

  do something;

}

void main() // second seperate program

{

  do something else;

}

You have to rename the main of the function program to something other than main. I just used myFunction() here but that`s really vague. Use easily remembered names. Don`t forget variable and constants which the function needs.

void myFunction()

{

  do something;

}

void main()

{

  myFunction(something);

}

Edit: you also use return in C to exit a function. Void functions return nothing so it`s not required but it can be handy for exiting earlier if theres an error or something.

Thanks for the quick reply.

:-)