Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MMA8451Q8 SLCD mbed
Fork of SSD_341_535_finalv3 by
Diff: lcd_acc_46_v3.cpp
- Revision:
- 0:88ddbd76b455
- Child:
- 1:6c2ec7b0e1c9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lcd_acc_46_v3.cpp Sun Nov 30 00:08:36 2014 +0000
@@ -0,0 +1,174 @@
+#include "mbed.h"
+#include "MMA8451Q.h"
+#include "SLCD.h"
+/*
+Test of the accelerometer, digital I/O, on-board LCD screen, and 16-bit ADC.
+ Looing at vector product of the x-y components of the accelerometer.
+ Works pretty well. Still rough, program wise - sc 140710
+ Addiing touch sensor to replace potentiometer.
+ Show x and y components
+ */
+
+#define BLINKTIME 0.5
+#define DATATIME 0.1
+#define NUMLEDS 2
+#define LEDON 0
+#define LEDOFF 1
+#define SCALING 13
+#define RSTARTMESS "RSET"
+#define MAXVECT "MAXV"
+#define YCOMP "YCMP"
+#define ANALTOVOLTS 3.3
+#define LEDDELAY 0.400
+
+#define PROGNAME "LCD_ACC_LCDv3 46\r/n"
+
+//#define PRINTDBUG
+// Accelerometer SPI pins
+#if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
+ PinName const SDA = PTE25;
+ PinName const SCL = PTE24;
+#elif defined (TARGET_KL05Z)
+ PinName const SDA = PTB4;
+ PinName const SCL = PTB3;
+#else
+ #error TARGET NOT DEFINED
+#endif
+
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+
+SLCD slcd; //define LCD display
+Timer blinkTimer;
+Timer dataTimer;
+Timer displayTimer;
+MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
+AnalogIn TMP01(PTB1);
+DigitalOut LEDs[NUMLEDS]={LED_RED, LED_GREEN}; //Indicator LEDs
+Serial pc(USBTX, USBRX);
+
+
+void LCDMess(char *lMess, float dWait){
+ slcd.Home();
+ slcd.clear();
+ slcd.printf(lMess);
+ wait(dWait);
+}
+void LCDMessNoDwell(char *lMess){
+ slcd.Home();
+ slcd.clear();
+ slcd.printf(lMess);
+}
+void GetOffsets(float *xo, float *yo){
+ LCDMess(RSTARTMESS, BLINKTIME);
+ *xo = acc.getAccX();
+ *yo = acc.getAccY();
+ return;
+}
+
+
+void allLEDsOFF(int numberOfLEDS) {
+ int i;
+ for (i=0;i<numberOfLEDS; i++){
+ LEDs[i] = LEDOFF;
+ }
+
+}
+
+void runLEDs(int numberOfLEDS) {
+ int i;
+ for (i=0;i<numberOfLEDS; i++){
+ LEDs[i] = LEDON;
+ wait(0.2);
+ }
+ allLEDsOFF(numberOfLEDS);
+}
+
+
+
+int main() {
+
+ int RButtonState;
+ int LButtonState;
+ DigitalIn RtButton(PTC12);
+ DigitalIn LftButton(PTC3);
+ float xOffset;
+ float yOffset;
+ int displayState = true;
+ float xAcc;
+ float yAcc;
+ float zAcc;
+ float vector;
+ float vMax = 0.0;
+ int DisplayTime = DATATIME;
+ int outState = false;
+ char lcdData[10]; //buffer needs places dor decimal pt and colon
+
+
+#ifdef PRINTDBUG
+ pc.printf(PROGNAME);
+#endif
+// Initialze readings and sequence the LED's for dramtic effect.
+ allLEDsOFF(NUMLEDS);
+ GetOffsets(&xOffset, &yOffset);
+// runLEDs(NUMLEDS);
+ blinkTimer.start();
+ blinkTimer.reset();
+
+// main loop forever
+ while(true) {
+// Alive LEDs
+ while(blinkTimer.read()> LEDDELAY) {
+ LEDs[0].write(outState);
+ LEDs[1].write(!outState);
+ outState = !outState;
+ blinkTimer.reset();
+ }
+// Handle user input for display selections
+ RButtonState = !RtButton; // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream
+ if (RButtonState){
+ vMax = 0.0; // Clear vMax
+ GetOffsets(&xOffset, &yOffset); // set zero baseling for position
+#ifdef PRINTDBUG
+ pc.printf("xOffset = %f\r\n", xOffset);
+ pc.printf("yOffset = %f\r\n", yOffset);
+#endif
+ }
+ LButtonState = !LftButton;
+ if (LButtonState) { //Change data that is displayed
+ displayState = !displayState;
+ // Change to switch/case soon.
+ if (displayState) {
+ LCDMess(YCOMP,BLINKTIME);
+ }else {
+ LCDMess(MAXVECT,BLINKTIME);
+ }
+ }
+ //while (dataTimer.read() > DATATIME){
+//Get accelerometer data - tilt angles minus offset for zero mark.
+// No offset
+ xAcc = abs(acc.getAccX());
+ yAcc = abs(acc.getAccY());
+ zAcc = abs(acc.getAccZ());
+ // Calulate vector sum of x,y and z reading.
+ vector = sqrt(pow(xAcc,2) + pow(yAcc,2)+ pow(zAcc,2));
+ if (vector > vMax) {
+ vMax = vector;
+ }
+ wait(DATATIME);
+#ifdef PRINTDBUG
+ pc.printf("vector = %f\r\n", yAcc);
+ pc.printf("scaling = %f\r\n", scaleExpansion);
+ pc.printf("RawTemp = %f\r\n", FDeg);
+#endif
+// Display the appropriate data on the LCD based upon what mode was chosen
+
+ if (displayState) {
+ sprintf (lcdData,"%4.3f",yAcc);
+ }else {
+ sprintf (lcdData,"%4.3f",vMax);
+ }
+ DisplayTime = BLINKTIME;
+ LCDMess(lcdData,DisplayTime);
+// Wait then do the whole thing again.
+ }//forever loop
+}// main
\ No newline at end of file
