IFD:PhysicalComp2011/Julia Putscher/Codes for Arduino and Processing - EKG Sensor

From Medien Wiki

Codes For The EKG Sensor

Arduino

void setup() {
// initialize the serial communication:
Serial.begin(9600);
}

void loop() {
// send the value of analog input 0:
Serial.println(analogRead(0));
// wait a bit for the analog-to-digital converter
// to stabilize after the last reading:
delay(10);
}


Processing

import processing.serial.*;

Serial myPort;        // The serial port
int xPos = 1;         // horizontal position of the graph
int thresh = 190;

void setup () {
// set the window size:
size(800, 500);       
myPort = new Serial(this, "COM4", 9600); // local USB- port
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
background(0);
}


void draw () {
// everything happens in the serialEvent()
}


void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
println(inByte);
// draw the line:
stroke(127,34,255);
line(xPos, height, xPos, height - inByte);

line(0, thresh, width, thresh);
if(inByte > thresh){
fill(255,0,0);
ellipse(100,100,50,50);
}else{
fill(0,0,0);
ellipse(100,100,50,50);
}

// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
}
}