Devices & Components
Arduino Uno Rev3
Ultrasonic Sensor - HC-SR04 (Generic)
Temperature Sensor
LED (generic)
Software & Tools
ARTe (Arduino Real-Time extension)
Project description
Code
Multitasking-RealTime-Arduino-System
This simple project shows how Arduino can be used with a real time operating system (Erika). A special version of Arduino called ARTe was used for software development ( http://retis.sssup.it/?q=arte ). ARTe (Arduino Real-Time extension) is an extension to the Arduino framework that supports multitasking and real-time preemptive scheduling. Thanks to ARTe, the user can easily specify and run multiple concurrent loops at differents rates, in addition to the single execution cycle provided by the standard Arduino framework. Today ARTE supports the most popular platforms: Arduino UNO and Arduino DUE. In addition to the single loop present in the standard Arduino approach, the user can easily specify a number of concurrent loops to be executed at specific rates. Concurrency and real-time scheduling is provided by the ERIKA Enterprise (http://erika.tuxfamily.org/drupal/) open-source real-time kernel. Loops can use the standard Arduino libraries, which have been enhanced to guarantee mutual exclusion in the access of shared data structures. The impact of ARTe in terms of footprint and runtime overhead has beed evaluated by extensive tests and resulted to be negligible. In this project was used to run 3 periodic processes that handle different activities. A process for managing every 3 seconds the acquisition of the distance through the sensor HC-SR04. Another process for flashing a LED every 7 seconds. A final process to read the temperature returned by the LM35 sensor expressed in degrees Celsius is executed every 11 seconds. All processes are managed in real time mode.
Multitasking and Real-Time Arduino Processing
arduino
This simple project shows how Arduino can be used with a real time operating system (Erika). A special version of Arduino called ARTe was used for software development ( http://retis.sssup.it/?q=arte ). ARTe (Arduino Real-Time extension) is an extension to the Arduino framework that supports multitasking and real-time preemptive scheduling. Thanks to ARTe, the user can easily specify and run multiple concurrent loops at differents rates, in addition to the single execution cycle provided by the standard Arduino framework. Today ARTE supports the most popular platforms: Arduino UNO and Arduino DUE. In addition to the single loop present in the standard Arduino approach, the user can easily specify a number of concurrent loops to be executed at specific rates. Concurrency and real-time scheduling is provided by the ERIKA Enterprise (http://erika.tuxfamily.org/drupal/) open-source real-time kernel. Loops can use the standard Arduino libraries, which have been enhanced to guarantee mutual exclusion in the access of shared data structures. The impact of ARTe in terms of footprint and runtime overhead has beed evaluated by extensive tests and resulted to be negligible. In this project was used to run 3 periodic processes that handle different activities. A process for managing every 3 seconds the acquisition of the distance through the sensor HC-SR04. Another process for flashing a LED every 7 seconds. A final process to read the temperature returned by the LM35 sensor expressed in degrees Celsius is executed every 11 seconds. All processes are managed in real time mode.
1//Pin definition 2static int trigger = 9; // Pin HC-SR04 3static int echo = 8; // Pin HC-SR04 4static int led_HC = 10; // Pin HC-SR04 5static int led_pulsed = 13; // Timed pulsed led 6static int led_temperature = 12; // Led to temperature overflow 7static int LM35_analogPin = 0; // analog pin for temperature sensor 8 9 10// Variables for calculating the distance with sensor HC-SR04 11long duration; // Flight time of signal 12long distance; // Calculated distance 13float temp_C; // temperature variable (Celsius degrees) 14 15void setup() { 16 //Serial communication 17 Serial.begin(9600); 18 19 //Pin setup 20 pinMode(trigger, OUTPUT); 21 pinMode(echo, INPUT); 22 pinMode(led_HC, OUTPUT); 23 pinMode(led_pulsed, OUTPUT); 24 pinMode(led_temperature, OUTPUT); 25 26 digitalWrite(echo, LOW); 27 digitalWrite(trigger, LOW); 28 digitalWrite(led_HC, LOW); 29 digitalWrite(led_pulsed, LOW); 30 digitalWrite(led_temperature, LOW); 31 32 //Analog input 33 analogReference(INTERNAL); // prendo la misura analogica di riferimento di Arduino a 1.1V 34 35 //Variable initialization of the distance 36 duration = 0; 37 distance = 0; 38} 39 40void loop() { 41} 42 43void loop1(3000) { 44 //Send a HIGH pulse to the trigger pin 45 digitalWrite(trigger, HIGH); 46 //I leave it to the HIGH value for 10 microseconds 47 delayMicroseconds(10); 48 //I carry it back to the LOW state 49 digitalWrite(trigger, LOW); 50 51 //I get the number of microseconds for which the echo PIN is left in HIGH state 52 duration = pulseIn(echo, HIGH); 53 54 /*Sound velocity is 340 meters per second, or 29 microseconds per cent. 55 Our impulse travels back and forth, so to calculate the distance 56 Between the sensor and our obstacle we need to do:*/ 57 distance = duration / 29 / 2; 58 59 // Turn on the led if there is an obstacle at a distance of less than 10 cm 60 if (distance < 10) { 61 digitalWrite(led_HC, HIGH); 62 } 63 else { 64 digitalWrite(led_HC, LOW); 65 } 66 // Print on the serial buffer 67 Serial.print("duration : "); 68 Serial.print(duration); 69 Serial.print(" - distance : "); 70 Serial.println(distance); 71} 72 73void loop2(7000) { 74 int i; 75 Serial.println("Pulsed LED"); 76 // Code for the flashing of the led 77 for (i = 0; i < 5; i++) 78 { 79 digitalWrite(led_pulsed, HIGH); 80 delay(500); 81 digitalWrite(led_pulsed, LOW); 82 delay(500); 83 } 84} 85 86void loop3(11000) { 87 float sum_temp = 0; 88 float average_temp = 0; 89 /* Acquire 3 values from the sensor every 500 milliseconds. 90 Through an appropriate conversion I get the measure in degrees Celsius. 91 Calculate the average of subsequent measurements.*/ 92 for (int i = 0; i < 3; i++){ 93 delay(500); 94 temp_C = (1.1 * analogRead(LM35_analogPin) * 100.0) / 1024; 95 sum_temp += temp_C; 96 } 97 average_temp = sum_temp / 3; 98 // If the temperature is greater than 23 degrees, turn on the led 99 if (average_temp > 23) { 100 digitalWrite(led_temperature, HIGH); 101 } 102 else { 103 digitalWrite(led_temperature, LOW); 104 } 105 Serial.print("Temperature : "); 106 Serial.println(average_temp); 107} 108 109 110 111
Multitasking-RealTime-Arduino-System
This simple project shows how Arduino can be used with a real time operating system (Erika). A special version of Arduino called ARTe was used for software development ( http://retis.sssup.it/?q=arte ). ARTe (Arduino Real-Time extension) is an extension to the Arduino framework that supports multitasking and real-time preemptive scheduling. Thanks to ARTe, the user can easily specify and run multiple concurrent loops at differents rates, in addition to the single execution cycle provided by the standard Arduino framework. Today ARTE supports the most popular platforms: Arduino UNO and Arduino DUE. In addition to the single loop present in the standard Arduino approach, the user can easily specify a number of concurrent loops to be executed at specific rates. Concurrency and real-time scheduling is provided by the ERIKA Enterprise (http://erika.tuxfamily.org/drupal/) open-source real-time kernel. Loops can use the standard Arduino libraries, which have been enhanced to guarantee mutual exclusion in the access of shared data structures. The impact of ARTe in terms of footprint and runtime overhead has beed evaluated by extensive tests and resulted to be negligible. In this project was used to run 3 periodic processes that handle different activities. A process for managing every 3 seconds the acquisition of the distance through the sensor HC-SR04. Another process for flashing a LED every 7 seconds. A final process to read the temperature returned by the LM35 sensor expressed in degrees Celsius is executed every 11 seconds. All processes are managed in real time mode.
Multitasking and Real-Time Arduino Processing
arduino
This simple project shows how Arduino can be used with a real time operating system (Erika). A special version of Arduino called ARTe was used for software development ( http://retis.sssup.it/?q=arte ). ARTe (Arduino Real-Time extension) is an extension to the Arduino framework that supports multitasking and real-time preemptive scheduling. Thanks to ARTe, the user can easily specify and run multiple concurrent loops at differents rates, in addition to the single execution cycle provided by the standard Arduino framework. Today ARTE supports the most popular platforms: Arduino UNO and Arduino DUE. In addition to the single loop present in the standard Arduino approach, the user can easily specify a number of concurrent loops to be executed at specific rates. Concurrency and real-time scheduling is provided by the ERIKA Enterprise (http://erika.tuxfamily.org/drupal/) open-source real-time kernel. Loops can use the standard Arduino libraries, which have been enhanced to guarantee mutual exclusion in the access of shared data structures. The impact of ARTe in terms of footprint and runtime overhead has beed evaluated by extensive tests and resulted to be negligible. In this project was used to run 3 periodic processes that handle different activities. A process for managing every 3 seconds the acquisition of the distance through the sensor HC-SR04. Another process for flashing a LED every 7 seconds. A final process to read the temperature returned by the LM35 sensor expressed in degrees Celsius is executed every 11 seconds. All processes are managed in real time mode.
1//Pin definition 2static int trigger = 9; // Pin HC-SR04 3static int 4 echo = 8; // Pin HC-SR04 5static int led_HC = 10; // Pin HC-SR04 6static int 7 led_pulsed = 13; // Timed pulsed led 8static int led_temperature = 12; // Led 9 to temperature overflow 10static int LM35_analogPin = 0; // analog pin for temperature 11 sensor 12 13 14// Variables for calculating the distance with sensor HC-SR04 15long 16 duration; // Flight time of signal 17long distance; // Calculated distance 18float 19 temp_C; // temperature variable (Celsius degrees) 20 21void setup() { 22 //Serial 23 communication 24 Serial.begin(9600); 25 26 //Pin setup 27 pinMode(trigger, 28 OUTPUT); 29 pinMode(echo, INPUT); 30 pinMode(led_HC, OUTPUT); 31 pinMode(led_pulsed, 32 OUTPUT); 33 pinMode(led_temperature, OUTPUT); 34 35 digitalWrite(echo, LOW); 36 37 digitalWrite(trigger, LOW); 38 digitalWrite(led_HC, LOW); 39 digitalWrite(led_pulsed, 40 LOW); 41 digitalWrite(led_temperature, LOW); 42 43 //Analog input 44 analogReference(INTERNAL); 45 // prendo la misura analogica di riferimento di Arduino a 1.1V 46 47 //Variable 48 initialization of the distance 49 duration = 0; 50 distance = 0; 51} 52 53void 54 loop() { 55} 56 57void loop1(3000) { 58 //Send a HIGH pulse to the trigger 59 pin 60 digitalWrite(trigger, HIGH); 61 //I leave it to the HIGH value for 10 62 microseconds 63 delayMicroseconds(10); 64 //I carry it back to the LOW state 65 66 digitalWrite(trigger, LOW); 67 68 //I get the number of microseconds for which 69 the echo PIN is left in HIGH state 70 duration = pulseIn(echo, HIGH); 71 72 73 /*Sound velocity is 340 meters per second, or 29 microseconds per cent. 74 Our 75 impulse travels back and forth, so to calculate the distance 76 Between the sensor 77 and our obstacle we need to do:*/ 78 distance = duration / 29 / 2; 79 80 // 81 Turn on the led if there is an obstacle at a distance of less than 10 cm 82 if 83 (distance < 10) { 84 digitalWrite(led_HC, HIGH); 85 } 86 else { 87 digitalWrite(led_HC, 88 LOW); 89 } 90 // Print on the serial buffer 91 Serial.print("duration : "); 92 93 Serial.print(duration); 94 Serial.print(" - distance : "); 95 Serial.println(distance); 96} 97 98void 99 loop2(7000) { 100 int i; 101 Serial.println("Pulsed LED"); 102 // Code for 103 the flashing of the led 104 for (i = 0; i < 5; i++) 105 { 106 digitalWrite(led_pulsed, 107 HIGH); 108 delay(500); 109 digitalWrite(led_pulsed, LOW); 110 delay(500); 111 112 } 113} 114 115void loop3(11000) { 116 float sum_temp = 0; 117 float average_temp 118 = 0; 119 /* Acquire 3 values from the sensor every 500 milliseconds. 120 Through 121 an appropriate conversion I get the measure in degrees Celsius. 122 Calculate 123 the average of subsequent measurements.*/ 124 for (int i = 0; i < 3; i++){ 125 126 delay(500); 127 temp_C = (1.1 * analogRead(LM35_analogPin) * 100.0) / 1024; 128 129 sum_temp += temp_C; 130 } 131 average_temp = sum_temp / 3; 132 // If the 133 temperature is greater than 23 degrees, turn on the led 134 if (average_temp > 135 23) { 136 digitalWrite(led_temperature, HIGH); 137 } 138 else { 139 digitalWrite(led_temperature, 140 LOW); 141 } 142 Serial.print("Temperature : "); 143 Serial.println(average_temp); 144} 145 146 147 148
Downloadable files
Circuit Diagram
Electronic circuit of the components used. This schema has been designed to work properly with the implemented Arduino code. If the layout of the components is changed, you must change the pin statement in the Arduino code.
Circuit Diagram
Comments
Only logged in users can leave comments