VEX Ultrasonic Range finder + Arduino UNO

Hi all,
I read some other forums on how to get the vex range finder to work with the arduino uno. I am having some troubles though. The only numbers i have been able to get are 5 digit numbers in the 30,000 range. This is the code I used that supposedly worked for someone else but gives me these funky numbers that dont even correspond to how close an object is to the range finder.

Code

/*
Uses the Vex Range Finder in conjunction with 
Arduino Mega 2560
Pin 13 Triggers the Pulse (Yellow lead)
Pin 10 Recieves the Echo  (Orange lead)
*/

const int Trig_pin =  13;   // pin for triggering pulse
const int Echo_pin = 10;     // pin for recieving echo
long duration;


void setup() {
  Serial.begin(9600);
  Serial.println ("Starting");
  // initialize the pulse pin as output:
  pinMode(Trig_pin, OUTPUT);      
  // initialize the echo_pin pin as an input:
  pinMode(Echo_pin, INPUT);     
  
}

void loop(){
  digitalWrite(Trig_pin, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig_pin, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig_pin, LOW);
  duration = pulseIn(Echo_pin,HIGH);
   
    
   
   Serial.println("Duration:  ");
   Serial.println(duration, DEC);

}

funky numbers ----
36731
0
36730
0
36728
0
36733
0
36732
0
36732
0
36731
0
36732
0
36732
0
36733
0
36732
0
36735

TWeiss33:
Hi all,
I read some other forums on how to get the vex range finder to work with the arduino uno. I am having some troubles though. The only numbers i have been able to get are 5 digit numbers in the 30,000 range. This is the code I used that supposedly worked for someone else but gives me these funky numbers that dont even correspond to how close an object is to the range finder.

First, use the NewPing library instead of rolling your own, you're project will work so much better.

Secondly, did you notice that it's not displaying "Duration: " either? You probabaly really mean for that line to read:

Serial.print("Duration:  ");

But, it's not showing "Duration:" in your output. In other words, this has nothing to do with your sensor. You can't even get your sketch to output "Duration:" to the serial port. Notice in your sketch where you open the serial port, you specify 9600 baud. My guess, is that you've set a different baud rate in the Arduino IDE.

Basically, your sketch is set to 9600 baud and your computer program (Arduino IDE) is set to some other speed. So, the data is garbage. My guess is if you make both the baud rates the same, it will at least show "Duration:". Then and ONLY then can you start diagnosing a possible problem with your sensor or the sketch.

Tim

Thanks for the help Tim!