Can you run methods in arduino code?

Hi. We are writing some code for a servo, and we're not very familiar with the arduino coding style. What we want to do is have three different blocks of code, and then run different blocks depending on the instruction. So, my question is simply can we write say 3 different methods, and then say like, if instruction char is equal to 1, run method one, if equal to to char2, run method 2 etc? If it is possible, what would be the syntax for writing it? I'm more used to Java, so I'd have like

methodName(){
code here;
}
//Run the method
methodName();

Thanks :slight_smile:

Yes, both C and C++ have functions.

@AMPS-N - are you just trying to boost your post count (again), or don't you understand the question?

Sexy_Slenderman:
can we write say 3 different methods, and then say like, if instruction char is equal to 1, run method one, if equal to to char2, run method 2 etc? If it is possible, what would be the syntax for writing it?

If you only have a few choices to make you could use a sequence of if/else statements. A switch/case statement is a more elegant way to compare a scalar value against a large number of values. If the values you're testing for are (mostly) consecutive then you could also use an array of function pointers.

Arduino is C++ with a sort of front end that is very friendly to real-time controller programming.
Put the code that only runs once in setup() and recurring code in loop() and that replaces main() for your purposes.

So the priesthood have renamed functions as members? Typical.

You might find these pages to be of some help from terms to syntax.
The last one references the libraries of the base AVR GCC.

http://www.nongnu.org/avr-libc/user-manual/modules.html

So the priesthood have renamed functions as members? Typical

Classes have member functions, but as far as I know, there's no formal use of "method" in C++.

I think they use method as opposed to function to make it obvious that the method is an integral part of an object not a semi-independent function as in C.

You also need to invoke the object's instance to call it, which is also unlike C.

AWOL:

So the priesthood have renamed functions as members? Typical

Classes have member functions, but as far as I know, there's no formal use of "method" in C++.

There wasn't when I learned it but that was over 20 years ago.

If they didn't change the lingo, students might be able to learn from old books and programmers.