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

From Medien Wiki
mNo edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Heartrate Sensor - Simple Circuit==
===Arduino===
===Arduino===


<syntaxhighlight lang="CPP">
int an1,an2 = 0;
int an1,an2 = 0;
<br>
int redLedPin =13;
int redLedPin =13;
<br>
boolean triggered = false;
boolean triggered = false;
void setup(){
void setup(){
<br>
Serial.begin(9600);
Serial.begin(9600);
<br>
pinMode(redLedPin, OUTPUT); // set the red LED pin to be an output
pinMode(redLedPin, OUTPUT); // set the red LED pin to be an output
<br>
// Serial.println("Starting");
// Serial.println("Starting");
<br>
}
}
<br>
 
<br>
void loop(){
void loop(){
<br>
// read analog value in
// read analog value in
<br>
int an2 = analogRead(0);
int an2 = analogRead(0);
<br>
Serial.print("Y");
Serial.print("Y");
<br>
Serial.println(an2,DEC);
Serial.println(an2,DEC);
//threshold
//threshold
<br>
int an1= analogRead(5);
int an1= analogRead(5);
<br>
Serial.print("X");
Serial.print("X");
<br>
Serial.println(an1,DEC);
Serial.println(an1,DEC);
<br>
<br>


if(an1 > an2 && !triggered){
if(an1 > an2 && !triggered){
<br>
triggered = true;
triggered = true;
<br>
digitalWrite(redLedPin, HIGH); // turn off the red LED
digitalWrite(redLedPin, HIGH); // turn off the red LED
<br>
}
}
<br>
 
if(an1 <= an2 && triggered){
if(an1 <= an2 && triggered){
<br>
triggered = false;
triggered = false;
<br>
digitalWrite(redLedPin, LOW); // turn off the red LED
digitalWrite(redLedPin, LOW); // turn off the red LED
<br>
}
}
<br>
}
}
<br>
</syntaxhighlight>
<br>
 
===Processing===
===Processing===


import processing.serial.*;
import processing.serial.*;
<br>
 
 
String buff = "";
String buff = "";
<br>
 
int val = 0;
int val = 0;
<br>
 
int NEWLINE = 10;
int NEWLINE = 10;
<br>
 
int xPos,yPos,zPos = 0;
int xPos,yPos,zPos = 0;
<br>
 
int displaySize = 2;
int displaySize = 2;
<br>
 
int an1, an2, an3;
int an1, an2, an3;
//an1 pot; an2 ir;
//an1 pot; an2 ir;
<br>
 
Serial port;
Serial port;
<br>
 
<br>
 
void setup(){
void setup(){
<br>
 
background(80);
background(80);
<br>
 
size(800,600);
size(800,600);
<br>
 
smooth();
smooth();
<br>
 
<br>
 
port = new Serial(this,"COM4", 9600); //local USB- port
port = new Serial(this,"COM4", 9600); //local USB- port
<br>
 
}
}
<br>
 
<br>
 
void draw(){
void draw(){
<br>
 
// new background over old
// new background over old
<br>
 
fill(80,5);
fill(80,5);
<br>
 
noStroke();
noStroke();
<br>
 
rect(0,0,width,height);
rect(0,0,width,height);
<br>
 
// wipe out a small area in front of the new data
// wipe out a small area in front of the new data
<br>
 
fill(80);<br>
fill(80);
rect(xPos+displaySize,0,50,height);
rect(xPos+displaySize,0,50,height);
<br>
 
// check for serial, and process
// check for serial, and process
<br>
 
while (port.available() > 0) {
while (port.available() > 0) {
<br>
 
serialEvent(port.read());
serialEvent(port.read());
<br>
 
}
}
<br>
 
}
}
<br>
 
<br>
 
