This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:iot-open:programming_fundamentals_rtu:interrupts_and_sub-programs [2018/01/24 14:01] – created Agrisnik | en:iot-open:programming_fundamentals_rtu:interrupts_and_sub-programs [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 2: | Line 2: | ||
===== Functions ===== | ===== Functions ===== | ||
+ | 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: | ||
+ | <code c> | ||
+ | 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: | ||
+ | |||
+ | <code c> | ||
+ | void exampleFunction() | ||
+ | { | ||
+ | digitalWrite(13, | ||
+ | delay(1000); | ||
+ | digitalWrite(13, | ||
+ | delay(1000); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | In the example code can be seen that the return type of an // | ||
+ | |||
+ | This function should be called inside the //loop()// function in the following way: | ||
+ | |||
+ | <code c> | ||
+ | void loop() | ||
+ | { | ||
+ | exampleFunction(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The whole code in the Arduino environment looks like this: | ||
+ | |||
+ | <code c> | ||
+ | void loop() | ||
+ | { | ||
+ | exampleFunction(); | ||
+ | } | ||
+ | |||
+ | void exampleFunction() | ||
+ | { | ||
+ | digitalWrite(13, | ||
+ | delay(1000); | ||
+ | digitalWrite(13, | ||
+ | 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: | ||
+ | |||
+ | <code c> | ||
+ | int sumOfTwoNumbers(int x, int y) //the return type is " | ||
+ | { | ||
+ | return (x+y); //the value next to the " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | In the //loop()// this function would be called in a following way: | ||
+ | <code c> | ||
+ | void loop() | ||
+ | { | ||
+ | int result = sumOfTwoNumbers(2, | ||
+ | } | ||
+ | </ | ||
===== Interrupts ===== | ===== Interrupts ===== | ||
+ | // | ||
+ | |||
+ | 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, that occur during the normal code execution of the code, without continuously checking them, like pushing a button. | ||
+ | |||
+ | Different Arduino types has different external interrupt pin availability. In most Arduino boards pins number 2 and 3 are used for interrupts. | ||
+ | |||
+ | To attach interrupt, the function // | ||
+ | - //pin// – the number of a pin number where the interrupt signal generating device will be attached, | ||
+ | - //ISR// – the name of a function of interrupt service routine, | ||
+ | - //mode// - defines when interrupt signal is triggered. There are four basic //mode// values: | ||
+ | * LOW - interrupt is triggered when the pin value is LOW, | ||
+ | * HIGH - interrupt is triggered when the pin value is HIGH, | ||
+ | * CHANGE - interrupt is triggered when the pin value is changed, | ||
+ | * RISING - interrupt is triggered when the pin value is changed from LOW to HIGH. | ||
+ | |||
+ | The example program that uses interrupt: | ||
+ | |||
+ | <code c> | ||
+ | volatile bool button =0; //a variable to save button state | ||
+ | |||
+ | void setup() { | ||
+ | pinMode(13, | ||
+ | pinMode(2, | ||
+ | attachInterrupt(digitalPinToInterrupt(2), | ||
+ | } | ||
+ | |||
+ | void ButtonIRS() { // IRS function | ||
+ | button =!button; | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | digitalWrite (13, | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Check yourself** | ||
+ | |||
+ | 1. What are the built-in functions used for? | ||
+ | * To reduce the size of the program | ||
+ | * To delete unnecessary functions | ||
+ | * To simplify the source file | ||
+ | * To increase the speed of the program | ||
+ | |||
+ | 2. Which of the following statements are true? | ||
+ | * built-in functions must return a value. | ||
+ | * built-in functions can not return values. | ||
+ | * The compiler can ignore the declaration of the built-in function. | ||
+ | * built-in functions can not contain more than 10 lines of code. | ||
+ | 3. Is it possible to guarantee that the declared built-in function is really built-in? | ||
+ | * guarantee is not possible, in each individual case it is different | ||
+ | * can be confidently ensured that the function you have declared as built-in is really built-in | ||
+ | |