Monday, November 19, 2012

Some more Ultrasonic Range Finder tests: Processing Edition

So as seen from the videos Nezla posted, one of the tests we did was hook up the ultrasonic range finder  data into the serial port so that Processing could read and process it. To visualize that the data was coming through easily, I simply turned the read (which was coming in at inches) to the diameter of an elipse in processing. The result was the closer you were to the sensor, the smaller the circle. (IE: It did what we wanted it to do.)

Arduino Code: (project 38 modified)

// Project 38
#define sensorPin 9
long pwmRange, inch, cm;
void setup() {
  // Start serial communications
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
}
void loop() {
pwmRange = pulseIn(sensorPin, HIGH);

  // 147uS per inch according to datasheet
  inch = pwmRange / 147;

  Serial.println(inch);
}


Processing Code: (an example I do not remember)
import processing.serial.*;
Serial myPort; // The serial port:
  //one variable needed for the circle diameter
int circDiameter;

void setup() {
  size(400, 300);
  println(Serial.list());
    // match the port correctly from the printed serial list
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil('\n');
}

void draw() {
  background(200);
  fill(165, 27, 27);
  ellipse(width/2, height/2, (circDiameter*10)-500, (circDiameter*10)-500);
}

void serialEvent(Serial myPort) {
    // read String from the serial port:
  String inString = myPort.readString();
  println(inString);
  float tempDiameter = float(inString);
    //turn serial info into the circDiameter variable
  circDiameter = int( map(tempDiameter, 0, 1023, 30, 250) );
}

No comments:

Post a Comment