using threads and potentiometers to control on board LED brightness

Dependencies:   mbed-rtos mbed

Committer:
wray2303
Date:
Wed Dec 09 02:41:04 2015 +0000
Revision:
0:a32d7ad5d9e0
Child:
1:6d23f3636441
first 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
wray2303 0:a32d7ad5d9e0 13 */
wray2303 0:a32d7ad5d9e0 14
wray2303 0:a32d7ad5d9e0 15
wray2303 0:a32d7ad5d9e0 16 #include "mbed.h"
wray2303 0:a32d7ad5d9e0 17 #include "rtos.h" // real time operating system (rtos) library
wray2303 0:a32d7ad5d9e0 18
wray2303 0:a32d7ad5d9e0 19
wray2303 0:a32d7ad5d9e0 20 /**
wray2303 0:a32d7ad5d9e0 21 @namespace Potentiometer
wray2303 0:a32d7ad5d9e0 22 @brief represents potentiometer in value on pin 20
wray2303 0:a32d7ad5d9e0 23 */
wray2303 0:a32d7ad5d9e0 24 AnalogIn Potentiometer(p20);
wray2303 0:a32d7ad5d9e0 25
wray2303 0:a32d7ad5d9e0 26 /**
wray2303 0:a32d7ad5d9e0 27 @namespace Potentiometer1
wray2303 0:a32d7ad5d9e0 28 @brief represents potentiometer in value on pin 19
wray2303 0:a32d7ad5d9e0 29 */
wray2303 0:a32d7ad5d9e0 30 AnalogIn Potentiometer1(p19);
wray2303 0:a32d7ad5d9e0 31
wray2303 0:a32d7ad5d9e0 32 /**
wray2303 0:a32d7ad5d9e0 33 @namespace led
wray2303 0:a32d7ad5d9e0 34 @brief represents on board LED initialised to have brightness controlled with PWM
wray2303 0:a32d7ad5d9e0 35 */
wray2303 0:a32d7ad5d9e0 36 PwmOut led(LED1);
wray2303 0:a32d7ad5d9e0 37
wray2303 0:a32d7ad5d9e0 38 /**
wray2303 0:a32d7ad5d9e0 39 @namespace led1
wray2303 0:a32d7ad5d9e0 40 @brief represents on board LED initialised to have brightness controlled with PWM
wray2303 0:a32d7ad5d9e0 41 */
wray2303 0:a32d7ad5d9e0 42 PwmOut led1(LED4);
wray2303 0:a32d7ad5d9e0 43
wray2303 0:a32d7ad5d9e0 44 /**
wray2303 0:a32d7ad5d9e0 45 *function used as main for created thread.
wray2303 0:a32d7ad5d9e0 46 *controls on board LED with potentiometer
wray2303 0:a32d7ad5d9e0 47 */
wray2303 0:a32d7ad5d9e0 48 void LED2_CONTROL(void const *args) /// This is the thread function, essentially another int main()
wray2303 0:a32d7ad5d9e0 49 {
wray2303 0:a32d7ad5d9e0 50 while(1) {
wray2303 0:a32d7ad5d9e0 51 led = Potentiometer.read();
wray2303 0:a32d7ad5d9e0 52 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 53 /// and the status code is indicating the execution status of the function, so setting this wait also actually sets the
wray2303 0:a32d7ad5d9e0 54 /// execution status, meaning it shows the thread is being executed.
wray2303 0:a32d7ad5d9e0 55 /// the integer x is in MILLI_SECONDS
wray2303 0:a32d7ad5d9e0 56 }
wray2303 0:a32d7ad5d9e0 57
wray2303 0:a32d7ad5d9e0 58 }
wray2303 0:a32d7ad5d9e0 59
wray2303 0:a32d7ad5d9e0 60
wray2303 0:a32d7ad5d9e0 61 int main()
wray2303 0:a32d7ad5d9e0 62 {
wray2303 0:a32d7ad5d9e0 63
wray2303 0:a32d7ad5d9e0 64 Thread thread(LED2_CONTROL); /// set the Thread, give the Thread a name (thread) and assign
wray2303 0:a32d7ad5d9e0 65 /// a function to designate as the seperate thread
wray2303 0:a32d7ad5d9e0 66
wray2303 0:a32d7ad5d9e0 67 while (1) {
wray2303 0:a32d7ad5d9e0 68
wray2303 0:a32d7ad5d9e0 69 led1 = Potentiometer1.read();
wray2303 0:a32d7ad5d9e0 70 Thread::wait(1); /// indicates status of this thread also
wray2303 0:a32d7ad5d9e0 71
wray2303 0:a32d7ad5d9e0 72
wray2303 0:a32d7ad5d9e0 73 }
wray2303 0:a32d7ad5d9e0 74 }
wray2303 0:a32d7ad5d9e0 75