Thanks guys, ill try all suggestions later today and let you know how I get on.
I did try newping but it didnt make much sense to me, im very much a newbie and this is the first thing ive done othr than making an LED flash and other very simple bits.
NewPing has fairly simple examples. But, maybe in my side-quest to steer people to an event-driven programming paradigm I've confused some. Below is an example NewPing sketch using a more common to Arduino, line-by-line with delays which works well for a "hello world" sample:
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 13 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(50); // Wait 50 milliseconds between pings (ping about 20 times per second).
int cm = sonar.ping_cm(); // Send out the ping, get the results in centimeters.
Serial.print("Ping: ");
Serial.print(cm); // Print the result (0 = outside the set distance range, no ping echo)
Serial.println("cm");
}
As you're new to the Arduino I believe the above sketch would make more sense. Let me know if it's not clear. I would suggest, however, that you consider an event-driven paradigm instead of the all too common line-by-line with delays. Line-by-line with delays may be easy to understand and works fine for a example. But, it doesn't work well when you try to build something complex. Changing your mindset now to an event-driven paradigm will make it much easier down the road when you want to incorporate motors, servos and sensors in your project and have your sketch do complex tasks.
Tim