Interactive Device Design
/
idd_graph_sensor
Transmit X axis accelerometer data to PC for graphing
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "MMA8451Q.h" 00003 00004 // define I2C Pins and address for KL25Z. Taken from default sample code. 00005 PinName const SDA = PTE25; 00006 PinName const SCL = PTE24; 00007 #define MMA8451_I2C_ADDRESS (0x1d<<1) 00008 00009 //serial connection to PC via USB 00010 Serial pc(USBTX, USBRX); 00011 00012 int main(void) 00013 { 00014 //configure on-board I2C accelerometer on KL25Z 00015 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); 00016 00017 float x; 00018 00019 while (true) { 00020 x = acc.getAccX(); //get acceleration 00021 pc.printf("%1.2f\n", x); //print ascii-encoded float to serial port 00022 wait(0.05f); // wait 50ms (20Hz update rate) 00023 } 00024 } 00025 00026 /* 00027 // Graphing sketch for Processing 00028 00029 00030 // This program takes ASCII-encoded strings containing floating point numbers 00031 // from the serial port at 9600 baud and graphs them. It expects values in the 00032 // range -1.0 to 1.0, followed by a newline, or newline and carriage return 00033 00034 // Created 20 Apr 2005 00035 // Updated 18 Jan 2008 by Tom Igoe 00036 // Adapted 16 Sep 2014 by Bjoern Hartmann for mbed 00037 // This example code is in the public domain. 00038 00039 import processing.serial.*; 00040 00041 Serial myPort; // The serial port 00042 int xPos = 1; // horizontal position of the graph 00043 00044 float minVal=-1.0; 00045 float maxVal=1.0; 00046 00047 void setup () { 00048 // set the window size: 00049 size(400, 300); 00050 00051 // List all the available serial ports 00052 println(Serial.list()); 00053 // Open whatever port is the one you're using. 00054 myPort = new Serial(this, "/dev/tty.usbmodem1412", 9600); 00055 // don't generate a serialEvent() unless you get a newline character: 00056 myPort.bufferUntil('\n'); 00057 // set inital background: 00058 background(0); 00059 } 00060 void draw () { 00061 // everything happens in the serialEvent() 00062 } 00063 00064 void serialEvent (Serial myPort) { 00065 // get the ASCII string: 00066 String inString = myPort.readStringUntil('\n'); 00067 00068 if (inString != null) { 00069 // trim off any whitespace: 00070 inString = trim(inString); 00071 // convert to an int and map to the screen height: 00072 float inFloat = float(inString); 00073 float screenY = map(inFloat, minVal, maxVal, 0, height); 00074 00075 // draw the line from bottom of screen to desired height 00076 stroke(127, 34, 255); 00077 line(xPos, height, xPos, height - screenY); 00078 00079 // at the edge of the screen, go back to the beginning: 00080 if (xPos >= width) { 00081 xPos = 0; 00082 background(0); 00083 } else { 00084 // increment the horizontal position: 00085 xPos++; 00086 } 00087 } 00088 } 00089 */
Generated on Wed Jul 20 2022 06:43:26 by 1.7.2