# IR Code RobotC

Hello. I am a student on a FTC team with a question regarding IR sensor coding in RobotC. I was recently working on a program that uses the IR Beacon for this year’s FTC Game. I started off with a more basic and conventional IR Code as follows:

{
while(1 == 1)
{
if(SensorValue[IRSeeker2] == 5)
{
motor[motorD] = 50;
motor[motorE] = 50;
motor[motorF] = -50;
motor[motorG] = -50;
}

``````	if(SensorValue[IRSeeker2] &gt; 5)
{
motor[motorD] = 50;
motor[motorE] = 50;
motor[motorF] = 50;
motor[motorG] = 50;
}

if(SensorValue[IRSeeker2] &lt; 5)
{
motor[motorD] = -50;
motor[motorE] = -50;
motor[motorF] = -50;
motor[motorG] = -50;
}
}
``````

}

This worked very well and did exactly what it was intended to do; follow the IR beacon infinitely. However, when looking at the code and testing it with the IR beacon on the ring dispenser I realized I needed something that stopped a just slightly before reaching the ring rack so that we could place the ring instead of the code going on infinitely. Below is the code that I came up with.

{
if(SensorValue[IRSeeker2] == 5)
{
motor[motorD] = 50;
motor[motorE] = 50;
motor[motorF] = -50;
motor[motorG] = -50;
wait1Msec(5000);
}

``````	if(SensorValue[IRSeeker2] &gt; 5)
{
motor[motorD] = 50;
motor[motorE] = 50;
motor[motorF] = 50;
motor[motorG] = 50;
wait1Msec(5000);
}

if(SensorValue[IRSeeker2] &lt; 5)
{
motor[motorD] = -50;
motor[motorE] = -50;
motor[motorF] = -50;
motor[motorG] = -50;
wait1Msec(5000);
}
``````

}

However, this didn’t work as intended. Instead of doing the desired task when the IR beacon was at different positions, each time it defaulted to the third section which is what it is supposed to do only when the sensor value is < 5. I made sure that it wasn’t a physical problem with the bot by running our previous code and it still worked as well as it did before. Therefore, I am a tad stuck as to why it isn’t changing when the beacon is in different positions. Any help with this would be very much appreciated. Thanks.

JIT

Start by reading through your second program, and restating it in English:

When the program starts, it does three comparisons, seeing whether the value returned by the IR sensor is equal to, greater than, or less than 5. Only one of these will be true. Based off of the results of these comparisons, it sets the motors to either drive straight, turn right or turn left. Then, regardless of the case, the program waits for 5 seconds. Then the program exits.

From this description, we can conclude that the program is only checking the IR sensor one time. As to why it always defaulted to the third case, it’s possible that when the program first starts, the sensor gets a couple of bad readings because it’s still “warming up.” You wouldn’t notice these in the first program because it’s checking the sensor continuously a couple hundred times a second.

If you want the program to only run for a certain amount of time and then stop, I would recommend using the first program, but changing the while loop condition to stop looping after a certain amount of time. You can check out the RobotC docs on Timing for more info, but here’s what I would suggest:

``````task main()
{
time100[T1] = 0;
while(time100[T1] < 50)
{

// insert sensor if statements here

}
}
``````

Thank you very much; that was exactly what I was looking for.

JIT