![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
part3
main.cpp@0:cad38d31219a, 2015-03-03 (annotated)
- Committer:
- jaredwil
- Date:
- Tue Mar 03 19:24:53 2015 +0000
- Revision:
- 0:cad38d31219a
part3;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jaredwil | 0:cad38d31219a | 1 | //Uses the measured z-acceleration to drive leds 2 and 3 of the mbed |
jaredwil | 0:cad38d31219a | 2 | |
jaredwil | 0:cad38d31219a | 3 | #include "mbed.h" |
jaredwil | 0:cad38d31219a | 4 | #include "MMA7660.h" |
jaredwil | 0:cad38d31219a | 5 | |
jaredwil | 0:cad38d31219a | 6 | MMA7660 MMA(p28, p27); |
jaredwil | 0:cad38d31219a | 7 | BusOut leds(LED1,LED2,LED3,LED4); |
jaredwil | 0:cad38d31219a | 8 | |
jaredwil | 0:cad38d31219a | 9 | //used for debugging |
jaredwil | 0:cad38d31219a | 10 | Serial pc(USBTX, USBRX); |
jaredwil | 0:cad38d31219a | 11 | |
jaredwil | 0:cad38d31219a | 12 | int main() { |
jaredwil | 0:cad38d31219a | 13 | if (MMA.testConnection()) |
jaredwil | 0:cad38d31219a | 14 | leds = 0x1; |
jaredwil | 0:cad38d31219a | 15 | wait(1); |
jaredwil | 0:cad38d31219a | 16 | leds = 0x0; |
jaredwil | 0:cad38d31219a | 17 | float x = 0, y = 0, z = 0; |
jaredwil | 0:cad38d31219a | 18 | while(1) { |
jaredwil | 0:cad38d31219a | 19 | leds = 0x0; |
jaredwil | 0:cad38d31219a | 20 | x = MMA.x(); |
jaredwil | 0:cad38d31219a | 21 | y = MMA.y(); |
jaredwil | 0:cad38d31219a | 22 | z = MMA.z(); |
jaredwil | 0:cad38d31219a | 23 | |
jaredwil | 0:cad38d31219a | 24 | //NOTE: All directions based on looking at device with MBED cord towards |
jaredwil | 0:cad38d31219a | 25 | //the user |
jaredwil | 0:cad38d31219a | 26 | if(x > 0.5){ //Forward |
jaredwil | 0:cad38d31219a | 27 | leds = 0x6; |
jaredwil | 0:cad38d31219a | 28 | wait(1); |
jaredwil | 0:cad38d31219a | 29 | } |
jaredwil | 0:cad38d31219a | 30 | if(x < -0.5){ //Backwards |
jaredwil | 0:cad38d31219a | 31 | leds = 0x9; |
jaredwil | 0:cad38d31219a | 32 | wait(1); |
jaredwil | 0:cad38d31219a | 33 | } |
jaredwil | 0:cad38d31219a | 34 | if(y > 0.5){ //LEFT |
jaredwil | 0:cad38d31219a | 35 | leds = 0xE; |
jaredwil | 0:cad38d31219a | 36 | wait(1); |
jaredwil | 0:cad38d31219a | 37 | } |
jaredwil | 0:cad38d31219a | 38 | if(y < -0.5){ //RIGHT |
jaredwil | 0:cad38d31219a | 39 | leds = 0x7; |
jaredwil | 0:cad38d31219a | 40 | wait(1); |
jaredwil | 0:cad38d31219a | 41 | } |
jaredwil | 0:cad38d31219a | 42 | if((z) > 1.2){ //UP |
jaredwil | 0:cad38d31219a | 43 | leds = 0xF; |
jaredwil | 0:cad38d31219a | 44 | wait(1); |
jaredwil | 0:cad38d31219a | 45 | } |
jaredwil | 0:cad38d31219a | 46 | if((z) < 0.5){ //DOWN |
jaredwil | 0:cad38d31219a | 47 | leds = 0xA; |
jaredwil | 0:cad38d31219a | 48 | wait(0.2); |
jaredwil | 0:cad38d31219a | 49 | leds = 0x5; |
jaredwil | 0:cad38d31219a | 50 | wait(0.2); |
jaredwil | 0:cad38d31219a | 51 | leds = 0xA; |
jaredwil | 0:cad38d31219a | 52 | wait(0.2); |
jaredwil | 0:cad38d31219a | 53 | leds = 0x5; |
jaredwil | 0:cad38d31219a | 54 | wait(0.2); |
jaredwil | 0:cad38d31219a | 55 | leds = 0xA; |
jaredwil | 0:cad38d31219a | 56 | wait(0.2); |
jaredwil | 0:cad38d31219a | 57 | leds = 0x5; |
jaredwil | 0:cad38d31219a | 58 | wait(0.2); |
jaredwil | 0:cad38d31219a | 59 | } |
jaredwil | 0:cad38d31219a | 60 | |
jaredwil | 0:cad38d31219a | 61 | |
jaredwil | 0:cad38d31219a | 62 | |
jaredwil | 0:cad38d31219a | 63 | |
jaredwil | 0:cad38d31219a | 64 | //for debug |
jaredwil | 0:cad38d31219a | 65 | //pc.printf("x: %f y: %f z: %f \r",MMA.x(),MMA.y(),MMA.z()); |
jaredwil | 0:cad38d31219a | 66 | |
jaredwil | 0:cad38d31219a | 67 | } |
jaredwil | 0:cad38d31219a | 68 | |
jaredwil | 0:cad38d31219a | 69 | } |