Reads fsr to test your strength

Dependencies:   mbed

Committer:
jdefond
Date:
Fri Sep 26 20:46:27 2014 +0000
Revision:
0:c939a1a4e267
For group

Who changed what in which revision?

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