Hello, I have set up an SRF05 range finder (SRF05 Technical Documentation) with my arduino dicemilia, and got it up and running just fine with this sketch for the arduino:
/*
Ultrasonic Sensor sketch
This program reads a Devantech SRF04 ultrasonic distance sensor
The SRF04 sensor's pins are connected as described below.
Created 9 November 2006
By Tom Igoe and Neilson Abeel
*/
#define echoPin 2 // the SRF04's echo pin
#define initPin 3 // the SRF04's init pin
unsigned long pulseTime = 0; // variable for reading the pulse
void setup() {
// make the init pin an output:
pinMode(initPin, OUTPUT);
// make the echo pin an input:
pinMode(echoPin, INPUT);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
// send the sensor a 10microsecond pulse:
digitalWrite(initPin, HIGH);
delayMicroseconds(10);
digitalWrite(initPin, LOW);
// wait for the pulse to return. The pulse
// goes from low to HIGH to low, so we specify
// that we want a HIGH-going pulse below:
pulseTime = pulseIn(echoPin, HIGH);
pulseTime=pulseTime/58;
// print out that number
Serial.println(pulseTime,DEC);
delay(50);
}
I get a perferct reading on the serial monitor window.
But I have a hard time to get this reading of the distance inprocessing (it will be udes to scale an image)
I just want to get this distance reading in an int variable to use it as a multiplier.
I have tried many examples, this one is the closest to working:
import processing.serial.*;
Serial port; // Create object from Serial class
int val; // Data received from the serial port
String portname = "/dev/cu.usbserial-A4001LGU"; // find the name of your serial port in your system setup!
int baudrate = 9600; // set baudrate here
void setup() {
size(200, 200);
noStroke();
frameRate(10); // Run 10 frames per second
// Open the port that the board is connected to and use the same speed (9600 bps)
port = new Serial(this,portname, 9600);
}
void draw() {
while (port.available() > 0) {
String inBuffer = port.readString();
if (inBuffer != null) {
println(inBuffer);
}
}
}
I do get the reading but I can't seem to use this value stored in "inbuffer" to put in an int variable since it is not a number but a string (well this is where I think the problem is...but I am quite a newbee so it is a guess)
I also tried:
if (0 < port.available()) { // If data is available to read,
val = port.read(); // read it and store it in val
println(val);
}
println(inBuffer);
}
But then the numbers are crazy, nothing in common with the sensor reading...