IFD:PhysicalComp2011/Julia Putscher/Codes for Arduino and Processing - EKG Sensor: Difference between revisions
From Medien Wiki
m (bitte syntaxhighlight für code verwenden!) |
|||
Line 2: | Line 2: | ||
===Arduino=== | ===Arduino=== | ||
<syntaxhighlight lang="CPP"> | |||
void setup() { | void setup() { | ||
// initialize the serial communication: | // initialize the serial communication: | ||
Serial.begin(9600); | Serial.begin(9600); | ||
} | } | ||
void loop() { | void loop() { | ||
// send the value of analog input 0: | // send the value of analog input 0: | ||
Serial.println(analogRead(0)); | Serial.println(analogRead(0)); | ||
// wait a bit for the analog-to-digital converter | // wait a bit for the analog-to-digital converter | ||
// to stabilize after the last reading: | // to stabilize after the last reading: | ||
delay(10); | delay(10); | ||
} | } | ||
< | </syntaxhighlight> | ||
===Processing=== | ===Processing=== | ||
<syntaxhighlight lang="CPP"> | |||
import processing.serial.*; | |||
Serial myPort; // The serial port | Serial myPort; // The serial port | ||
int xPos = 1; // horizontal position of the graph | int xPos = 1; // horizontal position of the graph | ||
int thresh = 190; | int thresh = 190; | ||
void setup () { | void setup () { | ||
// set the window size: | // set the window size: | ||
size(800, 500); | size(800, 500); | ||
myPort = new Serial(this, "COM4", 9600); // local USB- port | myPort = new Serial(this, "COM4", 9600); // local USB- port | ||
// don't generate a serialEvent() unless you get a newline character: | // don't generate a serialEvent() unless you get a newline character: | ||
myPort.bufferUntil('\n'); | myPort.bufferUntil('\n'); | ||
background(0); | background(0); | ||
} | } | ||
void draw () { | void draw () { | ||
// everything happens in the serialEvent() | // everything happens in the serialEvent() | ||
} | } | ||
void serialEvent (Serial myPort) { | void serialEvent (Serial myPort) { | ||
// get the ASCII string: | // get the ASCII string: | ||
String inString = myPort.readStringUntil('\n'); | String inString = myPort.readStringUntil('\n'); | ||
if (inString != null) { | if (inString != null) { | ||
// trim off any whitespace: | // trim off any whitespace: | ||
inString = trim(inString); | inString = trim(inString); | ||
// convert to an int and map to the screen height: | // convert to an int and map to the screen height: | ||
float inByte = float(inString); | float inByte = float(inString); | ||
inByte = map(inByte, 0, 1023, 0, height); | inByte = map(inByte, 0, 1023, 0, height); | ||
println(inByte); | println(inByte); | ||
// draw the line: | // draw the line: | ||
stroke(127,34,255); | stroke(127,34,255); | ||
line(xPos, height, xPos, height - inByte); | line(xPos, height, xPos, height - inByte); | ||
line(0, thresh, width, thresh); | line(0, thresh, width, thresh); | ||
if(inByte > thresh){ | if(inByte > thresh){ | ||
fill(255,0,0); | fill(255,0,0); | ||
ellipse(100,100,50,50); | ellipse(100,100,50,50); | ||
}else{ | }else{ | ||
fill(0,0,0); | fill(0,0,0); | ||
ellipse(100,100,50,50); | ellipse(100,100,50,50); | ||
} | } | ||
// at the edge of the screen, go back to the beginning: | // at the edge of the screen, go back to the beginning: | ||
if (xPos >= width) { | if (xPos >= width) { | ||
xPos = 0; | xPos = 0; | ||
background(0); | background(0); | ||
} | } | ||
else { | else { | ||
// increment the horizontal position: | // increment the horizontal position: | ||
xPos++; | xPos++; | ||
} | } | ||
} | } | ||
} | } | ||
< | </syntaxhighlight> | ||
[[Category:Arduino]] | |||
[[Category:Processing]] |
Latest revision as of 19:24, 1 May 2012
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++;
}
}
}