Mario Quartey's Accelerometer Code

Dependencies:   MMA8452 N5110 PowerControl mbed

Revision:
1:8359e6982b30
Parent:
0:39bc90f27661
--- a/Acceloro.cpp	Mon May 11 10:04:53 2015 +0000
+++ b/Acceloro.cpp	Mon May 11 18:27:20 2015 +0000
@@ -13,64 +13,61 @@
 #include "PowerControl/PowerControl.h"
 #include "PowerControl/EthernetPowerControl.h"
 
-/**
-//@to powerdown the usb
-*/
-
 char buffer[14]; // to store array character takes 6 pixels
 char buffer1[14]; 
 char buffer2[14]; 
 char buffer3[14];
 
+DigitalOut led(LED1);
 
-#define USR_POWERDOWN (0X104)
-int semihost_powerdown()
-{
-    uint32_t arg;
-    return __semihost (USR_POWERDOWN, &arg);
-}
+void readings();
 
-    
-DigitalOut myled(LED1);
+PwmOut myled(p22);
+PwmOut buzzer(p21);
 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
 MMA8452 mma8452(p28,p27);  // SDA, SCL
 Serial serial(USBTX,USBRX);
+AnalogIn pot(p20);
 
-int multiplication (int length, int reg)
-{
-    int m;
-    m=length*reg;
-    return m;
+Timeout timer; // create ticker object
+
+int timerFlag = 0; // flag for timer
+
+//Interrupt Service Routine
+void timerExpired() {
+    timerFlag = 1;
+    //led=!led;
+    
+    float x=-(1-pot.read())*44/45+1;
+    timer.detach();
+    timer.attach(&timerExpired,x);
+    
 }
 
 
-int main() {
-/**
-@Powers Down Ethernet
-@Powers Down USB interface
+
 
-*/
-PHY_PowerDown();
-semihost_powerdown();
-/**
-@Initialises lcd and accelerometer
-*/
-   //  lcd.initSPI();
+void turnon () {  //Turns on LCD AND Accelerometer.
      lcd.init();
-     lcd.printString("Hello World!", 0,0);
+     lcd.printString("Mario Quartey", 0,0);
+     lcd.printString("Accelerometer", 0,1);
+     lcd.printString("Elec 2645", 0,2);
+     lcd.printString("Dr C Evans!", 0,3);
      lcd.refresh();
-     wait(1);
+     wait(2);
      lcd.clear();
      mma8452.init();  // 100 Hz update rate, ±4g scale
-
-  Acceleration acceleration;  // Accleration structure declared in MMA8452 class
-    while (1) {
-        lcd.printString("X Y Z Values", 0,0);
+     }
+void readings () {
+     Acceleration acceleration; 
+     lcd.printString("X Y Z Values", 0,0);
         acceleration = mma8452.readValues();   // read current values and print over serial port
          int xx = sprintf(buffer, "x = %.2f", acceleration.x); // figure eout the length of chars
          int yy =  sprintf(buffer1, "y = %.2f", acceleration.y);
          int zz = sprintf(buffer2, "z = %.2f", acceleration.z);
-        serial.printf("x = %.2f g y = %.2f g z = %.2f g\n",acceleration.x,acceleration.y,acceleration.z);
+          float mag = sqrt((acceleration.x*acceleration.x + acceleration.y*acceleration.y + acceleration.z*acceleration.z));
+          int ff = sprintf(buffer3, "Mag = %.2f", mag);
+        //serial.printf("x = %.2f g y = %.2f g z = %.2f g\n",acceleration.x,acceleration.y,acceleration.z);
         wait(0.1);   // short delay until next reading
         if (xx <= 14){  // if the length is smaller than 14 chars. it prints
             lcd.printString(buffer, 10, 1);
@@ -78,16 +75,36 @@
             lcd.printString(buffer1, 10, 2);
         if (zz <= 14){  // if the length is smaller than 14 chars. it prints
             lcd.printString(buffer2, 10, 3);
+        if (mag <= 14){  // if the length is smaller than 14 chars. it prints
+            lcd.printString(buffer3, 10, 4);   
+            if (mag > 1.5)
+            for(float p = 0.0f; p < 1.0f; p += 0.1f) {
+            buzzer = p;
+            wait (0.3);
+            buzzer = 0;
+        }}}}}}  
+        
+
+int main() {
+   turnon();
+    
+    timer.attach(&timerExpired,1);
+
+PHY_PowerDown(); //Powers Down ethernet
+
+    while (1) {
+       
+        for(float p = 0.0f; p < 1.0f; p += 0.1f) {
+            myled = p;
+
+             }
+       
+         if (timerFlag) {
+            readings ();
             
-          float mag = sqrt((acceleration.x*acceleration.x + acceleration.y*acceleration.y + acceleration.z*acceleration.z));
-          int ff = sprintf(buffer3, "Mag = %.2f", mag);
-          if (mag <= 14){  // if the length is smaller than 14 chars. it prints
-            lcd.printString(buffer3, 10, 4);
-     }
+            timerFlag = 0;
             }
-   } 
+       lcd.refresh();
         }
-        
-        lcd.refresh();
-    }
-}
+            
+     }