IFD:PhysicalComp2011/Julia Putscher/Codes for Arduino and Processing: Difference between revisions

From Medien Wiki
Line 62: Line 62:


import processing.serial.*;
import processing.serial.*;
<br>
<br>
<br>
String buff = "";
String buff = "";
Line 206: Line 207:
<br>
<br>
}
}
<br>
<br>
==Heartrate Sensor - Circuit With Amplifier==
===Arduino===
define mask 255 // kill top bits
<br>
int potPin = 0; // select the input pin for the pot
<br>
int ledPin = 13; // select the pin for the LED
<br>
int val = 16706; // variable to store the value coming from the sensor
<br>
int val2 =0;
<br>
int a =0;
<br>
int b =0;
<br>
int beats[]= {0,0,0,0,0};// to track last five reads for a pattern
<br>
boolean beated = false;
<br>
//function dec
<br>
boolean getBioData();
<br>
<br>
void setup() {
<br>
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
<br>
Serial.begin(9600);
<br>
}
<br>
<br>
void loop() {
<br>
char check=' ';
<br>
val = analogRead(potPin); // read the value from the sensor
<br>
if (Serial.read() =='a'){ // check buffer for an 'a'
<br>
val2 = val;
<br>
b= val & mask;
<br>
a =((val2>>8) & mask); //just in case mask
<br>
delay(20);
<br>
// Serial.print("b"); // debug
<br>
// Serial.print(b);
<br>
Serial.print(a,BYTE);
<br>
Serial.print(b,BYTE);
<br>
if (getBioData()){ // call bio function
<br>
Serial.print('b',BYTE);
<br>
}
<br>
else Serial.print('n',BYTE);
<br>
}
<br>
}
<br>
boolean getBioData(){
<br>
int beatVal = analogRead(potPin); // read the value from the sensor
<br>
beats[4] = beatVal; // put in back of array
<br>
int beatDif = beats[5 - 1] - beats[0];
<br>
for (int i = 0; i < 5;i++){
<br>
beats[i] = beats[i+1]; // push zero out front
<br>
}
<br>
// check for beat
<br>
if ( beatDif > 10 && (beated != true)){
<br>
beated = true;
<br>
return true;
<br>
}
<br>
else if( beatDif < 2 ){
<br>
beated = false;
<br>
return false;
<br>
}
<br>
else return false;
<br>
}
<br>
<br>
===Processing===
import processing.serial.*;
<br>
<br>
Serial port; // Create object from Serial class
<br>
int val; // Data received from the serial port
<br>
int WIDTH=800; // set width
<br>
int number=0;
<br>
int num[] = new int[3];
<br>
int points[]= new int[WIDTH]; // points to be drawn from incoming data
<br>
char beat=' ';
<br>
int beats=0;
<br>
int dropNum[] = new int[4]; // array used to compare data not needed
<br>
<br>
void setup() {
<br>
println(Serial.list());
<br>
size(WIDTH, 700);
<br>
frameRate(30);
<br>
port = new Serial(this,"COM4", 9600); // local USB- port
<br>
}
<br>
<br>
void draw() {
<br>
background(0);// to erase
<br>
port.write('a');
<br>
if (2 < port.available()) { // wait for three bytes
<br>
for (int i=0;i<3;i++){
<br>
num[i] = port.read(); // read them into an array
<br>
}
<br>
//println( num[0]);
<br>
//println( num[1]);
<br>
number = (num[0] << 8)+num[1]; // num range add two incoming bytes together after shifting
<br>
beat = (char) num[2]; // look to see if there is a 'b' to signal a beat
<br>
println(beats);
<br>
}
<br>
stroke(0,255,100);
<br>
if (beat == 'b'){// sent from arduino
<br>
beats++;
<br>
}
<br>
// draw heart beat data
<br>
strokeWeight(1);
<br>
points[(WIDTH/2)] = number; // strat drawing half way accross screen give current reading to array
<br>
//goes through all points and draws a line between consecutive ones
<br>
for (int i=1 ;i< points.length-1; i++){
<br>
points[i]= points[i+1];
<br>
line(i,height-points[i-1]-40,i,height-points[i]-40);
<br>
}
<br>
}
<br>
<br>