How do I begin line following with a button push?

robot follows line very well until I add button to start program, then it only moves forward…I am stumped! Goal is for robot to follow line after button push.

#pragma config(Sensor, in1, lineFollowerRIGHT, sensorLineFollower)
#pragma config(Sensor, in2, lineFollowerCENTER, sensorLineFollower)
#pragma config(Sensor, in3, lineFollowerLEFT, sensorLineFollower)
#pragma config(Sensor, dgtl1, button, sensorTouch)
#pragma config(Motor, port2, rightMotor, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port3, leftMotor, tmotorServoContinuousRotation, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main()
{

while(true)
{

if(SensorValue(button)==1)
{
wait(1);
}

if(SensorValue(lineFollowerRIGHT) > threshold)
{
  // counter-steer right:
  motor[leftMotor]  = 30;
  motor[rightMotor] = -45;
}
// CENTER sensor sees dark:
if(SensorValue(lineFollowerCENTER) > threshold)
{
  // go straight
  motor[leftMotor]  = 30;
  motor[rightMotor] = 30;
}
// LEFT sensor sees dark:
if(SensorValue(lineFollowerLEFT) > threshold)
{
  // counter-steer left:
  motor[leftMotor]  = -45;
  motor[rightMotor] = 30;

}
}
}

I am assuming this block of code is the only change you made before the robot stopped working correctly.

if(SensorValue(button)==1)
{
    wait(1);
}

The if statement will read only once, and the robot doesn’t loop back over this statement and immediately executes the statements below it. Because the statement won’t loop back, the button press (or absence thereof) won’t have an effect on running the code or not. I assume you want the robot to wait to execute anything until that button is pressed. This if statement won’t do the trick. Instead, do this:


while(SensorValue[button] == 0)
{
//Insert nothing.
}

This is a proper loop that you need for your robot to work the way you want it to. What this will do is it will constantly check to see if the button is pressed. If it’s not, the robot does nothing (per the empty loop) and checks again. Once it detects the button is pressed, the robot will exit this loop and run the rest of the code.

that was the first thing I tried actually…the robot did not function properly so I tried several other ways…could it possibly be something mechanical?

i take that back…i used if rather than while…why does this make such a difference? and thank-you for your assistance

still not the desired result though…

Have an initial while loop with a condition of the button being not pressed, and inside have no contents.

Have a second while loop (after, not inside of the first one) and put your line following code inside of it.

This should “wait” at the first loop until the button is pressed, then jump into the second while loop, which is your line following code.

The if statement does not repeat. So you’re either giving a tiny delay or not, and then you’re continuing onward with you code regardless. With a small enough wait, this essentially does nothing at all. Also, since it only checks that once, if you don’t have the button pressed or not as desired when you start the program, you’ll miss your opportunity.

What you really want is for the program to go into a holding pattern until you press the button. That means you need a loop. A while loop incorporates an if statement into it for its exit condition. So, with the while loop, you keep checking that button forever until you find it, at which point you can continue on with the program.

On a related note, you want to put that while (was if) bit for the button press before and outside of your while(true). You want this somewhat-infinite loop to end before running your infinite loop to follow the line. The way you have it written, it’s going to check for a button press every single time it checks the line tracker.

The following is what I would do. It allows the push button to act as a toggle to enable/disable the line follower.


#pragma config(Sensor, in1, lineFollowerRIGHT, sensorLineFollower)
#pragma config(Sensor, in2, lineFollowerCENTER, sensorLineFollower)
#pragma config(Sensor, in3, lineFollowerLEFT, sensorLineFollower)
#pragma config(Sensor, dgtl1, button, sensorTouch)
#pragma config(Motor, port2, rightMotor, tmotorServoContinuousRotation, openLoop)
#pragma config(Motor, port3, leftMotor, tmotorServoContinuousRotation, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main()
{
    bool lineFollowerActive = false;
    while(true)
    {
        //Use button to toggle the boolean "lineFollowerActive"
        //If button is pushed
        if(SensorValue[button] == 1)
        {
            //Wait for button release
            while(SensorValue[button] == 1)
            {
                //Do nothing
                EndTimeSlice();
            }
            //Toggle line follower
            lineFollowerActive = !lineFollowerActive;
        }
        
        if(lineFollowerActive)
        {
            if(SensorValue(lineFollowerRIGHT) > threshold)
            {
                // counter-steer right:
                motor[leftMotor] = 30;
                motor[rightMotor] = -45;
            }
            // CENTER sensor sees dark:
            if(SensorValue(lineFollowerCENTER) > threshold)
            {
                // go straight
                motor[leftMotor] = 30;
                motor[rightMotor] = 30;
            }
            // LEFT sensor sees dark:
            if(SensorValue(lineFollowerLEFT) > threshold)
            {
                // counter-steer left:
                motor[leftMotor] = -45;
                motor[rightMotor] = 30;
            }
        }
    }
}