CLASS DOCUMENTS


REPORTS & ASSIGNMENTS


CLASS CONTENT


USING THIS SITE








registered authors login here

You are: (logout)



For more on PMWiki, see pmwiki.org


Three Sensors Set Thresh

By Jeff Gray, February 2008

Just like previous, three sensors with one threshold. On mouse click, another sensor sets the low limit of that threshold.

Arduino Code

int an1, an2, an3 = 0;

void setup() {
  Serial.begin(19200);
}

void loop() {
  an1 = analogRead(0);
  delay(5); 
  an2 = analogRead(1);
  delay(5); 
  an3 = analogRead(2);
  Serial.print("X");
  Serial.println(an1,DEC);
  Serial.print("Y");
  Serial.println(an2,DEC);
  Serial.print("Z");
  Serial.println(an3,DEC);
  delay(15);               
}

Processing Code

import processing.serial.*;

String buff = "";
int val = 0;
int NEWLINE = 10;
int xPos,yPos,zPos = 0;
int displaySize = 2;
int an1, an2, an3;

int yHigh = 200;
int yLow = 300;

Serial port;

void setup(){
  background(80);
  size(800,600);
  smooth();

  port = new Serial(this, Serial.list()[0], 19200);
}

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());
  }

  if(an2 < yHigh || an2 > yLow){
    fill(255,0,0);
    stroke(0);
    rect(width-40,20,20,20);
  } else {
    fill(80);
    stroke(80);
    rect(width-40,20,20,20);
  }
}

void mousePressed(){
 yHigh = an3; 
}

void serialEvent(int serial) { 
  if(serial != '\n') { 
    buff += char(serial);
  } else {
    int curX = buff.indexOf("X");
    int curY = buff.indexOf("Y");
    int curZ = buff.indexOf("Z");

    if(curX >=0){
      String val = buff.substring(curX+1);
      an1 = Integer.parseInt(val.trim());

      xPos++;
      if(xPos > width) xPos = 0;

      sensorTic(xPos,an1+20,255,0,0);
    }    
    if(curY >=0){
      String val = buff.substring(curY+1);
      an2 = Integer.parseInt(val.trim()) / 2;

      yPos++;
      if(yPos > width) yPos = 0;

      sensorTic(yPos,an2,0,255,0);
    }
    if(curZ >=0){
      String val = buff.substring(curZ+1);
      an3 = Integer.parseInt(val.trim()) / 2;

      zPos++;
      if(zPos > width) zPos = 0;

      sensorTic(zPos,an3,0,0,255);
    }

    // Clear the value of "buff"
    buff = "";
  }

  drawHighLow();
}

void drawHighLow(){
  stroke(200);
  line(0,yLow,width,yLow);
  line(0,yHigh,width,yHigh);
}

void sensorTic(int x, int y, int r, int g, int b){
  stroke(r,g,b);
  fill(r,g,b);
  ellipse(x,y,displaySize,displaySize);
}
  Edit | View | History | Print | Recent Changes | Search Page last modified on February 25, 2008, at 12:13 PM