Tutorial 2 on Writing a Custom Function for Intel® Galileo Boards
Up until this point, we've been writing our code inside of the setup and loop functions. This is perfectly fine. But what if your program had lots of complex logic? How would you organize your code?
There is an art to programming and it begins with knowing where to put your code.
Coding can get messy sometimes. If you have ever read any open source code, you may run into some code that:
- lack of comments: including comments helps you and whoever is reading it understand what's going on
- logic isn't organized: organizing your code into logical chunks helps debugging and code maintenance
- names are misleading: variable and function names should properly describe what they intend to do
- cleverness over clarity: code should be easily read; try not to be to cryptic in what you're trying to accomplish
Programs in any language can be written and organized however the programmer sees fit. This is fine if you are the only one reading your code. However, if you are working on a team, it is important to provide some guidelines that everyone follows. One day, the code you write will need to be updated and read by other people. Writing organized code and practicing common coding practices will help you and your fellow engineer write beautiful code.
For tips on improving your coding style, check out Google's C++ coding style guide.
There are two kinds of functions:
- Executes instructions and returns no value (type void)
- Executes instructions and returns a value (types include int, float, double)
See more in the Data Types section on the Arduino reference page.
Function types
Type void:
void sayHello(){
Serial.println("Hello!");
}
Type integer:
int theAnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything(){
return 42;
}
We can assign the value of functions that return something to variables:
int myAnswer = theAnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything();
Let's say that we wanted to make a function a bit more useful. We can use function parameters to pass unique data that will have an impact on the result of the function. For example:
int add(int x, int y){
return x + y;
}
We would call this function, passing custom parameters like so:
int numbersAdded = add(2, 2); // The variable numbersAdded is equal to 4
There are a variety of ways you can write a function. It all depends on what you want it to do. A good place to start writing a custom function is to define the function signature.
Function Signature
Before writing the code of a function, it's best to determine what you are trying to accomplish.
- Will it return a value?
- How many parameters will be passed to it (if any)?
- What's the purpose of the function?
Let's pretend you're writing a program that outputs a custom message to the serial monitor. This message will comment on the weather (that could be based off a temperature sensor, for example).
You don't know what exactly the code inside the function looks like yet, but you know what you want it to do:
- Take two custom parameters to concatenate (or join) together
- Take an number of type Double that will append to the message
- Does not return a value
- The serial monitor output would print out "It is 30 degrees outside."
Our signature could look something like this:
void myMessage(String msg1, String msg2, double deg){}
Calling the function would look something like:
// Degrees here would be a predefined variable based on a temperature sensor reading.
myMessage("It is", degrees, "outside");
Challenge
How would you write the code to complete this function's task?
- Introduction
- Advanced Input
- Pulse Width Modulation (PWM)
- Writing a custom function
- Using a custom function
- Wrap Up