using threads and potentiometers to control on board LED brightness

Dependencies:   mbed-rtos mbed

Committer:
wray2303
Date:
Wed Dec 09 02:47:18 2015 +0000
Revision:
1:6d23f3636441
Parent:
0:a32d7ad5d9e0
2nd revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wray2303 0:a32d7ad5d9e0 1 /**
wray2303 0:a32d7ad5d9e0 2 @file main.cpp Thread_use7
wray2303 0:a32d7ad5d9e0 3
wray2303 0:a32d7ad5d9e0 4 @brief Code which uses two potentiometers to control the brightness of on board LED's, at the same time, using threads.
wray2303 0:a32d7ad5d9e0 5
wray2303 0:a32d7ad5d9e0 6 @Authour Louis Wray
wray2303 0:a32d7ad5d9e0 7
wray2303 0:a32d7ad5d9e0 8 @Date 8th December 2015
wray2303 0:a32d7ad5d9e0 9
wray2303 0:a32d7ad5d9e0 10 Think of threads as "paths" except your code can walk down more than one path a time a time
wray2303 0:a32d7ad5d9e0 11 */
wray2303 0:a32d7ad5d9e0 12 #include "mbed.h"
wray2303 0:a32d7ad5d9e0 13 #include "rtos.h" // real time operating system (rtos) library
wray2303 0:a32d7ad5d9e0 14
wray2303 0:a32d7ad5d9e0 15
wray2303 0:a32d7ad5d9e0 16 /**
wray2303 0:a32d7ad5d9e0 17 @namespace Potentiometer
wray2303 0:a32d7ad5d9e0 18 @brief represents potentiometer in value on pin 20
wray2303 0:a32d7ad5d9e0 19 */
wray2303 0:a32d7ad5d9e0 20 AnalogIn Potentiometer(p20);
wray2303 0:a32d7ad5d9e0 21
wray2303 0:a32d7ad5d9e0 22 /**
wray2303 0:a32d7ad5d9e0 23 @namespace Potentiometer1
wray2303 0:a32d7ad5d9e0 24 @brief represents potentiometer in value on pin 19
wray2303 0:a32d7ad5d9e0 25 */
wray2303 0:a32d7ad5d9e0 26 AnalogIn Potentiometer1(p19);
wray2303 0:a32d7ad5d9e0 27
wray2303 0:a32d7ad5d9e0 28 /**
wray2303 0:a32d7ad5d9e0 29 @namespace led
wray2303 0:a32d7ad5d9e0 30 @brief represents on board LED initialised to have brightness controlled with PWM
wray2303 0:a32d7ad5d9e0 31 */
wray2303 0:a32d7ad5d9e0 32 PwmOut led(LED1);
wray2303 0:a32d7ad5d9e0 33
wray2303 0:a32d7ad5d9e0 34 /**
wray2303 0:a32d7ad5d9e0 35 @namespace led1
wray2303 0:a32d7ad5d9e0 36 @brief represents on board LED initialised to have brightness controlled with PWM
wray2303 0:a32d7ad5d9e0 37 */
wray2303 0:a32d7ad5d9e0 38 PwmOut led1(LED4);
wray2303 0:a32d7ad5d9e0 39
wray2303 0:a32d7ad5d9e0 40 /**
wray2303 1:6d23f3636441 41 function used as main for created thread.
wray2303 1:6d23f3636441 42 controls on board LED with potentiometer
wray2303 0:a32d7ad5d9e0 43 */
wray2303 0:a32d7ad5d9e0 44 void LED2_CONTROL(void const *args) /// This is the thread function, essentially another int main()
wray2303 0:a32d7ad5d9e0 45 {
wray2303 0:a32d7ad5d9e0 46 while(1) {
wray2303 0:a32d7ad5d9e0 47 led = Potentiometer.read();
wray2303 0:a32d7ad5d9e0 48 Thread::wait(1); /// Thread::wait(x) must be inclued and x must be a real integer greater than 0, this is because it is a "status code"
wray2303 0:a32d7ad5d9e0 49 /// and the status code is indicating the execution status of the function, so setting this wait also actually sets the
wray2303 0:a32d7ad5d9e0 50 /// execution status, meaning it shows the thread is being executed.
wray2303 0:a32d7ad5d9e0 51 /// the integer x is in MILLI_SECONDS
wray2303 0:a32d7ad5d9e0 52 }
wray2303 0:a32d7ad5d9e0 53
wray2303 0:a32d7ad5d9e0 54 }
wray2303 0:a32d7ad5d9e0 55
wray2303 0:a32d7ad5d9e0 56
wray2303 0:a32d7ad5d9e0 57 int main()
wray2303 0:a32d7ad5d9e0 58 {
wray2303 0:a32d7ad5d9e0 59
wray2303 0:a32d7ad5d9e0 60 Thread thread(LED2_CONTROL); /// set the Thread, give the Thread a name (thread) and assign
wray2303 0:a32d7ad5d9e0 61 /// a function to designate as the seperate thread
wray2303 0:a32d7ad5d9e0 62
wray2303 0:a32d7ad5d9e0 63 while (1) {
wray2303 0:a32d7ad5d9e0 64
wray2303 0:a32d7ad5d9e0 65 led1 = Potentiometer1.read();
wray2303 0:a32d7ad5d9e0 66 Thread::wait(1); /// indicates status of this thread also
wray2303 0:a32d7ad5d9e0 67
wray2303 0:a32d7ad5d9e0 68
wray2303 0:a32d7ad5d9e0 69 }
wray2303 0:a32d7ad5d9e0 70 }
wray2303 0:a32d7ad5d9e0 71