Help with Program, not working!

Okay, so my robot doesn’t function when I turn it on and upload the program. I know for sure it has something to do with the pre-auton. Can someone help? Thanks. (here is my pre-auton section)

const short leftButton = 1;
const short centerButton = 2;
const short rightButton = 4;

void waitForPress()
{
while(nLCDButtons == 0){}
wait1Msec(5);
}
void waitForRelease()
{
while(nLCDButtons != 0){}
wait1Msec(5);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void pre_auton()
{
//Declare count variable to keep track of our choice
int count = 0;

//------------- Beginning of User Interface Code ---------------
//Clear LCD
clearLCDLine(0);
clearLCDLine(1);
//Loop while center button is not pressed

while(nLCDButtons != centerButton)

{
	//Switch case that allows the user to choose from 4 different options
	switch(count){
	case 0:
		//Display first choice
		displayLCDCenteredString(0, "Auto Skyrise Red");
		displayLCDCenteredString(1, "<		 Enter		>");
		waitForPress();
		//Increment or decrement "count" based on button press
		if(nLCDButtons == leftButton)
		{
			waitForRelease();
			count = 3;
		}
		else if(nLCDButtons == rightButton)
		{
			waitForRelease();
			count++;
		}
		break;
	case 1:
		//Display second choice
		displayLCDCenteredString(0, "Auto Skyrsie Blue");
		displayLCDCenteredString(1, "<		 Enter		>");
		waitForPress();
		//Increment or decrement "count" based on button press
		if(nLCDButtons == leftButton)
		{
			waitForRelease();
			count--;
		}
		else if(nLCDButtons == rightButton)
		{
			waitForRelease();
			count++;
		}
		break;
	case 2:
		//Display third choice
		displayLCDCenteredString(0, "Auto Cubes red-driver, blue-X");
		displayLCDCenteredString(1, "<		 Enter		>");
		waitForPress();
		//Increment or decrement "count" based on button press
		if(nLCDButtons == leftButton)
		{
			waitForRelease();
			count--;
		}
		else if(nLCDButtons == rightButton)
		{
			waitForRelease();
			count++;
		}
		break;
	case 3:
		//Display fourth choice
		displayLCDCenteredString(0, "Auto Cubes red-X, blue-driver");
		displayLCDCenteredString(1, "<		 Enter		>");
		waitForPress();
		//Increment or decrement "count" based on button press
		if(nLCDButtons == leftButton)
		{
			waitForRelease();
			count--;
		}
		else if(nLCDButtons == rightButton)
		{
			waitForRelease();
			count = 0;
		}
		break;
	default:
		count = 0;
		break;
	}
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bLCDBacklight = true;									// Turn on LCD Backlight
string mainBattery, backupBattery;


{
	clearLCDLine(0);											// Clear line 1 (0) of the LCD
	clearLCDLine(1);											// Clear line 2 (1) of the LCD

	//Display the Primary Robot battery voltage
	displayLCDString(0, 0, "Primary: ");
	sprintf(mainBattery, "%1.2f%c", nImmediateBatteryLevel/1000.0,'V'); //Build the value to be displayed
	displayNextLCDString(mainBattery);

	//Display the Backup battery voltage
	displayLCDString(1, 0, "Backup: ");
	sprintf(backupBattery, "%1.2f%c", BackupBatteryLevel/1000.0, 'V');	//Build the value to be displayed
	displayNextLCDString(backupBattery);

	//Short delay for the LCD refresh rate
	wait1Msec(100);
}

}

Your LCD can display only 16 characters per line, but “Auto Skyrsie Blue” is 17.
I can’t even count how many “Auto Cubes red-driver, blue-X” might have.
Also, you have only 2 lines of display.

Is this what you mean by not working?

It would be more helpful if you posted the .c file of your code. It is hard to diagnose a problem with only one portion of the code. Also, what is the robot doing…nothing? Need more details.

One thing I notice. It looks like you took the sample code as an example and modified it for your own needs. We did the same thing but we had to take the “int count = 0;” at the beginning of pre-auton and move it above pre-auton to make it a global variable. The problem is that when you put a variable (int count = 0) in pre-auton it only exists in pre-auton. When your code switches to autonomous, it doesn’t know what the integer “count” is anymore. Therefore is doesn’t “remember” what buttons your had pushed during pre-auton.

If you put “int count = 0;” above the pre-auton portion of the code it will now be a global variable which will allow it to be used by any other portion of your code.

There may be other problems, but I am fairly certain that is your main one.

Just to be sure, you have isolated the problem to this section. Did you try commenting just this out and see if it works then.

29

I’ll take your word for it! :slight_smile:

Yep, I should make that text shorter. But right now I’m not exactly working on the LCD. What isn’t working is the robot, when I turn everything on, nothing works. The servos go to 0, but the remotes don’t work. When I eliminate the pre-auton from my code, everything works. I think that may be the problem though with the int = 0 thing. Ill try that when I see my robot next. Thanks.

what did you mean by this?

I just meant that an LCD has only 2 lines available. They are lines 0 and 1. I think you restricted yourself to only 2 lines, but I just wanted to make sure you were not expecting strings over 16 characters from spilling over onto several multiple lines. Sorry that my comment was ambiguous.

So i tried the code, it’s waiting for you to hit the LCD center button, are you doing that? After that the robot runs.

I am not using the LCD right now and I’m not worried about the autonomous. I’m confused why the robot remotes don’t make the robot work when I have the pre-auton in the whole code. Is the robot not working because I haven’t selected an autonomous using the LCD? Is that why the code doesn’t “move on”?

The code is waiting for user input using the LCD in pre-auton.

while(nLCDButtons != centerButton)
  {
  // snip
  }

I get the feeling you just dropped this code in after copying it from somewhere else and don’t really understand what it is trying to do.

I think you should attach a copy of your entire code, as was suggested earlier.

Are you using a competition template? If so, are you using the VexNet competition switch to test your code on your robot? or what?

The thing is, his code is “working”, but because there is no LCD he cannot push the LCD center button and allow pre-auton to exit. What’s needed is code in the waitForPress function that monitors the competition state and exit if the robot is not disabled.

I keep linking this thread about autonomous selection using an LCD, I know the code there was slightly above beginner level, but it is designed to overcome exactly the sort of problem the OP has.
ROBOTC LCD autonomous selection

Although I do encourage all students to write their own code, if the OP is going to use an example found on the forum without understanding how it works, at least use one of mine :slight_smile:

I think my own kids did exactly that last year. It was a great help for I was clueless-as-could-be when it came to the LCD. Thanks! :slight_smile:

Yes, I did copy in a sample program, and not all of it makes sense and yes, I am using a competition template. I have not used the competition switch yet to test the robot, just a regular vexnet connection from remote to cortex.

For now I just have commented out the pre-auton and the robot works just fine. I’ll wait till I actually have the LCD on the robot and ready to go, to see if it actually works, but thanks for all your help, it is much appreciated!!!