![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
-blinking LEDs using push button -motion sensor detection and printing to uart
main.cpp@1:3cded0feb65d, 2018-06-23 (annotated)
- Committer:
- rakeshravula
- Date:
- Sat Jun 23 02:43:09 2018 +0000
- Revision:
- 1:3cded0feb65d
- Parent:
- 0:dc6b1f4adb5a
Updated to show LEDS based on the direction you move the board using in built accelorometer;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rakeshravula | 0:dc6b1f4adb5a | 1 | #include "mbed.h" |
rakeshravula | 0:dc6b1f4adb5a | 2 | #include "LIS3DSH.h" |
rakeshravula | 1:3cded0feb65d | 3 | #include "USBSerial.h" |
rakeshravula | 0:dc6b1f4adb5a | 4 | |
rakeshravula | 0:dc6b1f4adb5a | 5 | DigitalOut myledG(PD_12); |
rakeshravula | 0:dc6b1f4adb5a | 6 | DigitalOut myledO(PD_13); |
rakeshravula | 0:dc6b1f4adb5a | 7 | DigitalOut myledR(PD_14); |
rakeshravula | 0:dc6b1f4adb5a | 8 | DigitalOut myledB(PD_15); |
rakeshravula | 0:dc6b1f4adb5a | 9 | InterruptIn button(PA_0); |
rakeshravula | 1:3cded0feb65d | 10 | USBSerial uart;//(PC_6,PC_7); |
rakeshravula | 0:dc6b1f4adb5a | 11 | LIS3DSH acc(PA_7, PA_6, PA_5, PE_3); |
rakeshravula | 0:dc6b1f4adb5a | 12 | |
rakeshravula | 0:dc6b1f4adb5a | 13 | char buttondetect = 0; |
rakeshravula | 0:dc6b1f4adb5a | 14 | void buttonIsr() |
rakeshravula | 0:dc6b1f4adb5a | 15 | { |
rakeshravula | 0:dc6b1f4adb5a | 16 | buttondetect = 1; |
rakeshravula | 0:dc6b1f4adb5a | 17 | } |
rakeshravula | 0:dc6b1f4adb5a | 18 | int main() { |
rakeshravula | 1:3cded0feb65d | 19 | //uart.baud(9600); |
rakeshravula | 0:dc6b1f4adb5a | 20 | |
rakeshravula | 0:dc6b1f4adb5a | 21 | button.fall(&buttonIsr); |
rakeshravula | 0:dc6b1f4adb5a | 22 | |
rakeshravula | 0:dc6b1f4adb5a | 23 | int16_t X, Y, Z; //signed integer variables for raw X,Y,Z values |
rakeshravula | 0:dc6b1f4adb5a | 24 | float roll, pitch; //float variables for angles |
rakeshravula | 0:dc6b1f4adb5a | 25 | |
rakeshravula | 0:dc6b1f4adb5a | 26 | if(acc.Detect() != 1) { |
rakeshravula | 0:dc6b1f4adb5a | 27 | uart.printf("LIS3DSH Acceleromoter not detected!\n"); |
rakeshravula | 0:dc6b1f4adb5a | 28 | while(1){ }; |
rakeshravula | 0:dc6b1f4adb5a | 29 | } |
rakeshravula | 0:dc6b1f4adb5a | 30 | while(1) { |
rakeshravula | 0:dc6b1f4adb5a | 31 | wait(0.5); |
rakeshravula | 0:dc6b1f4adb5a | 32 | static char counter = 0; |
rakeshravula | 0:dc6b1f4adb5a | 33 | acc.ReadData(&X, &Y, &Z); //read X, Y, Z values |
rakeshravula | 0:dc6b1f4adb5a | 34 | acc.ReadAngles(&roll, &pitch); //read roll and pitch angles |
rakeshravula | 0:dc6b1f4adb5a | 35 | uart.printf("X: %d Y: %d Z: %d\n", X, Y, Z); |
rakeshravula | 0:dc6b1f4adb5a | 36 | uart.printf("Roll: %f Pitch: %f\n", roll, pitch); |
rakeshravula | 1:3cded0feb65d | 37 | if(X > 0 && Y >0) |
rakeshravula | 1:3cded0feb65d | 38 | { |
rakeshravula | 1:3cded0feb65d | 39 | myledO = !myledO; |
rakeshravula | 1:3cded0feb65d | 40 | myledB = 0; |
rakeshravula | 1:3cded0feb65d | 41 | myledR = 0; |
rakeshravula | 1:3cded0feb65d | 42 | myledG = 0; |
rakeshravula | 1:3cded0feb65d | 43 | uart.printf("Yes...entered to Orange Roll: %f Pitch: %f\n", roll, pitch); |
rakeshravula | 1:3cded0feb65d | 44 | |
rakeshravula | 1:3cded0feb65d | 45 | } |
rakeshravula | 1:3cded0feb65d | 46 | |
rakeshravula | 1:3cded0feb65d | 47 | else if(X < 0 && Y < 0) |
rakeshravula | 1:3cded0feb65d | 48 | { |
rakeshravula | 1:3cded0feb65d | 49 | myledB = !myledB; |
rakeshravula | 1:3cded0feb65d | 50 | myledG = 0; |
rakeshravula | 1:3cded0feb65d | 51 | myledR = 0; |
rakeshravula | 1:3cded0feb65d | 52 | myledO = 0; |
rakeshravula | 1:3cded0feb65d | 53 | uart.printf("Yes..entered to blue Roll: %f Pitch: %f\n", roll, pitch); |
rakeshravula | 1:3cded0feb65d | 54 | } |
rakeshravula | 1:3cded0feb65d | 55 | else if(X < 0 && Y > 0) |
rakeshravula | 1:3cded0feb65d | 56 | { |
rakeshravula | 1:3cded0feb65d | 57 | myledG = !myledG; |
rakeshravula | 1:3cded0feb65d | 58 | myledO = 0; |
rakeshravula | 1:3cded0feb65d | 59 | myledR = 0; |
rakeshravula | 1:3cded0feb65d | 60 | myledB = 0; |
rakeshravula | 1:3cded0feb65d | 61 | uart.printf("Yes..entered to Green Roll: %f Pitch: %f\n", roll, pitch); |
rakeshravula | 1:3cded0feb65d | 62 | |
rakeshravula | 1:3cded0feb65d | 63 | } |
rakeshravula | 1:3cded0feb65d | 64 | else if(X >0 && Y < 0) |
rakeshravula | 1:3cded0feb65d | 65 | { |
rakeshravula | 1:3cded0feb65d | 66 | myledR = !myledR; |
rakeshravula | 1:3cded0feb65d | 67 | myledB = 0; |
rakeshravula | 1:3cded0feb65d | 68 | myledO = 0; |
rakeshravula | 1:3cded0feb65d | 69 | myledG = 0; |
rakeshravula | 1:3cded0feb65d | 70 | uart.printf("Yes...entered to Red Roll: %f Pitch: %f\n", roll, pitch); |
rakeshravula | 1:3cded0feb65d | 71 | } |
rakeshravula | 1:3cded0feb65d | 72 | |
rakeshravula | 1:3cded0feb65d | 73 | |
rakeshravula | 1:3cded0feb65d | 74 | |
rakeshravula | 0:dc6b1f4adb5a | 75 | |
rakeshravula | 0:dc6b1f4adb5a | 76 | if(buttondetect){ |
rakeshravula | 0:dc6b1f4adb5a | 77 | buttondetect = 0; |
rakeshravula | 0:dc6b1f4adb5a | 78 | switch(counter){ |
rakeshravula | 0:dc6b1f4adb5a | 79 | case 0: |
rakeshravula | 0:dc6b1f4adb5a | 80 | myledG = !myledG; |
rakeshravula | 0:dc6b1f4adb5a | 81 | break; |
rakeshravula | 0:dc6b1f4adb5a | 82 | case 1: |
rakeshravula | 0:dc6b1f4adb5a | 83 | myledO = !myledO; |
rakeshravula | 0:dc6b1f4adb5a | 84 | break; |
rakeshravula | 0:dc6b1f4adb5a | 85 | case 2: |
rakeshravula | 0:dc6b1f4adb5a | 86 | myledR = !myledR; |
rakeshravula | 0:dc6b1f4adb5a | 87 | break; |
rakeshravula | 0:dc6b1f4adb5a | 88 | case 3: |
rakeshravula | 0:dc6b1f4adb5a | 89 | myledB = !myledB; |
rakeshravula | 0:dc6b1f4adb5a | 90 | break; |
rakeshravula | 0:dc6b1f4adb5a | 91 | } |
rakeshravula | 0:dc6b1f4adb5a | 92 | |
rakeshravula | 0:dc6b1f4adb5a | 93 | counter++; |
rakeshravula | 0:dc6b1f4adb5a | 94 | if(counter >= 4) |
rakeshravula | 0:dc6b1f4adb5a | 95 | counter =0; |
rakeshravula | 0:dc6b1f4adb5a | 96 | |
rakeshravula | 0:dc6b1f4adb5a | 97 | // wait(0.5); |
rakeshravula | 0:dc6b1f4adb5a | 98 | } |
rakeshravula | 0:dc6b1f4adb5a | 99 | } |
rakeshravula | 0:dc6b1f4adb5a | 100 | } |