ADXL345 test on L476

Dependencies:   mbed

Revision:
4:d850da8732c1
Parent:
3:618d78c7c53a
diff -r 618d78c7c53a -r d850da8732c1 main.cpp
--- a/main.cpp	Wed Jan 24 14:57:59 2018 +0000
+++ b/main.cpp	Wed Feb 07 16:05:59 2018 +0000
@@ -2,6 +2,9 @@
 #include "adxl345.h"
 #include <string>
 
+#define DEBUG       // it doesnt use modem in debug mode. I dont have it and code will stack on my prototype cuz will never receive answers from modem.
+                    // comment this line to turn off debug mode
+
 // skywire -------------------
 /* --CHANGE THIS FOR YOUR SETUP-- */
 #define DeviceID "yourDeviceIDhere"  //Freeboard DweetIO unique ID
@@ -9,6 +12,9 @@
 string APN = "yourAPNhere";
 // ===========================
 
+PwmOut mypwm(D3);
+Ticker timer1;
+
 ADXL345 adxl;
 
 int x, y, z;
@@ -23,6 +29,9 @@
 volatile char longtitude[15] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
 volatile char coorFlag = 0;
 
+volatile int period = 1000;         // buzzer variables
+volatile int rising = 1;
+
 
 // skywire ------------------------
 DigitalOut skywire_en(PA_6);                        // Skywire Enable
@@ -47,6 +56,9 @@
 
 void ADXL_ISR();
 void rxHandler();
+void data_send();
+void alarm_on();
+void alarm_off();
 
 
 
@@ -65,9 +77,17 @@
   if(adxl.triggered(interrupts, ADXL345_FREE_FALL)){
     pcSerial.printf("*** FREE FALL ***\n");
     
-    led = 1;
-    wait(2);
-    led = 0;
+    alarm_on();     // turn of alarm
+    led = 1;        // turn led on
+    
+    #ifndef DEBUG
+    data_send();        // send data if not in debug mode
+    #else
+    wait(10);      // just wait if in debug
+    #endif
+    
+    alarm_off();    // turn alarm off
+    led = 0;        // turn led off
   } 
 }
 
@@ -221,44 +241,8 @@
     wait(1);
 }
 
-
-// MAIN -=-=-=-=-=-=-=-=-
-//++++++++++++++++++++++++++++++++++++++++++++++++++++
-int main() {
-    
-    skywire.baud(115200);
-    skywire.attach(&Skywire_Rx_interrupt, Serial::RxIrq);
-    
-    i2cAcc.frequency(100000);
-    
-    adxl.powerOn();                     // Power on the ADXL345
-
-    adxl.setRangeSetting(8);           // Give the range settings
-                                      // Accepted values are 2g, 4g, 8g or 16g
-                                      // Higher Values = Wider Measurement Range
-                                      // Lower Values = Greater Sensitivity
- 
-    // Set values for what is considered FREE FALL (0-255)
-    adxl.setFreeFallThreshold(9);       // (5 - 9) recommended - 62.5mg per increment
-    adxl.setFreeFallDuration(20);       // (20 - 70) recommended - 5ms per increment
- 
-    // Setting all interupts to take place on INT1 pin
-    adxl.setImportantInterruptMapping(0, 0, 1, 0, 0);     // Sets "adxl.setEveryInterruptMapping(single tap, double tap, free fall, activity, inactivity);" 
-                                                        // Accepts only 1 or 2 values for pins INT1 and INT2. This chooses the pin on the ADXL345 to use for Interrupts.
-                                                        // This library may have a problem using INT2 pin. Default to INT1 pin.
-  
-    // Turn on Interrupts for each mode (1 == ON, 0 == OFF)
-    adxl.FreeFallINT(1);
-
-    
-    
-    pcSerial.printf("SparkFun ADXL345 Accelerometer Hook Up Guide Example\n");
-    
-    gpsSerial.attach(rxHandler);
-    
-    pcSerial.printf("Starting Demo...\r\n");
-    pcSerial.printf("Waiting for Skywire to Boot...\r\n");
-
+void modem_init()
+{
     //Enable Skywire
     skywire_en=0;
     wait(2);
@@ -296,7 +280,86 @@
     pcSerial.printf("Activating context...\r\n");
     skywire.printf("AT+CGACT=1,3\r\n");
     WaitForResponse("OK", 2);
+}
+
+// interrupt using for encreasing/decreasing tone
+void attime()
+{
     
+    if(rising == 1)
+    {
+        if(period < 1500)
+            period += 19;
+        else
+            rising = 0;
+    }
+    else
+    {
+        if(period > 1000)
+            period -= 19;
+        else
+            rising = 1;
+    }
+    
+    mypwm.period_us(period);
+    mypwm.pulsewidth_us(period/2); 
+}
+
+// turn alarm sound on
+void alarm_on()     
+{
+    timer1.attach(&attime, 0.01);
+}
+
+// turn alarm sound off
+void alarm_off()
+{
+    mypwm.pulsewidth(0);
+    timer1.detach();
+}
+
+
+
+// MAIN -=-=-=-=-=-=-=-=-
+//++++++++++++++++++++++++++++++++++++++++++++++++++++
+int main() {
+    
+    skywire.baud(115200);
+    skywire.attach(&Skywire_Rx_interrupt, Serial::RxIrq);
+    
+    i2cAcc.frequency(100000);
+    
+    adxl.powerOn();                     // Power on the ADXL345
+
+    adxl.setRangeSetting(8);           // Give the range settings
+                                      // Accepted values are 2g, 4g, 8g or 16g
+                                      // Higher Values = Wider Measurement Range
+                                      // Lower Values = Greater Sensitivity
+ 
+    // Set values for what is considered FREE FALL (0-255)
+    adxl.setFreeFallThreshold(9);       // (5 - 9) recommended - 62.5mg per increment
+    adxl.setFreeFallDuration(15);       // (20 - 70) recommended - 5ms per increment
+ 
+    // Setting all interupts to take place on INT1 pin
+    adxl.setImportantInterruptMapping(0, 0, 1, 0, 0);     // Sets "adxl.setEveryInterruptMapping(single tap, double tap, free fall, activity, inactivity);" 
+                                                        // Accepts only 1 or 2 values for pins INT1 and INT2. This chooses the pin on the ADXL345 to use for Interrupts.
+                                                        // This library may have a problem using INT2 pin. Default to INT1 pin.
+  
+    // Turn on Interrupts for each mode (1 == ON, 0 == OFF)
+    adxl.FreeFallINT(1);
+
+    
+    
+    pcSerial.printf("SparkFun ADXL345 Accelerometer Hook Up Guide Example\n");
+    
+    gpsSerial.attach(rxHandler);
+    
+    #ifndef DEBUG       // if not debug mode
+    pcSerial.printf("Starting Demo...\r\n");
+    pcSerial.printf("Waiting for Skywire to Boot...\r\n");
+
+    modem_init();       // init modem
+    #endif
 
     
     while(1) {
@@ -322,7 +385,9 @@
             pcSerial.printf("latitude: %s\n", latitude);
             pcSerial.printf("longtitude: %s\n", longtitude);
             
+            #ifndef DEBUG
             data_send();
+            #endif
                 
             coorFlag = 0;
         }