Examples > MultiThreading


This example shows how to blink LEDs at different rates using multithreading in Energia MT.

Hardware Required

  • MSP432P401R LaunchPad

Relevant groundwork



Only your LaunchPad Board is needed for this example.

Code Explanation

This multithreaded example is based on the basic Analog Input example, but adds multithreading functionality to read analog inputs at multiple rates in separate threads.  Each tab in the sketch is a different thread with its own setup and loop functions.  All the setup and loop functions must have matching name pairs for the ROTS compiler to recognize them as threads.

void setup(), void loop()

void setupAnalog2(), void loopAnalog()

In this example, the first thread reads in analog pin 23, then seizes the serial port (so that no other threads can interrupt the serial output) and prints the value of the sensor to the COM terminal.

In the Tab 1 setup() function pinMode() is used to set pin 23 as an analog input and we open a serial port with the Serial.begin() function.  In the loop() function, the value of analog pin 23 is read and set to the variable sensorValue.  The Serial.seize() function seizes the serial port, which makes sure no other threads write to the serial port until the Serial.release() function is called.  In between those two functions, the value of the analog pin (snsorValue) is printed to the serial terminal.

In Tab 2, we use the same sensorValue variable (which was established as a global in Tab 1) to read the value of the analog input and print it to the serial terminal.



Tab 1

int sensorPin = 23; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
 pinMode(8, INPUT);
void loop() {
 // read the value from the sensor:
 sensorValue = analogRead(sensorPin);
 Serial.print("Moisture Sensor Value: ");

Tab 2

void setupAnalog2() {
void loopAnalog() {
 // read the value from the sensor:
 sensorValue = analogRead(sensorPin);
 Serial.print("Value read by task 2: ");