Assessment 4
Dependencies: C12832 LM75B MMA7660 mbed
main.cpp@1:39e65eb5a52d, 2015-11-27 (annotated)
- Committer:
- co657_mh560
- Date:
- Fri Nov 27 18:24:11 2015 +0000
- Revision:
- 1:39e65eb5a52d
- Parent:
- 0:352d3cd4b88c
finished
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
co657_mh560 | 1:39e65eb5a52d | 1 | #include "mbed.h" |
co657_mh560 | 0:352d3cd4b88c | 2 | #include "LM75B.h" /* On sheild Temp sensor */ |
co657_mh560 | 0:352d3cd4b88c | 3 | #include "MMA7660.h" /* On sheild Accelerometer */ |
co657_mh560 | 0:352d3cd4b88c | 4 | #include "C12832.h" /* On sheild LCD */ |
co657_mh560 | 0:352d3cd4b88c | 5 | |
co657_mh560 | 0:352d3cd4b88c | 6 | Serial host (USBTX, USBRX); |
co657_mh560 | 1:39e65eb5a52d | 7 | LM75B temp (D14, D15); |
co657_mh560 | 0:352d3cd4b88c | 8 | C12832 lcd (D11, D13, D12, D7, D10); |
co657_mh560 | 0:352d3cd4b88c | 9 | MMA7660 MMA (D14, D15); |
co657_mh560 | 0:352d3cd4b88c | 10 | PwmOut speaker(D6); |
co657_mh560 | 1:39e65eb5a52d | 11 | InterruptIn sw2_int (PTC6); |
co657_mh560 | 1:39e65eb5a52d | 12 | static volatile int sw2_trig; |
co657_mh560 | 0:352d3cd4b88c | 13 | |
co657_mh560 | 0:352d3cd4b88c | 14 | |
co657_mh560 | 0:352d3cd4b88c | 15 | float tp, x, y, z, i; |
co657_mh560 | 1:39e65eb5a52d | 16 | int mode = 0; |
co657_mh560 | 0:352d3cd4b88c | 17 | |
co657_mh560 | 0:352d3cd4b88c | 18 | /* the fall interrupt for button2 */ |
co657_mh560 | 0:352d3cd4b88c | 19 | void sw2_interrupt (void) |
co657_mh560 | 0:352d3cd4b88c | 20 | { |
co657_mh560 | 0:352d3cd4b88c | 21 | sw2_trig = 1; |
co657_mh560 | 1:39e65eb5a52d | 22 | |
co657_mh560 | 0:352d3cd4b88c | 23 | } |
co657_mh560 | 0:352d3cd4b88c | 24 | |
co657_mh560 | 0:352d3cd4b88c | 25 | /* The rise interrupt for button2 */ |
co657_mh560 | 0:352d3cd4b88c | 26 | void sw2_interruptup (void) |
co657_mh560 | 0:352d3cd4b88c | 27 | { |
co657_mh560 | 1:39e65eb5a52d | 28 | sw2_trig = 0; |
co657_mh560 | 0:352d3cd4b88c | 29 | speaker = 0; |
co657_mh560 | 0:352d3cd4b88c | 30 | } |
co657_mh560 | 0:352d3cd4b88c | 31 | |
co657_mh560 | 1:39e65eb5a52d | 32 | int main () |
co657_mh560 | 0:352d3cd4b88c | 33 | { |
co657_mh560 | 1:39e65eb5a52d | 34 | host.baud(38400); /* Sets the baudrate which is the bits per second */ |
co657_mh560 | 1:39e65eb5a52d | 35 | sw2_trig = 0; |
co657_mh560 | 1:39e65eb5a52d | 36 | |
co657_mh560 | 1:39e65eb5a52d | 37 | |
co657_mh560 | 0:352d3cd4b88c | 38 | sw2_int.rise (&sw2_interruptup); |
co657_mh560 | 1:39e65eb5a52d | 39 | sw2_int.fall (&sw2_interrupt); |
co657_mh560 | 1:39e65eb5a52d | 40 | for(;;) { |
co657_mh560 | 1:39e65eb5a52d | 41 | |
co657_mh560 | 0:352d3cd4b88c | 42 | /* Displays the Temperature */ |
co657_mh560 | 1:39e65eb5a52d | 43 | tp = temp.read (); |
co657_mh560 | 0:352d3cd4b88c | 44 | lcd.cls(); |
co657_mh560 | 0:352d3cd4b88c | 45 | lcd.locate (0, 1); |
co657_mh560 | 1:39e65eb5a52d | 46 | lcd.printf ("Temp: %.3f C", tp); |
co657_mh560 | 1:39e65eb5a52d | 47 | host.printf ("%.3f \r", tp ); |
co657_mh560 | 1:39e65eb5a52d | 48 | wait(0.5); |
co657_mh560 | 1:39e65eb5a52d | 49 | |
co657_mh560 | 1:39e65eb5a52d | 50 | |
co657_mh560 | 1:39e65eb5a52d | 51 | // if temperature rises above certain point then play sound |
co657_mh560 | 1:39e65eb5a52d | 52 | |
co657_mh560 | 1:39e65eb5a52d | 53 | if(tp > 40) { |
co657_mh560 | 1:39e65eb5a52d | 54 | speaker.period(1.0/500.0); |
co657_mh560 | 1:39e65eb5a52d | 55 | speaker=0.5; |
co657_mh560 | 1:39e65eb5a52d | 56 | |
co657_mh560 | 0:352d3cd4b88c | 57 | } |
co657_mh560 | 0:352d3cd4b88c | 58 | // If temperature falls below certain point then play sounds |
co657_mh560 | 1:39e65eb5a52d | 59 | else if ( tp < 15) { |
co657_mh560 | 1:39e65eb5a52d | 60 | speaker.period(1.0/500.0); |
co657_mh560 | 1:39e65eb5a52d | 61 | speaker=0.5; |
co657_mh560 | 1:39e65eb5a52d | 62 | |
co657_mh560 | 0:352d3cd4b88c | 63 | } |
co657_mh560 | 1:39e65eb5a52d | 64 | /* Displays the Accelerometer */ |
co657_mh560 | 1:39e65eb5a52d | 65 | x = MMA.x(); |
co657_mh560 | 1:39e65eb5a52d | 66 | y = MMA.y(); |
co657_mh560 | 1:39e65eb5a52d | 67 | z = MMA.z(); |
co657_mh560 | 1:39e65eb5a52d | 68 | |
co657_mh560 | 1:39e65eb5a52d | 69 | // host.printf("Accel: X: %4f Y: %4f Z: %4f\r\n ", x,y,z); |
co657_mh560 | 1:39e65eb5a52d | 70 | wait(0.5); |
co657_mh560 | 1:39e65eb5a52d | 71 | |
co657_mh560 | 1:39e65eb5a52d | 72 | |
co657_mh560 | 1:39e65eb5a52d | 73 | |
co657_mh560 | 1:39e65eb5a52d | 74 | /* Checks to make sure the acceleromterter is moving still */ |
co657_mh560 | 1:39e65eb5a52d | 75 | |
co657_mh560 | 1:39e65eb5a52d | 76 | if (x -1 < x < x+1 && y -1 < y < y+1 && z -1 < z < z+1) { |
co657_mh560 | 1:39e65eb5a52d | 77 | i++; |
co657_mh560 | 1:39e65eb5a52d | 78 | } |
co657_mh560 | 1:39e65eb5a52d | 79 | if (i == 10) { |
co657_mh560 | 1:39e65eb5a52d | 80 | speaker.period(1.0/500.0); |
co657_mh560 | 1:39e65eb5a52d | 81 | speaker=0.5; |
co657_mh560 | 1:39e65eb5a52d | 82 | } |
co657_mh560 | 1:39e65eb5a52d | 83 | if (sw2_trig) { |
co657_mh560 | 0:352d3cd4b88c | 84 | mode++; |
co657_mh560 | 0:352d3cd4b88c | 85 | sw2_trig = 0; |
co657_mh560 | 0:352d3cd4b88c | 86 | } |
co657_mh560 | 1:39e65eb5a52d | 87 | /* puts the device to sleep */ |
co657_mh560 | 1:39e65eb5a52d | 88 | if (mode == 1) { |
co657_mh560 | 0:352d3cd4b88c | 89 | lcd.cls(); |
co657_mh560 | 0:352d3cd4b88c | 90 | lcd.locate(0,1); |
co657_mh560 | 0:352d3cd4b88c | 91 | lcd.printf("Now going into sleep mode"); |
co657_mh560 | 1:39e65eb5a52d | 92 | |
co657_mh560 | 0:352d3cd4b88c | 93 | sleep(); |
co657_mh560 | 1:39e65eb5a52d | 94 | |
co657_mh560 | 1:39e65eb5a52d | 95 | |
co657_mh560 | 0:352d3cd4b88c | 96 | } |
co657_mh560 | 1:39e65eb5a52d | 97 | |
co657_mh560 | 0:352d3cd4b88c | 98 | /* wakes the device up */ |
co657_mh560 | 1:39e65eb5a52d | 99 | if (mode >= 2) { |
co657_mh560 | 0:352d3cd4b88c | 100 | mode = 0; |
co657_mh560 | 1:39e65eb5a52d | 101 | |
co657_mh560 | 1:39e65eb5a52d | 102 | } |
co657_mh560 | 1:39e65eb5a52d | 103 | |
co657_mh560 | 1:39e65eb5a52d | 104 | |
co657_mh560 | 1:39e65eb5a52d | 105 | } |
co657_mh560 | 1:39e65eb5a52d | 106 | |
co657_mh560 | 1:39e65eb5a52d | 107 | |
co657_mh560 | 1:39e65eb5a52d | 108 | |
co657_mh560 | 0:352d3cd4b88c | 109 | } |
co657_mh560 | 0:352d3cd4b88c | 110 | |
co657_mh560 | 0:352d3cd4b88c | 111 | |
co657_mh560 | 0:352d3cd4b88c | 112 |