Starting point code for CS 220 Lab 5.

Dependencies:   TSI mbed

Fork of FRDM_TSI by mbed official

Committer:
tpkelliher
Date:
Mon Jun 26 16:05:16 2017 +0000
Revision:
6:1096b1fd8d0a
Parent:
1:51b1b688179a
Initial commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 0:0f00f07ebde0 1 #include "mbed.h"
chris 1:51b1b688179a 2 #include "TSISensor.h"
emilmont 0:0f00f07ebde0 3
tpkelliher 6:1096b1fd8d0a 4
tpkelliher 6:1096b1fd8d0a 5 #define LED_OFF 1.0
tpkelliher 6:1096b1fd8d0a 6 // Using pulse width modulation, the closer this value is to 0.0, the
tpkelliher 6:1096b1fd8d0a 7 // brighter the LED will be.
tpkelliher 6:1096b1fd8d0a 8 #define LED_ON 0.9
tpkelliher 6:1096b1fd8d0a 9
tpkelliher 6:1096b1fd8d0a 10
tpkelliher 6:1096b1fd8d0a 11 // Use pulse width modulation to decrease the LED's brightness.
tpkelliher 6:1096b1fd8d0a 12 PwmOut greenLed(LED_GREEN);
tpkelliher 6:1096b1fd8d0a 13 PwmOut redLed(LED_RED);
tpkelliher 6:1096b1fd8d0a 14 PwmOut blueLed(LED_BLUE);
tpkelliher 6:1096b1fd8d0a 15
tpkelliher 6:1096b1fd8d0a 16 // The blue LED is controlled by the button.
tpkelliher 6:1096b1fd8d0a 17 // The green LED is controlled by the KL25Z's touch sensor.
tpkelliher 6:1096b1fd8d0a 18 // The red LED is controlled by the cpuIntensive() function.
tpkelliher 6:1096b1fd8d0a 19 DigitalIn button(PTA1);
tpkelliher 6:1096b1fd8d0a 20 TSISensor tsi;
tpkelliher 6:1096b1fd8d0a 21
tpkelliher 6:1096b1fd8d0a 22 // The serial connection is available, if necessary, for debugging purposes.
tpkelliher 6:1096b1fd8d0a 23 Serial pc(USBTX,USBRX);
tpkelliher 6:1096b1fd8d0a 24
tpkelliher 6:1096b1fd8d0a 25
tpkelliher 6:1096b1fd8d0a 26 // Function prototype.
tpkelliher 6:1096b1fd8d0a 27 void cpuIntensive();
tpkelliher 6:1096b1fd8d0a 28
tpkelliher 6:1096b1fd8d0a 29
tpkelliher 6:1096b1fd8d0a 30 int main(void)
tpkelliher 6:1096b1fd8d0a 31 {
tpkelliher 6:1096b1fd8d0a 32 button.mode(PullUp);
tpkelliher 6:1096b1fd8d0a 33 redLed = greenLed = blueLed = LED_OFF;
tpkelliher 6:1096b1fd8d0a 34
tpkelliher 6:1096b1fd8d0a 35 while (1) {
tpkelliher 6:1096b1fd8d0a 36 cpuIntensive();
tpkelliher 6:1096b1fd8d0a 37
tpkelliher 6:1096b1fd8d0a 38 // Use the touch sensor to scale the green LED's brightness
tpkelliher 6:1096b1fd8d0a 39 // to be between LED_ON and LED_OFF.
tpkelliher 6:1096b1fd8d0a 40 greenLed = LED_ON + (LED_OFF - LED_ON) * (1.0 - tsi.readPercentage());
tpkelliher 6:1096b1fd8d0a 41
tpkelliher 6:1096b1fd8d0a 42 if (button)
tpkelliher 6:1096b1fd8d0a 43 blueLed = LED_OFF;
tpkelliher 6:1096b1fd8d0a 44 else
tpkelliher 6:1096b1fd8d0a 45 blueLed = LED_ON;
emilmont 0:0f00f07ebde0 46 }
tpkelliher 6:1096b1fd8d0a 47 }
tpkelliher 6:1096b1fd8d0a 48
tpkelliher 6:1096b1fd8d0a 49
tpkelliher 6:1096b1fd8d0a 50 // Using the long wait()s, simulate a CPU-intensive process.
tpkelliher 6:1096b1fd8d0a 51 void cpuIntensive()
tpkelliher 6:1096b1fd8d0a 52 {
tpkelliher 6:1096b1fd8d0a 53 redLed = LED_ON;
tpkelliher 6:1096b1fd8d0a 54 wait(2.0);
tpkelliher 6:1096b1fd8d0a 55 redLed = LED_OFF;
tpkelliher 6:1096b1fd8d0a 56 wait(3.0);
chris 1:51b1b688179a 57 }