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() {
<br>
// initialize the serial communication:
// initialize the serial communication:
<br>
Serial.begin(9600);
Serial.begin(9600);
<br>
}
}
<br>
 
<br>
void loop() {
void loop() {
<br>
// send the value of analog input 0:
// send the value of analog input 0:
<br>
Serial.println(analogRead(0));
Serial.println(analogRead(0));
<br>
// wait a bit for the analog-to-digital converter
// wait a bit for the analog-to-digital converter
<br>
// to stabilize after the last reading:
// to stabilize after the last reading:
<br>
delay(10);
delay(10);
<br>
}
}
<br>
</syntaxhighlight>
<br>
 


===Processing===
===Processing===
<syntaxhighlight lang="CPP">
import processing.serial.*;


import processing.serial.*;
<br>
<br>
Serial myPort;        // The serial port
Serial myPort;        // The serial port
<br>
int xPos = 1;        // horizontal position of the graph
int xPos = 1;        // horizontal position of the graph
<br>
int thresh = 190;
int thresh = 190;
<br>
 
<br>
void setup () {
void setup () {
<br>
// set the window size:
// set the window size:
<br>
size(800, 500);       
size(800, 500);       
<br>
myPort = new Serial(this, "COM4", 9600); // local USB- port
myPort = new Serial(this, "COM4", 9600); // local USB- port
<br>
// don't generate a serialEvent() unless you get a newline character:
// don't generate a serialEvent() unless you get a newline character:
<br>
myPort.bufferUntil('\n');
myPort.bufferUntil('\n');
<br>
background(0);
background(0);
<br>
}
}
<br>
 
<br>
 
void draw () {
void draw () {
<br>
// everything happens in the serialEvent()
// everything happens in the serialEvent()
<br>
}
}
<br>
 
<br>
 
void serialEvent (Serial myPort) {
void serialEvent (Serial myPort) {
<br>
// get the ASCII string:
// get the ASCII string:
<br>
String inString = myPort.readStringUntil('\n');
String inString = myPort.readStringUntil('\n');
<br>
<br>
if (inString != null) {
if (inString != null) {
<br>
// trim off any whitespace:
// trim off any whitespace:
<br>
inString = trim(inString);
inString = trim(inString);
<br>
// convert to an int and map to the screen height:
// convert to an int and map to the screen height:
<br>
float inByte = float(inString);
float inByte = float(inString);
<br>
inByte = map(inByte, 0, 1023, 0, height);
inByte = map(inByte, 0, 1023, 0, height);
<br>
println(inByte);
println(inByte);
<br>
// draw the line:
// draw the line:
<br>
stroke(127,34,255);
stroke(127,34,255);
<br>
line(xPos, height, xPos, height - inByte);
line(xPos, height, xPos, height - inByte);
<br>
 
<br>
line(0, thresh, width, thresh);
line(0, thresh, width, thresh);
<br>
if(inByte > thresh){
if(inByte > thresh){
<br>
fill(255,0,0);
fill(255,0,0);
<br>
ellipse(100,100,50,50);
ellipse(100,100,50,50);
<br>
}else{
}else{
<br>
fill(0,0,0);
fill(0,0,0);
<br>
ellipse(100,100,50,50);
ellipse(100,100,50,50);
<br>
}
}
<br>
 
<br>
// at the edge of the screen, go back to the beginning:
// at the edge of the screen, go back to the beginning:
<br>
if (xPos >= width) {
if (xPos >= width) {
<br>
xPos = 0;
xPos = 0;
<br>
background(0);
background(0);
<br>
}
}
<br>
else {
else {
<br>
// increment the horizontal position:
// increment the horizontal position:
<br>
xPos++;
xPos++;
<br>
}
}
<br>
}
}
<br>
}
}
<br>
</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++;
}
}
}