Reads fsr to test your strength

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

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