Transmit X axis accelerometer data to PC for graphing

Dependencies:   MMA8451Q mbed

Committer:
bjo3rn
Date:
Wed Sep 17 07:59:48 2014 +0000
Revision:
0:c9bb3c9d5ce8
Initial checkin

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bjo3rn 0:c9bb3c9d5ce8 1 #include "mbed.h"
bjo3rn 0:c9bb3c9d5ce8 2 #include "MMA8451Q.h"
bjo3rn 0:c9bb3c9d5ce8 3
bjo3rn 0:c9bb3c9d5ce8 4 // define I2C Pins and address for KL25Z. Taken from default sample code.
bjo3rn 0:c9bb3c9d5ce8 5 PinName const SDA = PTE25;
bjo3rn 0:c9bb3c9d5ce8 6 PinName const SCL = PTE24;
bjo3rn 0:c9bb3c9d5ce8 7 #define MMA8451_I2C_ADDRESS (0x1d<<1)
bjo3rn 0:c9bb3c9d5ce8 8
bjo3rn 0:c9bb3c9d5ce8 9 //serial connection to PC via USB
bjo3rn 0:c9bb3c9d5ce8 10 Serial pc(USBTX, USBRX);
bjo3rn 0:c9bb3c9d5ce8 11
bjo3rn 0:c9bb3c9d5ce8 12 int main(void)
bjo3rn 0:c9bb3c9d5ce8 13 {
bjo3rn 0:c9bb3c9d5ce8 14 //configure on-board I2C accelerometer on KL25Z
bjo3rn 0:c9bb3c9d5ce8 15 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
bjo3rn 0:c9bb3c9d5ce8 16
bjo3rn 0:c9bb3c9d5ce8 17 float x;
bjo3rn 0:c9bb3c9d5ce8 18
bjo3rn 0:c9bb3c9d5ce8 19 while (true) {
bjo3rn 0:c9bb3c9d5ce8 20 x = acc.getAccX(); //get acceleration
bjo3rn 0:c9bb3c9d5ce8 21 pc.printf("%1.2f\n", x); //print ascii-encoded float to serial port
bjo3rn 0:c9bb3c9d5ce8 22 wait(0.05f); // wait 50ms (20Hz update rate)
bjo3rn 0:c9bb3c9d5ce8 23 }
bjo3rn 0:c9bb3c9d5ce8 24 }
bjo3rn 0:c9bb3c9d5ce8 25
bjo3rn 0:c9bb3c9d5ce8 26 /*
bjo3rn 0:c9bb3c9d5ce8 27 // Graphing sketch for Processing
bjo3rn 0:c9bb3c9d5ce8 28
bjo3rn 0:c9bb3c9d5ce8 29
bjo3rn 0:c9bb3c9d5ce8 30 // This program takes ASCII-encoded strings containing floating point numbers
bjo3rn 0:c9bb3c9d5ce8 31 // from the serial port at 9600 baud and graphs them. It expects values in the
bjo3rn 0:c9bb3c9d5ce8 32 // range -1.0 to 1.0, followed by a newline, or newline and carriage return
bjo3rn 0:c9bb3c9d5ce8 33
bjo3rn 0:c9bb3c9d5ce8 34 // Created 20 Apr 2005
bjo3rn 0:c9bb3c9d5ce8 35 // Updated 18 Jan 2008 by Tom Igoe
bjo3rn 0:c9bb3c9d5ce8 36 // Adapted 16 Sep 2014 by Bjoern Hartmann for mbed
bjo3rn 0:c9bb3c9d5ce8 37 // This example code is in the public domain.
bjo3rn 0:c9bb3c9d5ce8 38
bjo3rn 0:c9bb3c9d5ce8 39 import processing.serial.*;
bjo3rn 0:c9bb3c9d5ce8 40
bjo3rn 0:c9bb3c9d5ce8 41 Serial myPort; // The serial port
bjo3rn 0:c9bb3c9d5ce8 42 int xPos = 1; // horizontal position of the graph
bjo3rn 0:c9bb3c9d5ce8 43
bjo3rn 0:c9bb3c9d5ce8 44 float minVal=-1.0;
bjo3rn 0:c9bb3c9d5ce8 45 float maxVal=1.0;
bjo3rn 0:c9bb3c9d5ce8 46
bjo3rn 0:c9bb3c9d5ce8 47 void setup () {
bjo3rn 0:c9bb3c9d5ce8 48 // set the window size:
bjo3rn 0:c9bb3c9d5ce8 49 size(400, 300);
bjo3rn 0:c9bb3c9d5ce8 50
bjo3rn 0:c9bb3c9d5ce8 51 // List all the available serial ports
bjo3rn 0:c9bb3c9d5ce8 52 println(Serial.list());
bjo3rn 0:c9bb3c9d5ce8 53 // Open whatever port is the one you're using.
bjo3rn 0:c9bb3c9d5ce8 54 myPort = new Serial(this, "/dev/tty.usbmodem1412", 9600);
bjo3rn 0:c9bb3c9d5ce8 55 // don't generate a serialEvent() unless you get a newline character:
bjo3rn 0:c9bb3c9d5ce8 56 myPort.bufferUntil('\n');
bjo3rn 0:c9bb3c9d5ce8 57 // set inital background:
bjo3rn 0:c9bb3c9d5ce8 58 background(0);
bjo3rn 0:c9bb3c9d5ce8 59 }
bjo3rn 0:c9bb3c9d5ce8 60 void draw () {
bjo3rn 0:c9bb3c9d5ce8 61 // everything happens in the serialEvent()
bjo3rn 0:c9bb3c9d5ce8 62 }
bjo3rn 0:c9bb3c9d5ce8 63
bjo3rn 0:c9bb3c9d5ce8 64 void serialEvent (Serial myPort) {
bjo3rn 0:c9bb3c9d5ce8 65 // get the ASCII string:
bjo3rn 0:c9bb3c9d5ce8 66 String inString = myPort.readStringUntil('\n');
bjo3rn 0:c9bb3c9d5ce8 67
bjo3rn 0:c9bb3c9d5ce8 68 if (inString != null) {
bjo3rn 0:c9bb3c9d5ce8 69 // trim off any whitespace:
bjo3rn 0:c9bb3c9d5ce8 70 inString = trim(inString);
bjo3rn 0:c9bb3c9d5ce8 71 // convert to an int and map to the screen height:
bjo3rn 0:c9bb3c9d5ce8 72 float inFloat = float(inString);
bjo3rn 0:c9bb3c9d5ce8 73 float screenY = map(inFloat, minVal, maxVal, 0, height);
bjo3rn 0:c9bb3c9d5ce8 74
bjo3rn 0:c9bb3c9d5ce8 75 // draw the line from bottom of screen to desired height
bjo3rn 0:c9bb3c9d5ce8 76 stroke(127, 34, 255);
bjo3rn 0:c9bb3c9d5ce8 77 line(xPos, height, xPos, height - screenY);
bjo3rn 0:c9bb3c9d5ce8 78
bjo3rn 0:c9bb3c9d5ce8 79 // at the edge of the screen, go back to the beginning:
bjo3rn 0:c9bb3c9d5ce8 80 if (xPos >= width) {
bjo3rn 0:c9bb3c9d5ce8 81 xPos = 0;
bjo3rn 0:c9bb3c9d5ce8 82 background(0);
bjo3rn 0:c9bb3c9d5ce8 83 } else {
bjo3rn 0:c9bb3c9d5ce8 84 // increment the horizontal position:
bjo3rn 0:c9bb3c9d5ce8 85 xPos++;
bjo3rn 0:c9bb3c9d5ce8 86 }
bjo3rn 0:c9bb3c9d5ce8 87 }
bjo3rn 0:c9bb3c9d5ce8 88 }
bjo3rn 0:c9bb3c9d5ce8 89 */