Functions are the set of statements that are executed always when the function is called. Two functions that were mentioned before are already known – setup() and loop(). The programmer is usually trying to make several functions that contain all the statements and then to call them in the setup() or loop() functions.
The structure of the function is following:
type functionName(arguments) //A return type, name and arguments of the function { //The body of a function – statements to execute }
For the example, a function that periodically turns on and off the LED is created:
void exampleFunction() { digitalWrite(13, HIGH); //the LED is ON delay(1000); digitalWrite(13, LOW); //the LED is OFF delay(1000); }
In the example code can be seen that the return type of aexampleFunction function is void that means the function does not have the return type. This function also does not have any arguments because the brackets are empty.
This function should be called inside the loop() function in the following way:
void loop() { exampleFunction(); //the call of the defined function inside loop() }
The whole code in the Arduino environment looks like this:
void loop() { exampleFunction(); //the call of the defined function inside loop() } void exampleFunction() { digitalWrite(13, HIGH); //the LED is ON delay(1000); digitalWrite(13, LOW); //the LED is OFF delay(1000); }
It can be seen that the function is defined outside the loop() or setup() functions.
When some specific result must be returned as a result of a function, then the function return type should be indicated, for example:
//the return type is "int"; arguments are still optional int sumOfTwoNumbers(int x, int y) { //the value next to the "return" should have the "int" type; //this is what will be returned as a result. return (x+y); }
In the loop() this function would be called in a following way:
void loop() { //the call of the defined function inside loop() int result = sumOfTwoNumbers(2, 3); }
Interrupt is a signal that stops the normal execution of a program in the processor, to be able to handle tasks with higher priority. These tasks usually are called Interrupt service routine (IRS) or interrupt handler. Interrupt signals can be generated from the external source, like a change of value on the pin and from the internal source, like a timer. When the interrupt signal is received, the processor stops executing the code and starts the IRS. After completing the IRS, the processor returns to the normal program execution state.
IRS should be as short as possible, and the return type of it is void. Some of normal Arduino functions do not work or behave differently in the IRS, for example, delay() function does not work in the IRS. Variables, used in the IRS must be volatile variables.
Interrupts are used to detect important real-time events, which occur during the normal code execution of the code, without continuously checking them, like pushing a button.
Different Arduino types have different external interrupt pin availability. In most Arduino boards pins, number 2 and 3 are used for interrupts.
To attach interrupt, the function attachInterrupt(digitalPinToInterrupt(pin), ISR, mode) is called. This function has 3 parameters.
The example program that uses interrupt:
volatile bool button =0; //A variable to save button state void setup() { //Define LED pin pinMode(13,OUTPUT); //Define button pin pinMode(2,INPUT_PULLUP); //Attach interrupt to button pin attachInterrupt(digitalPinToInterrupt(2),ButtonIRS,FALLING); } void ButtonIRS() { //IRS function button =!button; } void loop() { digitalWrite (13,button); }
Check Yourself
1. What are the built-in functions used for?
2. Which of the following statements are true?
3. Is it possible to guarantee that the declared built-in function is really built-in?