We have a loading ramp where a ball trips a limit switch, but I cant be sure of the timing. I am trying to figure out how to create a wait function (go on regardless) that also would catch the sensor trip if it occurred before a timeout period. Using a sleep command would likely miss the trip of the switch. This would be in autonomous if that matters…
any ideas?
There are a number of ways to do this but most boil down to using a counter in a loop that checks for the condition you want to be true. If the counter reaches some known value then the loop is exited and the program moves on. Here is a simple example.
#pragma config(Sensor, dgtl1, limitSwitch, sensorTouch)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
#define TIMEOUT 5000
#define TIMEOUT_DELAY 10
bool
waitForSwitch()
{
long currentTime = TIMEOUT;
// wait for switch press - 5 seconds maximum
for( currentTime = TIMEOUT; currentTime > 0; currentTime -= TIMEOUT_DELAY)
{
if( SensorValue limitSwitch ] == 1 )
break;
wait1Msec(TIMEOUT_DELAY);
}
if( currentTime == 0 )
return( false );
else
return( true );
}
task main()
{
if( waitForSwitch() )
writeDebugStreamLine("switch pressed");
else
writeDebugStreamLine("timeout");
while(1)wait1Msec(10);
}