Cycles between 3(4) modes: Thermometer Spirit Level Compass WIP - Etch a Sketch Move the joystick left or right to select a mode
Dependencies: C12832 FXOS8700Q LM75B mbed
main.cpp@3:3b1e081f6655, 2015-10-29 (annotated)
- Committer:
- co657_ts256
- Date:
- Thu Oct 29 23:54:50 2015 +0000
- Revision:
- 3:3b1e081f6655
- Parent:
- 2:9df380c6dd55
Updated description
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
co657_ts256 | 0:f9265a4e99e1 | 1 | #include "mbed.h" |
co657_ts256 | 0:f9265a4e99e1 | 2 | #include "C12832.h" |
co657_ts256 | 0:f9265a4e99e1 | 3 | #include "LM75B.h" |
co657_ts256 | 0:f9265a4e99e1 | 4 | #include "FXOS8700Q.h" |
co657_ts256 | 0:f9265a4e99e1 | 5 | |
co657_ts256 | 0:f9265a4e99e1 | 6 | #ifndef PI |
co657_ts256 | 0:f9265a4e99e1 | 7 | #define PI 3.14159265358979323846 |
co657_ts256 | 0:f9265a4e99e1 | 8 | #endif |
co657_ts256 | 0:f9265a4e99e1 | 9 | |
co657_ts256 | 3:3b1e081f6655 | 10 | // Cycles between 4 modes: |
co657_ts256 | 0:f9265a4e99e1 | 11 | // Thermometer |
co657_ts256 | 0:f9265a4e99e1 | 12 | // Spirit Level |
co657_ts256 | 0:f9265a4e99e1 | 13 | // Compass |
co657_ts256 | 3:3b1e081f6655 | 14 | // Etch a Sketch |
co657_ts256 | 0:f9265a4e99e1 | 15 | // Move the joystick left or right to select a mode |
co657_ts256 | 0:f9265a4e99e1 | 16 | |
co657_ts256 | 0:f9265a4e99e1 | 17 | C12832 shld_lcd (D11, D13, D12, D7, D10); |
co657_ts256 | 0:f9265a4e99e1 | 18 | |
co657_ts256 | 0:f9265a4e99e1 | 19 | LM75B lm_temp (D14, D15); |
co657_ts256 | 0:f9265a4e99e1 | 20 | MotionSensorDataUnits acc_data; |
co657_ts256 | 0:f9265a4e99e1 | 21 | MotionSensorDataUnits mag_data; |
co657_ts256 | 0:f9265a4e99e1 | 22 | FXOS8700Q_acc acc( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board |
co657_ts256 | 0:f9265a4e99e1 | 23 | FXOS8700Q_mag mag( PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // Proper Ports and I2C Address for K64F Freedom board |
co657_ts256 | 0:f9265a4e99e1 | 24 | InterruptIn left(A4); |
co657_ts256 | 1:2cd9904ca6a1 | 25 | InterruptIn right(A5); |
co657_ts256 | 0:f9265a4e99e1 | 26 | |
co657_ts256 | 2:9df380c6dd55 | 27 | AnalogIn pot1(A0); |
co657_ts256 | 2:9df380c6dd55 | 28 | AnalogIn pot2(A1); |
co657_ts256 | 2:9df380c6dd55 | 29 | |
co657_ts256 | 0:f9265a4e99e1 | 30 | DigitalOut red_led(LED1); |
co657_ts256 | 0:f9265a4e99e1 | 31 | DigitalOut blue_led(LED2); |
co657_ts256 | 0:f9265a4e99e1 | 32 | DigitalOut green_led(LED3); |
co657_ts256 | 0:f9265a4e99e1 | 33 | |
co657_ts256 | 0:f9265a4e99e1 | 34 | static volatile int mode = 0; |
co657_ts256 | 0:f9265a4e99e1 | 35 | int modes = 4; |
co657_ts256 | 0:f9265a4e99e1 | 36 | |
co657_ts256 | 0:f9265a4e99e1 | 37 | void left_interrupt(void) { |
co657_ts256 | 0:f9265a4e99e1 | 38 | mode--; |
co657_ts256 | 0:f9265a4e99e1 | 39 | if(mode < 0) { |
co657_ts256 | 0:f9265a4e99e1 | 40 | mode = 0; |
co657_ts256 | 0:f9265a4e99e1 | 41 | } |
co657_ts256 | 0:f9265a4e99e1 | 42 | } |
co657_ts256 | 0:f9265a4e99e1 | 43 | |
co657_ts256 | 0:f9265a4e99e1 | 44 | void right_interrupt(void) { |
co657_ts256 | 0:f9265a4e99e1 | 45 | mode++; |
co657_ts256 | 0:f9265a4e99e1 | 46 | if(mode >= modes) { |
co657_ts256 | 0:f9265a4e99e1 | 47 | mode = modes - 1; |
co657_ts256 | 0:f9265a4e99e1 | 48 | } |
co657_ts256 | 0:f9265a4e99e1 | 49 | } |
co657_ts256 | 0:f9265a4e99e1 | 50 | |
co657_ts256 | 0:f9265a4e99e1 | 51 | int main() |
co657_ts256 | 0:f9265a4e99e1 | 52 | { |
co657_ts256 | 0:f9265a4e99e1 | 53 | float x = 0, y = 0; |
co657_ts256 | 0:f9265a4e99e1 | 54 | left.mode (PullUp); |
co657_ts256 | 0:f9265a4e99e1 | 55 | left.fall (&left_interrupt); |
co657_ts256 | 0:f9265a4e99e1 | 56 | |
co657_ts256 | 0:f9265a4e99e1 | 57 | right.mode (PullUp); |
co657_ts256 | 0:f9265a4e99e1 | 58 | right.fall (&right_interrupt); |
co657_ts256 | 0:f9265a4e99e1 | 59 | acc.enable(); |
co657_ts256 | 0:f9265a4e99e1 | 60 | for(;;) { |
co657_ts256 | 0:f9265a4e99e1 | 61 | if(mode == 0) { |
co657_ts256 | 0:f9265a4e99e1 | 62 | shld_lcd.cls(); |
co657_ts256 | 0:f9265a4e99e1 | 63 | shld_lcd.locate(1, 1); |
co657_ts256 | 0:f9265a4e99e1 | 64 | shld_lcd.printf("Mode: Temperature"); |
co657_ts256 | 0:f9265a4e99e1 | 65 | shld_lcd.locate(1, 22); |
co657_ts256 | 0:f9265a4e99e1 | 66 | int initialT = floor(lm_temp.read()); |
co657_ts256 | 0:f9265a4e99e1 | 67 | int lowerT = initialT - 4; |
co657_ts256 | 0:f9265a4e99e1 | 68 | int upperT = initialT + 4; |
co657_ts256 | 0:f9265a4e99e1 | 69 | shld_lcd.printf("%i", lowerT); |
co657_ts256 | 0:f9265a4e99e1 | 70 | shld_lcd.locate(115, 22); |
co657_ts256 | 0:f9265a4e99e1 | 71 | shld_lcd.printf("%i", upperT); |
co657_ts256 | 0:f9265a4e99e1 | 72 | while(mode == 0) { |
co657_ts256 | 0:f9265a4e99e1 | 73 | wait(0.05); |
co657_ts256 | 0:f9265a4e99e1 | 74 | // Temperature Mode |
co657_ts256 | 0:f9265a4e99e1 | 75 | |
co657_ts256 | 0:f9265a4e99e1 | 76 | // temp range 26 - 30 |
co657_ts256 | 0:f9265a4e99e1 | 77 | float t = lm_temp.read (); |
co657_ts256 | 0:f9265a4e99e1 | 78 | int t_int = floor(t); |
co657_ts256 | 0:f9265a4e99e1 | 79 | if(t < lowerT) { |
co657_ts256 | 0:f9265a4e99e1 | 80 | t = lowerT; |
co657_ts256 | 0:f9265a4e99e1 | 81 | } else if(t > upperT) { |
co657_ts256 | 0:f9265a4e99e1 | 82 | t = upperT; |
co657_ts256 | 0:f9265a4e99e1 | 83 | } |
co657_ts256 | 0:f9265a4e99e1 | 84 | shld_lcd.locate (40, 22); |
co657_ts256 | 0:f9265a4e99e1 | 85 | shld_lcd.printf("Temp: %.2f", t); |
co657_ts256 | 0:f9265a4e99e1 | 86 | for(int i = 0; i <= 128; i ++) { |
co657_ts256 | 0:f9265a4e99e1 | 87 | int r = i / 16 + (lowerT + 0.1); |
co657_ts256 | 0:f9265a4e99e1 | 88 | if(r < t) { |
co657_ts256 | 0:f9265a4e99e1 | 89 | shld_lcd.line (i, 10, i, 20, 1); |
co657_ts256 | 0:f9265a4e99e1 | 90 | } else { |
co657_ts256 | 0:f9265a4e99e1 | 91 | shld_lcd.line (i, 10, i, 20, 0); |
co657_ts256 | 0:f9265a4e99e1 | 92 | } |
co657_ts256 | 0:f9265a4e99e1 | 93 | } |
co657_ts256 | 0:f9265a4e99e1 | 94 | } |
co657_ts256 | 0:f9265a4e99e1 | 95 | } |
co657_ts256 | 0:f9265a4e99e1 | 96 | else if(mode == 1) { |
co657_ts256 | 0:f9265a4e99e1 | 97 | while(mode == 1) { |
co657_ts256 | 0:f9265a4e99e1 | 98 | wait(0.05); |
co657_ts256 | 0:f9265a4e99e1 | 99 | acc.getAxis(acc_data); |
co657_ts256 | 0:f9265a4e99e1 | 100 | shld_lcd.cls(); |
co657_ts256 | 0:f9265a4e99e1 | 101 | shld_lcd.locate(1, 1); |
co657_ts256 | 0:f9265a4e99e1 | 102 | shld_lcd.printf("Mode: Spirit Level"); |
co657_ts256 | 0:f9265a4e99e1 | 103 | x = (x + acc_data.x * 32)/2; |
co657_ts256 | 0:f9265a4e99e1 | 104 | y = (y -(acc_data.y * 16))/2; |
co657_ts256 | 0:f9265a4e99e1 | 105 | shld_lcd.fillcircle(x+63, y+15, 3, 1); //draw bubble |
co657_ts256 | 0:f9265a4e99e1 | 106 | shld_lcd.circle(63, 15, 8, 1); |
co657_ts256 | 0:f9265a4e99e1 | 107 | } |
co657_ts256 | 0:f9265a4e99e1 | 108 | } |
co657_ts256 | 0:f9265a4e99e1 | 109 | else if(mode == 2) { |
co657_ts256 | 0:f9265a4e99e1 | 110 | while(mode == 2) { |
co657_ts256 | 0:f9265a4e99e1 | 111 | wait(0.25); |
co657_ts256 | 0:f9265a4e99e1 | 112 | mag.getAxis(mag_data); |
co657_ts256 | 0:f9265a4e99e1 | 113 | shld_lcd.cls(); |
co657_ts256 | 0:f9265a4e99e1 | 114 | shld_lcd.locate(1, 1); |
co657_ts256 | 0:f9265a4e99e1 | 115 | shld_lcd.printf("Mode: Compass"); |
co657_ts256 | 0:f9265a4e99e1 | 116 | shld_lcd.locate(1, 10); |
co657_ts256 | 0:f9265a4e99e1 | 117 | double heading = atan2(mag_data.y, mag_data.x); |
co657_ts256 | 0:f9265a4e99e1 | 118 | if(heading < 0) |
co657_ts256 | 0:f9265a4e99e1 | 119 | heading += 2*PI; |
co657_ts256 | 0:f9265a4e99e1 | 120 | if(heading > 2*PI) |
co657_ts256 | 0:f9265a4e99e1 | 121 | heading -= 2*PI; |
co657_ts256 | 0:f9265a4e99e1 | 122 | heading = heading * 180 / PI; |
co657_ts256 | 0:f9265a4e99e1 | 123 | shld_lcd.printf("Heading %1.2f", heading); |
co657_ts256 | 0:f9265a4e99e1 | 124 | //shld_lcd.printf("X: %1.2f Y: %1.2f", mag_data.x, mag_data.y); |
co657_ts256 | 0:f9265a4e99e1 | 125 | |
co657_ts256 | 0:f9265a4e99e1 | 126 | } |
co657_ts256 | 0:f9265a4e99e1 | 127 | } |
co657_ts256 | 0:f9265a4e99e1 | 128 | else if(mode == 3) { |
co657_ts256 | 2:9df380c6dd55 | 129 | int p1, p2; |
co657_ts256 | 0:f9265a4e99e1 | 130 | shld_lcd.locate(1,1); |
co657_ts256 | 0:f9265a4e99e1 | 131 | shld_lcd.printf("Mode: Etch-A-Sketch"); |
co657_ts256 | 0:f9265a4e99e1 | 132 | shld_lcd.locate(1,10); |
co657_ts256 | 3:3b1e081f6655 | 133 | shld_lcd.printf("Use potentiometers to draw."); |
co657_ts256 | 0:f9265a4e99e1 | 134 | wait(2); |
co657_ts256 | 2:9df380c6dd55 | 135 | shld_lcd.cls(); |
co657_ts256 | 0:f9265a4e99e1 | 136 | while(mode == 3) { |
co657_ts256 | 2:9df380c6dd55 | 137 | float p1 = (float)pot1; |
co657_ts256 | 2:9df380c6dd55 | 138 | float p2 = (float)pot2; |
co657_ts256 | 2:9df380c6dd55 | 139 | int x = floor((p1 * 127) + 0.5); |
co657_ts256 | 2:9df380c6dd55 | 140 | int y = floor((p2 * 32) + 0.5); |
co657_ts256 | 2:9df380c6dd55 | 141 | shld_lcd.pixel(x, y, 1); |
co657_ts256 | 2:9df380c6dd55 | 142 | shld_lcd.copy_to_lcd(); |
co657_ts256 | 2:9df380c6dd55 | 143 | wait(.001); |
co657_ts256 | 0:f9265a4e99e1 | 144 | } |
co657_ts256 | 0:f9265a4e99e1 | 145 | } |
co657_ts256 | 0:f9265a4e99e1 | 146 | } |
co657_ts256 | 0:f9265a4e99e1 | 147 | } |