Re: Cosine function
Posted: 24 Dec 2010, 16:17
Another approach to dealing with the problems you describe is put the US sensor in single-shot or ping mode. In this mode it will return up to eight values each time it is "fired". The values show detections of objects from closest to farthest or 255 which means no detection. In my own experiments I have never found more than four useful values, but there may be situations where more than four are useful. Here is some code that will let you experiment with ping mode.
//
//
Code: Select all
#define MAX_BYTES 4
const byte ping_cmnd[] = {0x2, 0x41, 0x1}; // sensor one-shot-mode command
const byte register_select[] = {0x2, 0x42}; // sensor data register
// Wait until Select button is bumped.
void wait_for_select()
{
until (ButtonPressed(BTNCENTER, true));
while (ButtonPressed(BTNCENTER, true));
PlayFile("! Click.rso");
}
// Ping the US sensor when the Select button is bumped.
task main()
{
byte data[MAX_BYTES];
byte data_size = MAX_BYTES;
SetSensorLowspeed(IN_4); // sensor is connected to port 4
while (true)
{
wait_for_select();
ClearScreen();
I2CWrite(IN_4, 0, ping_cmnd); // send the ping command
Wait(MS_100); // give sensor time to do the ping
// Read and display the range data produced by the ping
if (I2CBytes(IN_4, register_select, data_size, data))
{
for (int i = 0; i < MAX_BYTES; ++i)
NumOut(40, 56 - 8 * i, data[i]);
}
else TextOut(0, LCD_LINE1, "Sensor Error");
}
}
//