Mario Quartey's Accelerometer Code

Dependencies:   MMA8452 N5110 PowerControl mbed

Committer:
MrMavriks
Date:
Mon May 11 22:54:16 2015 +0000
Revision:
2:c3e9a24a3ddf
Done

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MrMavriks 2:c3e9a24a3ddf 1 /**
MrMavriks 2:c3e9a24a3ddf 2 @file Accel.cpp
MrMavriks 2:c3e9a24a3ddf 3 @brief Program Implementation
MrMavriks 2:c3e9a24a3ddf 4 */
MrMavriks 2:c3e9a24a3ddf 5
MrMavriks 2:c3e9a24a3ddf 6 #include "Accel.h"
MrMavriks 2:c3e9a24a3ddf 7 #include "mbed.h"
MrMavriks 2:c3e9a24a3ddf 8 #include "N5110.h"
MrMavriks 2:c3e9a24a3ddf 9 #include "MMA8452.h"
MrMavriks 2:c3e9a24a3ddf 10 #include "PowerControl/PowerControl.h"
MrMavriks 2:c3e9a24a3ddf 11 #include "PowerControl/EthernetPowerControl.h"
MrMavriks 2:c3e9a24a3ddf 12
MrMavriks 2:c3e9a24a3ddf 13 /// Buffers for values to be read or calculated
MrMavriks 2:c3e9a24a3ddf 14 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
MrMavriks 2:c3e9a24a3ddf 15 char buffer1[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
MrMavriks 2:c3e9a24a3ddf 16 char buffer2[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
MrMavriks 2:c3e9a24a3ddf 17 char buffer3[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
MrMavriks 2:c3e9a24a3ddf 18
MrMavriks 2:c3e9a24a3ddf 19 /// Gets Readings and Displays them
MrMavriks 2:c3e9a24a3ddf 20 void readings();
MrMavriks 2:c3e9a24a3ddf 21
MrMavriks 2:c3e9a24a3ddf 22 PwmOut myled(p22);
MrMavriks 2:c3e9a24a3ddf 23 PwmOut buzzer(p21);
MrMavriks 2:c3e9a24a3ddf 24 // VCC,SCE,RST,D/C,MOSI,SCLK,LED order of mbed pins should match
MrMavriks 2:c3e9a24a3ddf 25 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
MrMavriks 2:c3e9a24a3ddf 26 MMA8452 mma8452(p28,p27); // SDA, SCL
MrMavriks 2:c3e9a24a3ddf 27 Serial serial(USBTX,USBRX);
MrMavriks 2:c3e9a24a3ddf 28 AnalogIn pot(p20);
MrMavriks 2:c3e9a24a3ddf 29
MrMavriks 2:c3e9a24a3ddf 30 Timeout timer; // create ticker object
MrMavriks 2:c3e9a24a3ddf 31
MrMavriks 2:c3e9a24a3ddf 32 /// sets flag to 0
MrMavriks 2:c3e9a24a3ddf 33 int timerFlag = 0; // flag for timer
MrMavriks 2:c3e9a24a3ddf 34
MrMavriks 2:c3e9a24a3ddf 35 ///Interrupt Service Routine
MrMavriks 2:c3e9a24a3ddf 36 void timerExpired() {
MrMavriks 2:c3e9a24a3ddf 37 timerFlag = 1;
MrMavriks 2:c3e9a24a3ddf 38
MrMavriks 2:c3e9a24a3ddf 39 /// sets the scale for x to be able to reach 1-45 readings per second
MrMavriks 2:c3e9a24a3ddf 40 float x=-(1-pot.read())*44/45+1;
MrMavriks 2:c3e9a24a3ddf 41 timer.detach();
MrMavriks 2:c3e9a24a3ddf 42 timer.attach(&timerExpired,x);
MrMavriks 2:c3e9a24a3ddf 43
MrMavriks 2:c3e9a24a3ddf 44 }
MrMavriks 2:c3e9a24a3ddf 45 ///Turns on LCD AND Accelerometer.
MrMavriks 2:c3e9a24a3ddf 46 void turnon () {
MrMavriks 2:c3e9a24a3ddf 47 lcd.init();
MrMavriks 2:c3e9a24a3ddf 48 lcd.printString("Mario Quartey", 0,0);
MrMavriks 2:c3e9a24a3ddf 49 lcd.printString("Accelerometer", 0,1);
MrMavriks 2:c3e9a24a3ddf 50 lcd.printString("Elec 2645", 0,2);
MrMavriks 2:c3e9a24a3ddf 51 lcd.printString("Dr C Evans!", 0,3);
MrMavriks 2:c3e9a24a3ddf 52 lcd.refresh();
MrMavriks 2:c3e9a24a3ddf 53 wait(2);
MrMavriks 2:c3e9a24a3ddf 54 lcd.clear();
MrMavriks 2:c3e9a24a3ddf 55 mma8452.init(); // 100 Hz update rate, ±4g scale
MrMavriks 2:c3e9a24a3ddf 56 }
MrMavriks 2:c3e9a24a3ddf 57 /// Gets readings and Prints to LCD
MrMavriks 2:c3e9a24a3ddf 58 void readings () {
MrMavriks 2:c3e9a24a3ddf 59 Acceleration acceleration;
MrMavriks 2:c3e9a24a3ddf 60 lcd.printString("X Y Z Values", 0,0);
MrMavriks 2:c3e9a24a3ddf 61 acceleration = mma8452.readValues(); // read current values and print over serial port
MrMavriks 2:c3e9a24a3ddf 62 int xx = sprintf(buffer, "x = %.2f", acceleration.x); // figure eout the length of chars
MrMavriks 2:c3e9a24a3ddf 63 int yy = sprintf(buffer1, "y = %.2f", acceleration.y);
MrMavriks 2:c3e9a24a3ddf 64 int zz = sprintf(buffer2, "z = %.2f", acceleration.z);
MrMavriks 2:c3e9a24a3ddf 65 ///calculates the square root,multiplication and addition of the 3 arguments and return
MrMavriks 2:c3e9a24a3ddf 66 float mag = sqrt((acceleration.x*acceleration.x + acceleration.y*acceleration.y + acceleration.z*acceleration.z));
MrMavriks 2:c3e9a24a3ddf 67 int ff = sprintf(buffer3, "Mag = %.2f", mag);
MrMavriks 2:c3e9a24a3ddf 68 wait(0.1); // short delay until next reading
MrMavriks 2:c3e9a24a3ddf 69 if (xx <= 14){ // if the length is smaller than 14 chars. it prints
MrMavriks 2:c3e9a24a3ddf 70 lcd.printString(buffer, 10, 1);
MrMavriks 2:c3e9a24a3ddf 71 if (yy <= 14){ // if the length is smaller than 14 chars. it prints
MrMavriks 2:c3e9a24a3ddf 72 lcd.printString(buffer1, 10, 2);
MrMavriks 2:c3e9a24a3ddf 73 if (zz <= 14){ // if the length is smaller than 14 chars. it prints
MrMavriks 2:c3e9a24a3ddf 74 lcd.printString(buffer2, 10, 3);
MrMavriks 2:c3e9a24a3ddf 75 if (mag <= 14){ // if the length is smaller than 14 chars. it prints
MrMavriks 2:c3e9a24a3ddf 76 lcd.printString(buffer3, 10, 4);
MrMavriks 2:c3e9a24a3ddf 77
MrMavriks 2:c3e9a24a3ddf 78 /// Compares Values for magnitude and Triggers Buzzer
MrMavriks 2:c3e9a24a3ddf 79 if (mag > 1.5)
MrMavriks 2:c3e9a24a3ddf 80 for(float p = 0.0f; p < 1.0f; p += 0.1f) {
MrMavriks 2:c3e9a24a3ddf 81 buzzer = p;
MrMavriks 2:c3e9a24a3ddf 82 wait (0.3);
MrMavriks 2:c3e9a24a3ddf 83 buzzer = 0;
MrMavriks 2:c3e9a24a3ddf 84 }}}}}}
MrMavriks 2:c3e9a24a3ddf 85
MrMavriks 2:c3e9a24a3ddf 86
MrMavriks 2:c3e9a24a3ddf 87 int main() {
MrMavriks 2:c3e9a24a3ddf 88 turnon();
MrMavriks 2:c3e9a24a3ddf 89
MrMavriks 2:c3e9a24a3ddf 90 timer.attach(&timerExpired,1);
MrMavriks 2:c3e9a24a3ddf 91
MrMavriks 2:c3e9a24a3ddf 92 PHY_PowerDown(); //Powers Down ethernet
MrMavriks 2:c3e9a24a3ddf 93
MrMavriks 2:c3e9a24a3ddf 94 /// Flash LED in Infinite loop, while readings are being taken.
MrMavriks 2:c3e9a24a3ddf 95 while (1) {
MrMavriks 2:c3e9a24a3ddf 96
MrMavriks 2:c3e9a24a3ddf 97 for(float p = 0.0f; p < 1.0f; p += 0.1f) {
MrMavriks 2:c3e9a24a3ddf 98 myled = p;
MrMavriks 2:c3e9a24a3ddf 99 }
MrMavriks 2:c3e9a24a3ddf 100 if (timerFlag) {
MrMavriks 2:c3e9a24a3ddf 101 readings ();
MrMavriks 2:c3e9a24a3ddf 102
MrMavriks 2:c3e9a24a3ddf 103 timerFlag = 0;
MrMavriks 2:c3e9a24a3ddf 104 }
MrMavriks 2:c3e9a24a3ddf 105 lcd.refresh();
MrMavriks 2:c3e9a24a3ddf 106 }
MrMavriks 2:c3e9a24a3ddf 107
MrMavriks 2:c3e9a24a3ddf 108 }