Test of the accelerometer, digital I/O, on-board LCD screen. Looking at vector product of the x-y components of the accelerometer. Works pretty well. Still rough, program wise - sc 140710

Dependencies:   MMA8451Q SLCD mbed

Fork of ACC_LCD_341_MID by Stanley Cohen

Committer:
vbharam
Date:
Mon Sep 29 04:01:28 2014 +0000
Revision:
3:047cf7350900
Test of the accelerometer, digital I/O, on-board LCD screen.; Looking at vector product of the x-y components of the accelerometer.; Works pretty well. Still rough, program wise - sc 140710

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vbharam 3:047cf7350900 1 #include "mbed.h"
vbharam 3:047cf7350900 2 #include "MMA8451Q.h"
vbharam 3:047cf7350900 3 #include "SLCD.h"
vbharam 3:047cf7350900 4
vbharam 3:047cf7350900 5 /*
vbharam 3:047cf7350900 6 Test of the accelerometer, digital I/O, on-board LCD screen.
vbharam 3:047cf7350900 7 Looing at vector product of the x-y components of the accelerometer.
vbharam 3:047cf7350900 8 Works pretty well. Still rough, program wise - sc 140710
vbharam 3:047cf7350900 9
vbharam 3:047cf7350900 10 Author: Vishal Bharam
vbharam 3:047cf7350900 11 Date: 9/29/2014
vbharam 3:047cf7350900 12 */
vbharam 3:047cf7350900 13
vbharam 3:047cf7350900 14 #define DATATIME 0.150
vbharam 3:047cf7350900 15 #define PROGNAME "ACCLCD341VB\r/n"
vbharam 3:047cf7350900 16 #define PRINTDBUG
vbharam 3:047cf7350900 17
vbharam 3:047cf7350900 18
vbharam 3:047cf7350900 19 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
vbharam 3:047cf7350900 20 PinName const SDA = PTE25; // Data pins for the accelerometer/magnetometer.
vbharam 3:047cf7350900 21 PinName const SCL = PTE24; // DO NOT CHANGE
vbharam 3:047cf7350900 22 #elif defined (TARGET_KL05Z)
vbharam 3:047cf7350900 23 PinName const SDA = PTB4;
vbharam 3:047cf7350900 24 PinName const SCL = PTB3;
vbharam 3:047cf7350900 25 #else
vbharam 3:047cf7350900 26 #error TARGET NOT DEFINED
vbharam 3:047cf7350900 27 #endif
vbharam 3:047cf7350900 28
vbharam 3:047cf7350900 29 #define MMA8451_I2C_ADDRESS (0x1d<<1)
vbharam 3:047cf7350900 30
vbharam 3:047cf7350900 31 SLCD slcd; //define LCD display
vbharam 3:047cf7350900 32
vbharam 3:047cf7350900 33 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
vbharam 3:047cf7350900 34 Serial pc(USBTX, USBRX);
vbharam 3:047cf7350900 35
vbharam 3:047cf7350900 36 float sqrt_newt(float argument) {
vbharam 3:047cf7350900 37 return (sqrt(argument));
vbharam 3:047cf7350900 38 }
vbharam 3:047cf7350900 39
vbharam 3:047cf7350900 40
vbharam 3:047cf7350900 41 void LCDMess(char *lMess, float dWait){
vbharam 3:047cf7350900 42 slcd.Home();
vbharam 3:047cf7350900 43 slcd.clear();
vbharam 3:047cf7350900 44 slcd.printf(lMess);
vbharam 3:047cf7350900 45 wait(dWait);
vbharam 3:047cf7350900 46 }
vbharam 3:047cf7350900 47
vbharam 3:047cf7350900 48
vbharam 3:047cf7350900 49 int main() {
vbharam 3:047cf7350900 50 float xAcc;
vbharam 3:047cf7350900 51 float yAcc;
vbharam 3:047cf7350900 52 float vector;
vbharam 3:047cf7350900 53 char lcdData[10]; //buffer needs places dor decimal pt and colon
vbharam 3:047cf7350900 54
vbharam 3:047cf7350900 55 #ifdef PRINTDBUG
vbharam 3:047cf7350900 56 pc.printf(PROGNAME);
vbharam 3:047cf7350900 57 #endif
vbharam 3:047cf7350900 58 // main loop forever
vbharam 3:047cf7350900 59 while(true) {
vbharam 3:047cf7350900 60
vbharam 3:047cf7350900 61 //Get accelerometer data - tilt angles minus offset for zero mark.
vbharam 3:047cf7350900 62 xAcc = abs(acc.getAccX());
vbharam 3:047cf7350900 63 yAcc = abs(acc.getAccY());
vbharam 3:047cf7350900 64 // Calulate vector sum of x and y reading.
vbharam 3:047cf7350900 65 vector = sqrt_newt(pow(xAcc,2) + pow(yAcc,2));
vbharam 3:047cf7350900 66
vbharam 3:047cf7350900 67
vbharam 3:047cf7350900 68 #ifdef PRINTDBUG
vbharam 3:047cf7350900 69 pc.printf("xAcc = %f\r\n", xAcc);
vbharam 3:047cf7350900 70 pc.printf("yAcc = %f\r\n", yAcc);
vbharam 3:047cf7350900 71 pc.printf("vector = %f\r\n", vector);
vbharam 3:047cf7350900 72 #endif
vbharam 3:047cf7350900 73
vbharam 3:047cf7350900 74 sprintf (lcdData,"%4.3f",vector);
vbharam 3:047cf7350900 75 LCDMess(lcdData, DATATIME);
vbharam 3:047cf7350900 76 // Wait then do the whole thing again.
vbharam 3:047cf7350900 77 wait(DATATIME);
vbharam 3:047cf7350900 78 }
vbharam 3:047cf7350900 79 }