WE have to write a C# Form program which handing the Ultrasonic Sensor also.
My codes is up to now:
- Code: Select all
// sensor types
private NXTBrick.SensorType[] sensorTypes = new NXTBrick.SensorType[] {
NXTBrick.SensorType.Lowspeed9V };
// sensor modes
private NXTBrick.SensorMode[] sensorModes = new NXTBrick.SensorMode[] {
NXTBrick.SensorMode.Raw };
and
- Code: Select all
public bool GetUltrasonicSensorsValue(NXTBrick.Sensor sensor, out int value )
{
value = -1;
// request distance value
if (!nxt.LsWrite(NXTBrick.Sensor.First, new byte[] { 0x02, 0x42 }, 1))
return false;
int readyBytes = -1;
for ( int i = 0; i < 10; i++ )
{
if (!nxt.LsGetStatus(NXTBrick.Sensor.First, out readyBytes))
return false;
if ( readyBytes >= 1 )
{
// read from I2C device
byte[] readValues = new byte[1];
int bytesRead;
if (!nxt.LsRead(NXTBrick.Sensor.First, readValues, out bytesRead))
return false;
value = readValues[0];
return true;
}
}
return false;
}
and I have a timer for handing
- Code: Select all
private void ultrasonicTimer_Tick(object sender, EventArgs e)
{
int iUltrasound;
GetUltrasonicSensorsValue(NXTBrick.Sensor.First, out iUltrasound);
lblDistance.Text = Convert.ToString(iUltrasound);
}
but they didn't work.
Anyone can help me why?
THX
Istvan