Flow control with three threats Blinking LEDs in the main and a thread for the led. Reading analog input with 12 bits and calculating moving average in a thread for the potentiometer connect: D10 - 330 Ohm - LED - GND D11 - 330 Ohm - LED - GND GND - Pot 1 A3 - Pot 2 Vcc - Pot 3 Tested with Nucleo-L432KC but should work with almost any board Thanks to "mbed-os-Flow-Control_Thread" example and AnalogIn API Reference Timo Karppinen 25.6.2020, Apache 2.0

Flow control with three threats Blinking LEDs in the main and a thread for the led. Reading analog input with 12 bits and calculating moving average in a thread for the potentiometer connect: D10 - 330 Ohm - LED - GND D11 - 330 Ohm - LED - GND GND - Pot 1 A3 - Pot 2 Vcc - Pot 3

Tested with Nucleo-L432KC but should work with almost any board

Thanks to "mbed-os-Flow-Control_Thread" example and AnalogIn API Reference Timo Karppinen 25.6.2020, Apache 2.0

Committer:
timo_k2
Date:
Thu Jun 25 15:51:09 2020 +0000
Revision:
0:156098cfb039
Flow control with three threats. Blinking LEDs in the main and a thread for the led. Reading analog input with 12 bits and calculating moving average in a thread for the potentiometer.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
timo_k2 0:156098cfb039 1 /* Flow control with three threats
timo_k2 0:156098cfb039 2 * Blinking LEDs in the main and a thread for the led.
timo_k2 0:156098cfb039 3 * Reading analog input with 12 bits and calculating moving average in a thread for the potentiometer
timo_k2 0:156098cfb039 4 * connect:
timo_k2 0:156098cfb039 5 * D10 - 330 Ohm - LED - GND
timo_k2 0:156098cfb039 6 * D11 - 330 Ohm - LED - GND
timo_k2 0:156098cfb039 7 * GND - Pot 1
timo_k2 0:156098cfb039 8 * A3 - Pot 2
timo_k2 0:156098cfb039 9 * Vcc - Pot 3
timo_k2 0:156098cfb039 10 *
timo_k2 0:156098cfb039 11 * Tested with Nucleo-L432KC but should work with almost any board
timo_k2 0:156098cfb039 12 *
timo_k2 0:156098cfb039 13 * Thanks to "mbed-os-Flow-Control_Thread" example and AnalogIn API Reference
timo_k2 0:156098cfb039 14 * Timo Karppinen 25.6.2020, Apache 2.0
timo_k2 0:156098cfb039 15 */
timo_k2 0:156098cfb039 16
timo_k2 0:156098cfb039 17 #include "mbed.h"
timo_k2 0:156098cfb039 18
timo_k2 0:156098cfb039 19 DigitalOut led1(D10);
timo_k2 0:156098cfb039 20 DigitalOut led2(D11);
timo_k2 0:156098cfb039 21 AnalogIn pot1(A3);
timo_k2 0:156098cfb039 22 #define numSamples 1024
timo_k2 0:156098cfb039 23
timo_k2 0:156098cfb039 24 Thread threadL;
timo_k2 0:156098cfb039 25 Thread threadP;
timo_k2 0:156098cfb039 26
timo_k2 0:156098cfb039 27 // Thread for the LED2. Sleeps for 1 second.
timo_k2 0:156098cfb039 28 void led2_thread()
timo_k2 0:156098cfb039 29 {
timo_k2 0:156098cfb039 30 while (true) {
timo_k2 0:156098cfb039 31 led2 = !led2;
timo_k2 0:156098cfb039 32 printf("Led2 blink %d \n", led2.read());
timo_k2 0:156098cfb039 33
timo_k2 0:156098cfb039 34 ThisThread::sleep_for(1000);
timo_k2 0:156098cfb039 35 }
timo_k2 0:156098cfb039 36 }
timo_k2 0:156098cfb039 37
timo_k2 0:156098cfb039 38
timo_k2 0:156098cfb039 39 //Thread for the Pot. Sleeps for one millisecond in the middle of processing.
timo_k2 0:156098cfb039 40 void pot1_thread()
timo_k2 0:156098cfb039 41 {
timo_k2 0:156098cfb039 42 while(true) {
timo_k2 0:156098cfb039 43 uint16_t pot1Samples[numSamples]; //unsigned short in the range [0x0, 0xFFFF], [0, 65535]
timo_k2 0:156098cfb039 44 int sum;
timo_k2 0:156098cfb039 45 int result;
timo_k2 0:156098cfb039 46
timo_k2 0:156098cfb039 47 for (int i = 0; i < numSamples; i++) {
timo_k2 0:156098cfb039 48 pot1Samples[i] = pot1.read_u16()/16; // normalized into range [0x0, 0xFFFF] and to 12 bit [0x0, 0xFFF]
timo_k2 0:156098cfb039 49 //printf("Pot1 sample %d \n", pot1Samples[i]); // prints every sample
timo_k2 0:156098cfb039 50 ThisThread::sleep_for(1);
timo_k2 0:156098cfb039 51 }
timo_k2 0:156098cfb039 52 printf("Results: "); // always the average of last samples
timo_k2 0:156098cfb039 53 sum = 0;
timo_k2 0:156098cfb039 54 for (int i = 0; i < numSamples; i++) {
timo_k2 0:156098cfb039 55 sum = sum + pot1Samples[i];
timo_k2 0:156098cfb039 56 }
timo_k2 0:156098cfb039 57 result = sum/numSamples;
timo_k2 0:156098cfb039 58 printf("%d \n", result);
timo_k2 0:156098cfb039 59 }
timo_k2 0:156098cfb039 60 }
timo_k2 0:156098cfb039 61
timo_k2 0:156098cfb039 62 // The main() is in it's own thread.
timo_k2 0:156098cfb039 63 int main()
timo_k2 0:156098cfb039 64 {
timo_k2 0:156098cfb039 65 // Create a thread to execute the function led2_thread
timo_k2 0:156098cfb039 66 threadL.start(led2_thread);
timo_k2 0:156098cfb039 67 // led2_thread is executing concurrently with the main and other threads
timo_k2 0:156098cfb039 68
timo_k2 0:156098cfb039 69
timo_k2 0:156098cfb039 70 // Create a thread to execute the function pot1_thread
timo_k2 0:156098cfb039 71 threadP.start(pot1_thread);
timo_k2 0:156098cfb039 72 // pot1_thread is executing concurrently with the main and other threads
timo_k2 0:156098cfb039 73
timo_k2 0:156098cfb039 74
timo_k2 0:156098cfb039 75 while (true) {
timo_k2 0:156098cfb039 76 led1 = !led1;
timo_k2 0:156098cfb039 77 printf("Led1 blink %d \n", led1.read());
timo_k2 0:156098cfb039 78 ThisThread::sleep_for(500);
timo_k2 0:156098cfb039 79 }
timo_k2 0:156098cfb039 80 }