-blinking LEDs using push button -motion sensor detection and printing to uart

Dependencies:   LIS3DSH mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "LIS3DSH.h"
00003 #include "USBSerial.h"
00004 
00005 DigitalOut myledG(PD_12);
00006 DigitalOut myledO(PD_13);
00007 DigitalOut myledR(PD_14);
00008 DigitalOut myledB(PD_15);
00009 InterruptIn button(PA_0);
00010 USBSerial uart;//(PC_6,PC_7);
00011  LIS3DSH acc(PA_7, PA_6, PA_5, PE_3);
00012  
00013 char buttondetect = 0;
00014 void buttonIsr()
00015 {
00016     buttondetect = 1;
00017     }
00018 int main() {
00019    //uart.baud(9600);
00020   
00021    button.fall(&buttonIsr); 
00022    
00023    int16_t X, Y, Z;    //signed integer variables for raw X,Y,Z values
00024     float roll, pitch;  //float variables for angles
00025     
00026     if(acc.Detect() != 1) {
00027         uart.printf("LIS3DSH Acceleromoter not detected!\n");
00028         while(1){ };
00029     }
00030     while(1) {
00031         wait(0.5);
00032          static char counter = 0;
00033    acc.ReadData(&X, &Y, &Z);           //read X, Y, Z values
00034         acc.ReadAngles(&roll, &pitch);      //read roll and pitch angles
00035         uart.printf("X: %d  Y: %d  Z: %d\n", X, Y, Z);
00036         uart.printf("Roll: %f   Pitch: %f\n", roll, pitch);
00037         if(X > 0 && Y >0)
00038                     {
00039                         myledO = !myledO;
00040                         myledB = 0;
00041                         myledR = 0;
00042                         myledG = 0;
00043                         uart.printf("Yes...entered to Orange Roll: %f   Pitch: %f\n", roll, pitch);
00044                         
00045                         }
00046     
00047             else if(X < 0 && Y < 0)
00048             {
00049                 myledB = !myledB;
00050                 myledG = 0;
00051                         myledR = 0;
00052                         myledO = 0;
00053                  uart.printf("Yes..entered to blue Roll: %f   Pitch: %f\n", roll, pitch);
00054                 }
00055                 else if(X < 0 && Y > 0)
00056                 {
00057                     myledG = !myledG;
00058                     myledO = 0;
00059                         myledR = 0;
00060                         myledB = 0;
00061                     uart.printf("Yes..entered to Green Roll: %f   Pitch: %f\n", roll, pitch);
00062                     
00063                     }
00064                 else if(X >0 && Y < 0)
00065                 {
00066                     myledR = !myledR;
00067                     myledB = 0;
00068                         myledO = 0;
00069                         myledG = 0;
00070                     uart.printf("Yes...entered to Red Roll: %f   Pitch: %f\n", roll, pitch);
00071                     }
00072                     
00073                 
00074         
00075    
00076         if(buttondetect){
00077             buttondetect = 0;
00078         switch(counter){
00079             case 0:
00080             myledG = !myledG;
00081              break;
00082              case 1:
00083              myledO = !myledO;
00084              break;
00085              case 2:
00086              myledR = !myledR;
00087              break;
00088              case 3:
00089              myledB = !myledB;
00090              break;
00091          }   
00092          
00093              counter++;
00094              if(counter >= 4)
00095              counter =0;
00096              
00097       //  wait(0.5);
00098         }
00099     }
00100 }