Not sure where you got the code, but unless it’s written by a code generator the author should be ashamed for not providing in-code comments as documentation. I’m also not a fan of cryptic variable names like S1 and S2 because the reader has to spend more cognitive effort to figure out what those variables stand for. IMHO, RIGHT_IN_LEFT_IN, etc., would have been much more obvious.
I’ve added comments that I think make sense. The LineFollowFlag uses 10 as a median value. Every time one of the sensors is not over the line (and remember there are two sensors in the line following sensor), the LineFollowFlag will be decremented or incremented to track the last state. When both of the sensors indicate they are not over the line, the program checks the last LineFollowFlag value to determine whether it needs to back up, turn left, or turn right.
Hope this helps.
Chuck
void modeC()
{
uint8_t val;
// capture the current value of the line sensor
val = line.readSensors();
// if the speed is greater than 230 set the moveSpeed value to 230
// This is likely acting as a governor to control the maximum speed
if(moveSpeed >230)moveSpeed=230;
switch (val)
{
// Both line sensors are over the line
case S1_IN_S2_IN:
Forward(); // move forward
LineFollowFlag=10; // State flag, 10 is the median value
break;
// Left sensor is over the line, Right sensor is not over the line
case S1_IN_S2_OUT:
Forward(); // move forward
// decrement the state flag to indicate that the right sensor
// is not over the line
if (LineFollowFlag>1) LineFollowFlag--;
break;
// Left sensor is not over the line, Right sensor is over the line
case S1_OUT_S2_IN:
Forward(); // Move forward
// increment the state flag to indicate that the left sensor
// is not over the line
if (LineFollowFlag<20) LineFollowFlag++;
break;
// Both sensors are not over the line
case S1_OUT_S2_OUT:
// if both sensors are equally off, move backward
if(LineFollowFlag==10) Backward();
// if the Right sensor is not over the line, turn left
if(LineFollowFlag<10) TurnLeft();
// if the Left sensor was the last state before both sensors
// were off of the line, turn right
if(LineFollowFlag>10) TurnRight();
break;
}