Read and report XYZ axes from on-board accelerometer for graphing via serial

Dependencies:   MMA8451Q mbed

Committer:
bjo3rn
Date:
Wed Sep 17 08:01:31 2014 +0000
Revision:
0:e89ff0cffbd5
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bjo3rn 0:e89ff0cffbd5 1 #include "mbed.h"
bjo3rn 0:e89ff0cffbd5 2 #include "MMA8451Q.h"
bjo3rn 0:e89ff0cffbd5 3
bjo3rn 0:e89ff0cffbd5 4 // define I2C Pins and address for KL25Z. Taken from default sample code.
bjo3rn 0:e89ff0cffbd5 5 PinName const SDA = PTE25;
bjo3rn 0:e89ff0cffbd5 6 PinName const SCL = PTE24;
bjo3rn 0:e89ff0cffbd5 7 #define MMA8451_I2C_ADDRESS (0x1d<<1)
bjo3rn 0:e89ff0cffbd5 8
bjo3rn 0:e89ff0cffbd5 9 //serial connection to PC via USB
bjo3rn 0:e89ff0cffbd5 10 Serial pc(USBTX, USBRX);
bjo3rn 0:e89ff0cffbd5 11
bjo3rn 0:e89ff0cffbd5 12 int main(void)
bjo3rn 0:e89ff0cffbd5 13 {
bjo3rn 0:e89ff0cffbd5 14 //configure on-board I2C accelerometer on KL25Z
bjo3rn 0:e89ff0cffbd5 15 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
bjo3rn 0:e89ff0cffbd5 16 //map read acceleration to PWM output on red status LED
bjo3rn 0:e89ff0cffbd5 17 PwmOut rled(LED_RED);
bjo3rn 0:e89ff0cffbd5 18 float x,y,z;
bjo3rn 0:e89ff0cffbd5 19 while (true) {
bjo3rn 0:e89ff0cffbd5 20 float x, y, z;
bjo3rn 0:e89ff0cffbd5 21 x = acc.getAccX();
bjo3rn 0:e89ff0cffbd5 22 y = acc.getAccY();
bjo3rn 0:e89ff0cffbd5 23 z = acc.getAccZ();
bjo3rn 0:e89ff0cffbd5 24 wait(0.05f);
bjo3rn 0:e89ff0cffbd5 25 pc.printf("%1.2f %1.2f %1.2f\n", x, y, z);
bjo3rn 0:e89ff0cffbd5 26 }
bjo3rn 0:e89ff0cffbd5 27 }
bjo3rn 0:e89ff0cffbd5 28
bjo3rn 0:e89ff0cffbd5 29
bjo3rn 0:e89ff0cffbd5 30 /* *** PROCESSING SKETCH
bjo3rn 0:e89ff0cffbd5 31 // Three-axis Graphing sketch
bjo3rn 0:e89ff0cffbd5 32
bjo3rn 0:e89ff0cffbd5 33
bjo3rn 0:e89ff0cffbd5 34 // This program takes ASCII-encoded strings containing floating point numbers
bjo3rn 0:e89ff0cffbd5 35 // from the serial port at 9600 baud and graphs them. It expects values in the
bjo3rn 0:e89ff0cffbd5 36 // range -1.0 to 1.0, followed by a newline, or newline and carriage return
bjo3rn 0:e89ff0cffbd5 37
bjo3rn 0:e89ff0cffbd5 38 // Created 20 Apr 2005
bjo3rn 0:e89ff0cffbd5 39 // Updated 18 Jan 2008 by Tom Igoe
bjo3rn 0:e89ff0cffbd5 40 // Adapted 16 Sep 2014 by Bjoern Hartmann for mbed
bjo3rn 0:e89ff0cffbd5 41 // This example code is in the public domain.
bjo3rn 0:e89ff0cffbd5 42
bjo3rn 0:e89ff0cffbd5 43 import processing.serial.*;
bjo3rn 0:e89ff0cffbd5 44 import java.util.Scanner;
bjo3rn 0:e89ff0cffbd5 45
bjo3rn 0:e89ff0cffbd5 46 Serial myPort; // The serial port
bjo3rn 0:e89ff0cffbd5 47 int xPos = 1; // horizontal position of the graph
bjo3rn 0:e89ff0cffbd5 48 float lastX =0.f, lastY=0.f, lastZ=0.f;
bjo3rn 0:e89ff0cffbd5 49 float minVal=-1.0;
bjo3rn 0:e89ff0cffbd5 50 float maxVal=1.0;
bjo3rn 0:e89ff0cffbd5 51
bjo3rn 0:e89ff0cffbd5 52 void setup () {
bjo3rn 0:e89ff0cffbd5 53 // set the window size:
bjo3rn 0:e89ff0cffbd5 54 size(400, 300);
bjo3rn 0:e89ff0cffbd5 55
bjo3rn 0:e89ff0cffbd5 56 // List all the available serial ports
bjo3rn 0:e89ff0cffbd5 57 println(Serial.list());
bjo3rn 0:e89ff0cffbd5 58 // Open whatever port is the one you're using.
bjo3rn 0:e89ff0cffbd5 59 myPort = new Serial(this, "/dev/tty.usbmodem1412", 9600);
bjo3rn 0:e89ff0cffbd5 60 // don't generate a serialEvent() unless you get a newline character:
bjo3rn 0:e89ff0cffbd5 61 myPort.bufferUntil('\n');
bjo3rn 0:e89ff0cffbd5 62 // set inital background:
bjo3rn 0:e89ff0cffbd5 63 background(200);
bjo3rn 0:e89ff0cffbd5 64 }
bjo3rn 0:e89ff0cffbd5 65 void draw () {
bjo3rn 0:e89ff0cffbd5 66 // everything happens in the serialEvent()
bjo3rn 0:e89ff0cffbd5 67 }
bjo3rn 0:e89ff0cffbd5 68
bjo3rn 0:e89ff0cffbd5 69 void serialEvent (Serial myPort) {
bjo3rn 0:e89ff0cffbd5 70 // get the ASCII string:
bjo3rn 0:e89ff0cffbd5 71 String inString = myPort.readStringUntil('\n');
bjo3rn 0:e89ff0cffbd5 72
bjo3rn 0:e89ff0cffbd5 73 if (inString != null) {
bjo3rn 0:e89ff0cffbd5 74 //use the Scanner class to parse 3 floats on a line.
bjo3rn 0:e89ff0cffbd5 75 Scanner scanner = new Scanner(inString);
bjo3rn 0:e89ff0cffbd5 76 float xAcc=0.0,yAcc=0.0,zAcc=0.0;
bjo3rn 0:e89ff0cffbd5 77 try{
bjo3rn 0:e89ff0cffbd5 78 xAcc = scanner.nextFloat();
bjo3rn 0:e89ff0cffbd5 79 yAcc = scanner.nextFloat();
bjo3rn 0:e89ff0cffbd5 80 zAcc = scanner.nextFloat();
bjo3rn 0:e89ff0cffbd5 81
bjo3rn 0:e89ff0cffbd5 82 } catch (Exception e) {
bjo3rn 0:e89ff0cffbd5 83 print(e); //InputMismatchException
bjo3rn 0:e89ff0cffbd5 84 }
bjo3rn 0:e89ff0cffbd5 85
bjo3rn 0:e89ff0cffbd5 86 //draw x line in red
bjo3rn 0:e89ff0cffbd5 87 float xAccScreen = height - map(xAcc, minVal, maxVal, 0, height);
bjo3rn 0:e89ff0cffbd5 88 stroke(255,0,0);
bjo3rn 0:e89ff0cffbd5 89 line(xPos-1,lastX,xPos,xAccScreen);
bjo3rn 0:e89ff0cffbd5 90
bjo3rn 0:e89ff0cffbd5 91 //draw y line in green
bjo3rn 0:e89ff0cffbd5 92 float yAccScreen = height - map(yAcc, minVal, maxVal, 0, height);
bjo3rn 0:e89ff0cffbd5 93 stroke(0,196,0);
bjo3rn 0:e89ff0cffbd5 94 line(xPos-1,lastY,xPos,yAccScreen);
bjo3rn 0:e89ff0cffbd5 95
bjo3rn 0:e89ff0cffbd5 96 //draw z line in blue
bjo3rn 0:e89ff0cffbd5 97 float zAccScreen = height - map(zAcc, minVal, maxVal, 0, height);
bjo3rn 0:e89ff0cffbd5 98 stroke(0,0,255);
bjo3rn 0:e89ff0cffbd5 99 line(xPos-1,lastZ,xPos,zAccScreen);
bjo3rn 0:e89ff0cffbd5 100
bjo3rn 0:e89ff0cffbd5 101 // at the edge of the screen, go back to the beginning:
bjo3rn 0:e89ff0cffbd5 102 if (xPos >= width) {
bjo3rn 0:e89ff0cffbd5 103 xPos = 0;
bjo3rn 0:e89ff0cffbd5 104 background(200);
bjo3rn 0:e89ff0cffbd5 105 } else {
bjo3rn 0:e89ff0cffbd5 106 // increment the horizontal position:
bjo3rn 0:e89ff0cffbd5 107 xPos++;
bjo3rn 0:e89ff0cffbd5 108 }
bjo3rn 0:e89ff0cffbd5 109
bjo3rn 0:e89ff0cffbd5 110 lastX = xAccScreen;
bjo3rn 0:e89ff0cffbd5 111 lastY = yAccScreen;
bjo3rn 0:e89ff0cffbd5 112 lastZ = zAccScreen;
bjo3rn 0:e89ff0cffbd5 113
bjo3rn 0:e89ff0cffbd5 114 }
bjo3rn 0:e89ff0cffbd5 115 }
bjo3rn 0:e89ff0cffbd5 116 */