void serialEvent(int serial) {
void serialEvent(int serial) {
<br>
 
print("A"); //header variable, so we know which sensor value is which
print("A"); //header variable, so we know which sensor value is which
<br>
 
println(an1); //send as a ascii encoded number - we'll turn it back into a number at the other end
println(an1); //send as a ascii encoded number - we'll turn it back into a number at the other end
<br>
 
//Serial.print(10, BYTE); //terminating character
//Serial.print(10, BYTE); //terminating character
<br>
 
print("B"); //header variable, so we know which sensor value is which
print("B"); //header variable, so we know which sensor value is which
<br>
 
println(an2); //send as a ascii encoded number - we'll turn it back into a number at the other end
println(an2); //send as a ascii encoded number - we'll turn it back into a number at the other end
<br>
 
//Serial.print(10, BYTE); //terminating character
//Serial.print(10, BYTE); //terminating character
<br>
 
<br>
 
if(serial != '\n') {
if(serial != '\n') {
<br>
 
buff += char(serial);
buff += char(serial);
<br>
 
}
}
<br>
 
else {
else {
<br>
 
int curX = buff.indexOf("X");
int curX = buff.indexOf("X");
<br>
 
int curY = buff.indexOf("Y");
int curY = buff.indexOf("Y");
<br>
 
<br>
 
if(curX >=0){
if(curX >=0){
<br>
 
String val = buff.substring(curX+1);
String val = buff.substring(curX+1);
<br>
 
an1 = Integer.parseInt(val.trim());
an1 = Integer.parseInt(val.trim());
<br>
 
xPos++;
xPos++;
<br>
 
<br>
 
if(xPos > width) xPos = 0;
if(xPos > width) xPos = 0;
<br>
 
sensorTic1(xPos,an1);
sensorTic1(xPos,an1);
<br>
 
}
}
<br>
 
<br>
 
if(curY >=0){
if(curY >=0){
<br>
 
String val = buff.substring(curY+1);
String val = buff.substring(curY+1);
<br>
 
an2 = Integer.parseInt(val.trim());
an2 = Integer.parseInt(val.trim());
<br>
 
yPos++;
yPos++;
<br>
 
<br>
 
if(yPos > width) yPos = 0;
if(yPos > width) yPos = 0;
<br>
 
sensorTic2(yPos,an2);
sensorTic2(yPos,an2);
<br>
 
}
}
<br>
 
// Clear the value of "buff"
// Clear the value of "buff"
<br>
 
buff = "";<br>
buff = "";
}
}
<br>
 
}
}
<br>
 
<br>
 
void sensorTic1(int x, int y){
void sensorTic1(int x, int y){
<br>
 
stroke(0,0,255);
stroke(0,0,255);
<br>
 
fill(0,0,255);
fill(0,0,255);
<br>
 
ellipse(x,y,displaySize,displaySize);
ellipse(x,y,displaySize,displaySize);
<br>
 
}
}
<br>
 
<br>
 
