Solution to CS 220 Lab 5.

Dependencies:   TSI mbed

Fork of FRDM_TSI by mbed official

Committer:
tpkelliher
Date:
Mon Jun 26 16:06:57 2017 +0000
Revision:
6:47f9dc2e7bdb
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:47f9dc2e7bdb 4
tpkelliher 6:47f9dc2e7bdb 5 #define LED_OFF 1.0
tpkelliher 6:47f9dc2e7bdb 6 // Using pulse width modulation, the closer this value is to 0.0, the
tpkelliher 6:47f9dc2e7bdb 7 // brighter the LED will be.
tpkelliher 6:47f9dc2e7bdb 8 #define LED_ON 0.9
tpkelliher 6:47f9dc2e7bdb 9
tpkelliher 6:47f9dc2e7bdb 10
tpkelliher 6:47f9dc2e7bdb 11 // Use pulse width modulation to decrease the LED's brightness.
tpkelliher 6:47f9dc2e7bdb 12 PwmOut greenLed(LED_GREEN);
tpkelliher 6:47f9dc2e7bdb 13 PwmOut redLed(LED_RED);
tpkelliher 6:47f9dc2e7bdb 14 PwmOut blueLed(LED_BLUE);
tpkelliher 6:47f9dc2e7bdb 15
tpkelliher 6:47f9dc2e7bdb 16 // The blue LED is controlled by the button.
tpkelliher 6:47f9dc2e7bdb 17 // The green LED is controlled by the KL25Z's touch sensor.
tpkelliher 6:47f9dc2e7bdb 18 // The red LED is controlled by the cpuIntensive() function.
tpkelliher 6:47f9dc2e7bdb 19 InterruptIn button(PTA1);
tpkelliher 6:47f9dc2e7bdb 20 TSISensor tsi;
tpkelliher 6:47f9dc2e7bdb 21 Ticker flipper;
tpkelliher 6:47f9dc2e7bdb 22
tpkelliher 6:47f9dc2e7bdb 23 // The serial connection is available, if necessary, for debugging purposes.
tpkelliher 6:47f9dc2e7bdb 24 Serial pc(USBTX,USBRX);
tpkelliher 6:47f9dc2e7bdb 25
tpkelliher 6:47f9dc2e7bdb 26
tpkelliher 6:47f9dc2e7bdb 27 // Function prototype.
tpkelliher 6:47f9dc2e7bdb 28 void cpuIntensive();
tpkelliher 6:47f9dc2e7bdb 29
tpkelliher 6:47f9dc2e7bdb 30
tpkelliher 6:47f9dc2e7bdb 31 void flip()
tpkelliher 6:47f9dc2e7bdb 32 {
tpkelliher 6:47f9dc2e7bdb 33 greenLed = LED_ON + (LED_OFF - LED_ON) * (1.0 - tsi.readPercentage());
tpkelliher 6:47f9dc2e7bdb 34 }
tpkelliher 6:47f9dc2e7bdb 35
tpkelliher 6:47f9dc2e7bdb 36
tpkelliher 6:47f9dc2e7bdb 37 void rise()
tpkelliher 6:47f9dc2e7bdb 38 {
tpkelliher 6:47f9dc2e7bdb 39 blueLed = LED_OFF;
tpkelliher 6:47f9dc2e7bdb 40 }
tpkelliher 6:47f9dc2e7bdb 41
tpkelliher 6:47f9dc2e7bdb 42
tpkelliher 6:47f9dc2e7bdb 43 void fall()
tpkelliher 6:47f9dc2e7bdb 44 {
tpkelliher 6:47f9dc2e7bdb 45 blueLed = LED_ON;
tpkelliher 6:47f9dc2e7bdb 46 }
tpkelliher 6:47f9dc2e7bdb 47
tpkelliher 6:47f9dc2e7bdb 48
tpkelliher 6:47f9dc2e7bdb 49 int main(void)
tpkelliher 6:47f9dc2e7bdb 50 {
tpkelliher 6:47f9dc2e7bdb 51 button.mode(PullUp);
tpkelliher 6:47f9dc2e7bdb 52 flipper.attach(flip, 0.1);
tpkelliher 6:47f9dc2e7bdb 53 button.rise(rise);
tpkelliher 6:47f9dc2e7bdb 54 button.fall(fall);
tpkelliher 6:47f9dc2e7bdb 55 redLed = greenLed = blueLed = LED_OFF;
tpkelliher 6:47f9dc2e7bdb 56
tpkelliher 6:47f9dc2e7bdb 57 while (1) {
tpkelliher 6:47f9dc2e7bdb 58 cpuIntensive();
emilmont 0:0f00f07ebde0 59 }
tpkelliher 6:47f9dc2e7bdb 60 }
tpkelliher 6:47f9dc2e7bdb 61
tpkelliher 6:47f9dc2e7bdb 62
tpkelliher 6:47f9dc2e7bdb 63 // Using the long wait()s, simulate a CPU-intensive process.
tpkelliher 6:47f9dc2e7bdb 64 void cpuIntensive()
tpkelliher 6:47f9dc2e7bdb 65 {
tpkelliher 6:47f9dc2e7bdb 66 redLed = LED_ON;
tpkelliher 6:47f9dc2e7bdb 67 wait(2.0);
tpkelliher 6:47f9dc2e7bdb 68 redLed = LED_OFF;
tpkelliher 6:47f9dc2e7bdb 69 wait(3.0);
chris 1:51b1b688179a 70 }