Hi
I am trying to use specific ranges from an ultrasonic range sensor (2 sensors) hooked up to arduino to trigger an action in a Processing sketch. Arduino reads, prints and sends values from the sensor to Processing, who in turn, can print them out so I know communication is happening at this level.... What Processing is not doing is acknowledging a threshold other than 0 to trigger the sound.
I am extremely confused at this point (and clearly very new...)
I wonder if the error is at the serial level or parsing of data, or both or other....
Any help would be very very very very very appreciated!!!!!!!!
Thank you!!!!!!!!!!!!!!
ARDUINO:
//modified from Tom Igoe
const int pwPin = 7; // ultrasonic PW output
const int pingPin = 4; // PING))) sensor PW output
void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(pwPin, INPUT);
pinMode(pingPin, INPUT);
}
void loop()
{
int v;
v= ez1();
Serial.print (v);
Serial.print (" ");
v= PING();
Serial.print (v);
Serial.println("");
delay (500);
}
/* PING))) sensor; return range in inches. */
int PING () {
long duration;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
// delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
return (pulseIn (pingPin, HIGH) / 29 / 2);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
/* Read the EZ-1 sensor and return range, in inches. */
int ez1 () {
return (pulseIn (pwPin, HIGH) / 147);
}
PROCESSING:
// modified example by Tom Igoe
import processing.serial.*;
import java.lang.Integer;
import ddf.minim.*;
Minim minim;
AudioPlayer player;
AudioSample Sample1;
AudioSample Sample2;
int maxNumberOfSensors = 2;
Serial myPort;
float[] previousValue = new float[maxNumberOfSensors];
float val = 0;
float val1 = 1;
void setup() {
size(50, 50);
String portName = Serial.list()[0];
myPort = new Serial(this, Serial.list()[0], 9600);
println ("Port open");
myPort.clear();
myPort.bufferUntil('\n');
println("setup done");
minim = new Minim(this);
Sample1 = minim.loadSample("Sample1");
Sample2 = minim.loadSample("Sample2");
}
void draw() {
}
void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil('\n'); //get ASCII string
if (inString != null) {
print(inString);
inString = trim(inString);
//
// if (myPort.available() > 0) {
// print(myPort);
float incomingValues[] = float(split(inString, ","));
//print out the values
println("length: " + incomingValues.length + " values.\t");
if (incomingValues.length == maxNumberOfSensors) {
// for (int i = 0; i < incomingValues.length; i++ ) {
println("incomingValues");
if (incomingValues[0]>=0) {
val = incomingValues[0];
//println("val>0");
// Sample1();
}
if (incomingValues[1]>=0)
val1 = incomingValues[1];
//println("val2>0");
// Sample2();
}
//
// if (val>0 && val<100) {
// Sample1();
// }
if(val >= 0) {
Sample2();
}
}
}
void stop() {
Sample1();
Sample2();
minim.stop();
super.stop();
}