Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Melvin
Published © MIT

Multitasking And Real-Time Arduino System

This simple project shows how Arduino can be used with a RTOS. A special version of Arduino called ARTe was used for software development.

IntermediateProtip33,039
Multitasking And Real-Time Arduino System

Things used in this project

Hardware components

LED (generic)
LED (generic)
×3
Temperature Sensor
Temperature Sensor
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Arduino UNO
Arduino UNO
×1

Software apps and online services

ARTe (Arduino Real-Time extension)

Story

Read more

Schematics

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.

Code

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.
//Pin definition
static int trigger = 9; // Pin HC-SR04
static int echo = 8; // Pin HC-SR04
static int led_HC = 10; // Pin HC-SR04
static int led_pulsed = 13; // Timed pulsed led
static int led_temperature = 12; // Led to temperature overflow
static int LM35_analogPin = 0; // analog pin for temperature sensor


// Variables for calculating the distance with sensor HC-SR04
long duration; // Flight time of signal
long distance;  // Calculated distance 
float temp_C; // temperature variable (Celsius degrees)

void setup() {
  //Serial communication
  Serial.begin(9600);

  //Pin setup
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(led_HC, OUTPUT);
  pinMode(led_pulsed, OUTPUT);
  pinMode(led_temperature, OUTPUT);

  digitalWrite(echo, LOW);
  digitalWrite(trigger, LOW);
  digitalWrite(led_HC, LOW);
  digitalWrite(led_pulsed, LOW);
  digitalWrite(led_temperature, LOW);

  //Analog input
  analogReference(INTERNAL); // prendo la misura analogica di riferimento di Arduino a 1.1V

  //Variable initialization of the distance
  duration = 0;
  distance = 0;
}

void loop() {
}

void loop1(3000) {
  //Send a HIGH pulse to the trigger pin
  digitalWrite(trigger, HIGH);
  //I leave it to the HIGH value for 10 microseconds
  delayMicroseconds(10);
  //I carry it back to the LOW state
  digitalWrite(trigger, LOW);

  //I get the number of microseconds for which the echo PIN is left in HIGH state
  duration = pulseIn(echo, HIGH);

  /*Sound velocity is 340 meters per second, or 29 microseconds per cent.
    Our impulse travels back and forth, so to calculate the distance
   Between the sensor and our obstacle we need to do:*/
  distance = duration / 29 / 2;

  // Turn on the led if there is an obstacle at a distance of less than 10 cm
  if (distance < 10) {
    digitalWrite(led_HC, HIGH);
  }
  else {
    digitalWrite(led_HC, LOW);
  }
  // Print on the serial buffer
  Serial.print("duration : ");
  Serial.print(duration);
  Serial.print(" - distance : ");
  Serial.println(distance);
}

void loop2(7000) {
  int i;
  Serial.println("Pulsed LED");
  // Code for the flashing of the led
  for (i = 0; i < 5; i++)
  {
    digitalWrite(led_pulsed, HIGH);
    delay(500);
    digitalWrite(led_pulsed, LOW);
    delay(500);
  }
}

void loop3(11000) {
  float sum_temp = 0;
  float average_temp = 0;
  /* Acquire 3 values from the sensor every 500 milliseconds.
     Through an appropriate conversion I get the measure in degrees Celsius.
     Calculate the average of subsequent measurements.*/
  for (int i = 0; i < 3; i++){  
    delay(500);
    temp_C = (1.1 * analogRead(LM35_analogPin) * 100.0) / 1024;
    sum_temp += temp_C;
  }
  average_temp = sum_temp / 3;
  // If the temperature is greater than 23 degrees, turn on the led
  if (average_temp > 23) {
    digitalWrite(led_temperature, HIGH);
  }
  else {
    digitalWrite(led_temperature, LOW);
  }
  Serial.print("Temperature : ");
  Serial.println(average_temp);
}

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.

Credits

Melvin
1 project • 2 followers
I am Melvin Mancini, graduated with a Master’s Degree in Computing and Automation Engineering.

Comments