Wednesday, October 24, 2012

Final Processing Code


MAIN CODE:

PImage a;  // Declare variable "a" of type PImage

import processing.serial.*;      // this is how we talk to arduino
Serial port;                     // create and name the serial port
int heart = 0;            // used to time pulsing of heart graphic with your heart beat
int pulseRate = 0;        // used to hold pulse rate value sent from arduino 
                          // (beats per minute)
int Sensor = 0;           // used to hold raw sensor data from arduino
int[] pulseY;                 // used to hold pulse waveform Y positions
int[] rateY;                 // used to hold bpm waveform Y positions

boolean beat = false;            // used to advance heart rate graph
boolean newRate = false;         // used to update heart rate display

import processing.video.*;

Capture cam;
PImage img;
boolean newFrame=false;

int z = 0;
int sw = 0;

color black = color(0,0,0,40);
color white = color(255,255,255,0);
int numPixels;

void setup() {
  // Size of applet
  size(screen.width, screen.height);

// find and establish contact with the serial port
println(Serial.list());       // print a list of available serial ports
port = new Serial(this, Serial.list()[0], 115200); // choose the right one in square brackets
port.bufferUntil('\n');          // arduino will end each ascii string with 
                                 // a '\n' at the end (carriage return)
port.clear();                    // flush the serial buffer

  cam = new Capture(this, width, height, 10); // Capture
  // BlobDetection
  // img which will be sent to detection (a smaller copy of the cam frame);
  numPixels = cam.width * cam.height;
  noCursor();
  smooth();
  img = new PImage(80,60); 
  theBlobDetection = new BlobDetection(cam.width, cam.height);
  theBlobDetection.setPosDiscrimination(true);
  theBlobDetection.setThreshold(1.0f); // will detect bright areas whose luminosity > 0.2f;
}


void draw() {
  if (cam.available()) {
    
    cam.read();
    cam.loadPixels();
    int threshold = 95; // Set the threshold value
    float pixelBrightness; // Declare variable to store a pixel's color
    // Turn each pixel in the video frame black or white depending on its brightness
    loadPixels();
    for (int i = 0; i < numPixels; i++) {
      pixelBrightness = brightness(cam.pixels[i]);
      if (pixelBrightness > threshold) { // If the pixel is brighter than the
        pixels[i] = white; // threshold value, make it white
      } 
      else { // Otherwise,
        pixels[i] = black; // make it black
      }
    }
    updatePixels();

    theBlobDetection.computeBlobs(cam.pixels);
    drawBlobsAndEdges(true,true);
    
  }
}


void drawBlobsAndEdges(boolean drawBlobs, boolean drawEdges)
{
  
if (beat == true){
  fill(255,100);
  sw=2;
  rect(0,0,width,height);
  beat = false;      // reset beat flag
      Blob b;
      EdgeVertex eA,eB;
      for (int n=0 ; n<theBlobDetection.getBlobNb() ; n++) {
       b=theBlobDetection.getBlob(n);
         if (b!=null) {
         if (drawEdges) { 
         smooth();
         noStroke();
         //stroke(0);
         //strokeWeight(0);
           for (int m=0;m<b.getEdgeNb();m++) {
           eA = b.getEdgeVertexA(m);
           eB = b.getEdgeVertexB(m);
             if (eA !=null && eB !=null)
             line(eA.x*width, eA.y*height, eB.x*width, eB.y*height);
                }
              }
            }
          }
        }
          
else {
 noStroke();
 }
}



serialEvent tab


/*
Serial data is sent from arduino with leading ascii character 
ascii character tells processing what to do with the data
*/


void serialEvent(Serial port){   
   String inData = port.readStringUntil('\n');  
   inData = trim(inData);             // trim the \n off the end
// leading 'Q' means heart rate data   
   if (inData.charAt(0) == 'Q'){      // following string contains current heart rate  
     inData = inData.substring(1);    // cut off the leading 'Q'
     pulseRate = int(inData);         // convert ascii string to integer
     newRate = true;                  // flag the new heart rate data so it can be mapped onscreen
     return;
   }  
// leading 'S' means sensor data   
   if (inData.charAt(0) == 'S'){      // following string contains raw sensor data
     inData = inData.substring(1);    // cut off the leading 'S'
     Sensor = int(inData);            // convert ascii string to integer
   return;     
   }

// leading 'B' means arduino found a heart beat
   if (inData.charAt(0) == 'B'){          // following strint is time between beats in miliseconds
     inData = inData.substring(1);        // cut off the leading 'B'
     beat = true;                         // set beat flag to advance heart rate graph
     heart = 10;                          // begin heart graphic 'swell' timer 
     return;
   }

   
   
}



I know someone was reading the title of the code, but that had nothing to do with the project. It only had "blob" in the title because originally we were using blob detection.


No comments:

Post a Comment