According to the PING))) documentation under the Resources tab, the input pulse must be at least 2 microseconds but typically should be 5 microseconds. The ATmega1284p on the SVP-1284 runs at 20MHz, so when you call set_digital_output back-to-back your pulse will most likely not be long enough to have an effect. You should insert a delay using delay_us(), like this:
set_digital_output(IO_C0, LOW);
delay_us(10);
set_digital_output(IO_C0, HIGH);
delay_us(5);
set_digital_output(IO_C0, LOW);
delay_us(10);
set_digital_input(IO_C0, HIGH_IMPEDANCE);
Also, I don’t recommend printing to the LCD during this operation because printing can be relatively slow and might mess up your timing; it might be okay, but without actually timing it we can’t be sure. You can use the user LEDs to get feedback.
Getting accurate timing is going to be harder because it will involve writing code to use a hardware timer on the AVR (unless you use Ben’s method). The AVR has three timers, named Timer 0, Timer 1, and Timer 2. You can read all about the them in the datasheet for the ATmega1284p.
Timer 0 is not used by the AVR library on the SVP (but it is used by OrangutanMotors on other Orangutans), so your code can do whatever it wants to Timer 0 without messing up the library.
Timer 1 is used by OrangutanServos, OrangutanBuzzer, and OrangutanPulseIn. If you aren’t using those sections of the library, you can do whatever you want to Timer 1.
Timer 2 is used by OrangutanTime, OrangutanMotors, and PololuQTRSensors, so you probably don’t want to change its configuration. However, you can write a program that reads the value of Timer 2 repeatedly and uses that information to measure time spans accurately. I recommend this method because it will leave Timer 0 and Timer 1 free for later use, and we already have example code for it. The code in PololuQTRSensors.cpp in readPrivate() shows you how to measure time spans with Timer 2, and I’ve put the important parts of that code here:
unsigned int time = 0;
unsigned char last_time;
unsigned char delta_time;
TCCR2A |= 0x03;
TCCR2B = 0x02; // run timer2 in normal mode at 2.5 MHz (compatible with OrangutanMotors)
last_time = TCNT2;
while(1)
{
// Keep track of the total time.
// This implicitly casts the difference to unsigned char, so
// we don't add negative values.
delta_time = TCNT2 - last_time;
time += delta_time;
last_time += delta_time;
if(pulseIsDone()) // insert your own condition here
{
break;
}
if (time > 55000) // maximum PING))) pulse time should be 18.5 ms (e.g. 46250 ticks)
{
break;
}
}
After the loop runs, the variable “time” should contain a measurement of how long the pulse lasted, in units of Timer 2 ticks (0.4 microseconds). I haven’t compiled or tested this code, so there may be some problems, but it was copied from PololuQTRSensors.cpp which definitely works.
-David