Welcome back to our programming tutorial using the Arduino IDE. Today we will improve our knowledge of functions with practical examples and a RGB LED.
You can take a look at the previous chapters of the course here:
- Arduino IDE: knowing and using functions #9
- Arduino IDE: composing songs with an array #8.1
- Arduino IDE: what is an array or a vector #8
- Arduino IDE: RGB LED, for, while, do while loops #7
- Arduino IDE: for loops against while / do while #6
- Arduino IDE: while and do while loops #5
- Arduino IDE: turn on LEDs using a button (if) #4.1
- Arduino IDE: the conditional construction IF #4
- Arduino IDE: arithmetic and logical operators #3
- Arduino IDE: variables, constants and macros #2
- How can I use Arduino IDE to write a program? #1
- Arduino IDE: what it is and how it works #0
In the previous article of the guide we exposed the theoretical concepts on the behavior of a function.
In this article you will be able to understand how a function works from within the Arduino IDE software.
In the following project, the RGB LED will light up first in red, then in green and finally in blue and then cyclically return back to red.
The following materials are needed to carry out this project:
- (1) x Elegoo UNO R3
- (1) x Breadboard con 830 tie points
- (4) x M-M Connectors
- (1) x RGB LED
- (3) x Resistor 220 ohm
The connection diagram is as follows:
The wiring diagram is as follows:
Code with a void function (a detailed explanation is given in the following lines):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#define BLUE 3 #define GREEN 5 #define RED 6 #define delayTime 1000 // function declaration void rosso (); void verde (); void blu(); void setup() { pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(BLUE, OUTPUT) digitalWrite(RED, HIGH); digitalWrite(GREEN, LOW); digitalWrite(BLUE, LOW); } // Variables declaration int redValue; int greenValue; int blueValue; void loop() { // Functions call rosso(); verde(); blu(); } void rosso (){ redValue=255; greenValue=0; blueValue=0; analogWrite(RED, redValue); delay(delayTime); digitalWrite(RED, LOW); } void verde () { greenValue=255; redValue=0; blueValue=0; analogWrite(GREEN, greenValue); delay(delayTime); digitalWrite(GREEN, LOW); } void blu (){ blueValue=255; greenValue=0; redValue=0; analogWrite(BLUE, blueValue); delay(delayTime); digitalWrite(BLUE, LOW); } |
The void function is a very useful type of function, since it allows you to carry out various instructions (procedures) based on the number of calls the function made. The behavior of this function is quite self-explanatory when looking at the program.
The second sketch uses functions with a float return type. The purpose of the program is to calculate the value of the voltage applied at a specific terminal on the breadboard. Once calculated, this value will be converted to an 8-bit value (0-255 instead of 0-1023).
The sketch is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
float converti_valore (float a); //function declaration int lettura = 0; //0 is the analog pin A0, void setup () { Serial.begin (9600); <span class="crayon-c">// Enabling serial communication</span> } void loop (){ float l; l = analogRead (lettura); l = converti_valore (l); Serial.print(l); delay (2000); } // Fuction defintion float converti_valore (float a){ float p; p=a/4; // 8-bit scaling return p; // The function converti_valore will return the value of p. Such value will be assigned to the variable l, that called the function. } |
As for the second sketch we can observe the similarity between the declaration, call and definition of the function. The same number of parameters was used.
What happens when a function is called?
As reported in the previous article, the parameter(s) used when calling a function are called current parameters, while the parameters used when defining a function are called formal parameters.
When a function is called in the void loop (or void setup) the value of the current parameter of the function called in question will be assigned to the formal parameter of the defined function. In a nutshell, as regards this sketch, the value of the variable of the current parameter “l” will be assigned to the formal parameter “a” (of the defined function).
How does the return statement behave?
The return statement has a certain argument. This argument is represented by a number or variable. As far as the second sketch is concerned, the return statement has the variable “p” as its argument.
The value of the variable will be assigned to the variable that called the function (in this the value of the variable “p” will be assigned to the variable “l”, which called the function).
Follow us to keep yourself updated with the news!