I have hooked up the sensor (see the image) and written some very bare bones code. Essentially all the code really does is set pin 13 high for a short period of time and then polls pin 10. Pin 10 goes high instantly (for three cycles of the loop) and then stays low forever. I expected pin 10 to be low initially and then go high for a very short period of time and then low again.
Any ideas what I'm doing wrong here?
/*
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 pulse_pin = 13; // pin for triggering pulse
const int echo_pin = 10; // pin for recieving echo
int pulse_state ; // 0 (no pulse recieved) 1 (pulse recieved)
int port_10_value;
void setup() {
Serial.begin(9600);
Serial.println ("Starting");
// initialize the pulse pin as output:
pinMode(pulse_pin, OUTPUT);
// initialize the echo_pin pin as an input:
pinMode(echo_pin, INPUT);
//test code to be removed
}
void loop(){
pulse_state = 0;
port_10_value = 0;
digitalWrite(pulse_pin, LOW);
delayMicroseconds(2);
digitalWrite(pulse_pin, HIGH); // send the pulse
delayMicroseconds(5);
digitalWrite(pulse_pin, LOW); //wait and shut it down
Serial.println("in main loop");
while (pulse_state==0)
{
port_10_value = digitalRead (echo_pin);
Serial.println("port 10 value");
Serial.println(port_10_value,DEC);
// will stay here forever because pulse_state is not changed
}