void sensorTic2(int x, int y){
void sensorTic2(int x, int y){
<br>
 
stroke(255,0,0);
stroke(255,0,0);
<br>
 
fill(255,0,0);
fill(255,0,0);
<br>
 
ellipse(x,y,displaySize,displaySize);
ellipse(x,y,displaySize,displaySize);
<br>
 
}
 
 
 
==Heartrate Sensor - Circuit With Amplifier==
 
===Arduino===
 
define mask 255 // kill top bits
 
int potPin = 0; // select the input pin for the pot
 
int ledPin = 13; // select the pin for the LED
 
int val = 16706; // variable to store the value coming from the sensor
 
int val2 =0;
 
int a =0;
 
int b =0;
 
int beats[]= {0,0,0,0,0};// to track last five reads for a pattern
 
boolean beated = false;
 
//function dec
 
boolean getBioData();
 
 
void setup() {
 
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
 
Serial.begin(9600);
 
}
 
 
void loop() {
 
char check=' ';
 
val = analogRead(potPin); // read the value from the sensor
 
if (Serial.read() =='a'){ // check buffer for an 'a'
 
val2 = val;
 
b= val & mask;
 
a =((val2>>8) & mask); //just in case mask
 
delay(20);
 
// Serial.print("b"); // debug
 
// Serial.print(b);
 
Serial.print(a,BYTE);
 
Serial.print(b,BYTE);
 
if (getBioData()){ // call bio function
 
Serial.print('b',BYTE);
 
}
 
else Serial.print('n',BYTE);
 
}
 
}
 
boolean getBioData(){
 
int beatVal = analogRead(potPin); // read the value from the sensor
 
beats[4] = beatVal; // put in back of array
 
int beatDif = beats[5 - 1] - beats[0];
 
for (int i = 0; i < 5;i++){
 
beats[i] = beats[i+1]; // push zero out front
 
}
 
// check for beat
 
if ( beatDif > 10 && (beated != true)){
 
beated = true;
 
return true;
 
}
 
else if( beatDif < 2 ){
 
beated = false;
 
return false;
 
}
 
else return false;
 
}
 
 
 
===Processing===
 
import processing.serial.*;
 
 
Serial port; // Create object from Serial class
 
int val; // Data received from the serial port
 
int WIDTH=800; // set width
 
int number=0;
 
int num[] = new int[3];
 
int points[]= new int[WIDTH]; // points to be drawn from incoming data
 
char beat=' ';
 
int beats=0;
 
int dropNum[] = new int[4]; // array used to compare data not needed
 
 
void setup() {
println(Serial.list());
 
size(WIDTH, 700);
 
frameRate(30);
 
port = new Serial(this,"COM4", 9600); // local USB- port
 
}
 
 
void draw() {
background(0);// to erase
 
port.write('a');
 
if (2 < port.available()) { // wait for three bytes
 
for (int i=0;i<3;i++){
 
num[i] = port.read(); // read them into an array
 
}
 
//println( num[0]);
 
//println( num[1]);
number = (num[0] << 8)+num[1]; // num range add two incoming bytes together after shifting
 
beat = (char) num[2]; // look to see if there is a 'b' to signal a beat
 
println(beats);
 
}
 
stroke(0,255,100);
 
if (beat == 'b'){// sent from arduino
 
beats++;
 
}
 
// draw heart beat data
 
strokeWeight(1);
 
points[(WIDTH/2)] = number; // strat drawing half way accross screen give current reading to array
 
//goes through all points and draws a line between consecutive ones
 
for (int i=1 ;i< points.length-1; i++){
 
points[i]= points[i+1];
 
line(i,height-points[i-1]-40,i,height-points[i]-40);
 
}
 
}
}
<br>

Latest revision as of 00:48, 12 April 2012

Heartrate Sensor - Simple Circuit

Arduino

int an1,an2 = 0;
int redLedPin =13;
boolean triggered = false;
void setup(){
Serial.begin(9600);
pinMode(redLedPin, OUTPUT); // set the red LED pin to be an output
// Serial.println("Starting");
}

void loop(){
// read analog value in
int an2 = analogRead(0);
Serial.print("Y");
Serial.println(an2,DEC);
//threshold
int an1= analogRead(5);
Serial.print("X");
Serial.println(an1,DEC);

if(an1 > an2 && !triggered){
triggered = true;
digitalWrite(redLedPin, HIGH); // turn off the red LED
}

if(an1 <= an2 && triggered){
triggered = false;
digitalWrite(redLedPin, LOW); // turn off the red LED
}
}

Processing

import processing.serial.*;


String buff = "";

int val = 0;

int NEWLINE = 10;

int xPos,yPos,zPos = 0;

int displaySize = 2;

int an1, an2, an3; //an1 pot; an2 ir;

Serial port;


void setup(){

background(80);

size(800,600);

smooth();


port = new Serial(this,"COM4", 9600); //local USB- port

}


void draw(){

// new background over old

fill(80,5);

noStroke();

rect(0,0,width,height);

// wipe out a small area in front of the new data

fill(80); rect(xPos+displaySize,0,50,height);

// check for serial, and process

while (port.available() > 0) {

serialEvent(port.read());

}

}


