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