Part2

Dependencies:   MMA8451Q SLCD mbed

Fork of ACC_LCD_341_MID by Stanley Cohen

Committer:
scohennm
Date:
Thu Sep 25 23:45:35 2014 +0000
Revision:
2:6003ed409def
Parent:
1:9340a340e588
Child:
3:bc69bca92c05
Update for Newton SQRT

Who changed what in which revision?

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