void serialEvent(int serial) {

print("A"); //header variable, so we know which sensor value is which

println(an1); //send as a ascii encoded number - we'll turn it back into a number at the other end

//Serial.print(10, BYTE); //terminating character

print("B"); //header variable, so we know which sensor value is which

println(an2); //send as a ascii encoded number - we'll turn it back into a number at the other end

//Serial.print(10, BYTE); //terminating character


if(serial != '\n') {

buff += char(serial);

}

else {

int curX = buff.indexOf("X");

int curY = buff.indexOf("Y");


if(curX >=0){

String val = buff.substring(curX+1);

an1 = Integer.parseInt(val.trim());

xPos++;


if(xPos > width) xPos = 0;

sensorTic1(xPos,an1);

}


if(curY >=0){

String val = buff.substring(curY+1);

an2 = Integer.parseInt(val.trim());

yPos++;


if(yPos > width) yPos = 0;

sensorTic2(yPos,an2);

}

// Clear the value of "buff"

buff = ""; }

}


void sensorTic1(int x, int y){

stroke(0,0,255);

fill(0,0,255);

ellipse(x,y,displaySize,displaySize);

}


void sensorTic2(int x, int y){

stroke(255,0,0);

fill(255,0,0);

ellipse(x,y,displaySize,displaySize);

}


Heartrate Sensor - Circuit With Amplifier

Arduino

define mask 255 // kill top bits

int potPin = 0; // select the input pin for the pot

int ledPin = 13; // select the pin for the LED

int val = 16706; // variable to store the value coming from the sensor

int val2 =0;

int a =0;

int b =0;

int beats[]= {0,0,0,0,0};// to track last five reads for a pattern

boolean beated = false;

//function dec

boolean getBioData();


void setup() {

pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT

Serial.begin(9600);

}


void loop() {

char check=' ';

val = analogRead(potPin); // read the value from the sensor

if (Serial.read() =='a'){ // check buffer for an 'a'

val2 = val;

b= val & mask;

a =((val2>>8) & mask); //just in case mask

delay(20);

// Serial.print("b"); // debug

// Serial.print(b);

Serial.print(a,BYTE);

Serial.print(b,BYTE);

if (getBioData()){ // call bio function

Serial.print('b',BYTE);

}

else Serial.print('n',BYTE);

}

}

boolean getBioData(){

int beatVal = analogRead(potPin); // read the value from the sensor

beats[4] = beatVal; // put in back of array

int beatDif = beats[5 - 1] - beats[0];

for (int i = 0; i < 5;i++){

beats[i] = beats[i+1]; // push zero out front

}

// check for beat

if ( beatDif > 10 && (beated != true)){

beated = true;

return true;

}

else if( beatDif < 2 ){

beated = false;

return false;

}

else return false;

}


Processing

import processing.serial.*;


Serial port; // Create object from Serial class

int val; // Data received from the serial port

int WIDTH=800; // set width

int number=0;

int num[] = new int[3];

int points[]= new int[WIDTH]; // points to be drawn from incoming data

char beat=' ';

int beats=0;

int dropNum[] = new int[4]; // array used to compare data not needed


void setup() {

println(Serial.list());

size(WIDTH, 700);

frameRate(30);

port = new Serial(this,"COM4", 9600); // local USB- port

}


void draw() {

background(0);// to erase

port.write('a');

if (2 < port.available()) { // wait for three bytes

for (int i=0;i<3;i++){

num[i] = port.read(); // read them into an array

}

//println( num[0]);

//println( num[1]);

number = (num[0] << 8)+num[1]; // num range add two incoming bytes together after shifting

beat = (char) num[2]; // look to see if there is a 'b' to signal a beat

println(beats);

}

stroke(0,255,100);

if (beat == 'b'){// sent from arduino

beats++;

}

// draw heart beat data

strokeWeight(1);

points[(WIDTH/2)] = number; // strat drawing half way accross screen give current reading to array

//goes through all points and draws a line between consecutive ones

for (int i=1 ;i< points.length-1; i++){

points[i]= points[i+1];

line(i,height-points[i-1]-40,i,height-points[i]-40);

}

}