Help merging code

I need to merge several of my test code projects for my robot into a lcd menu, I want to be able to start a driver control with my battery voltages, and also have it start on that button press(it also needs to work in competition). Any help would be appreciated.
Driver:

#pragma config(Motor,  port2,           lfwMotor,      tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port4,           iMotor,        tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port5,            ,             tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port7,           rfwMotor,      tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port8,           rwMotor,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port9,           lwMotor,       tmotorVex393_MC29, openLoop, reversed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#pragma platform(VEX)

//Competition Control and Duration Settings
#pragma competitionControl(Competition)
#pragma autonomousDuration(20)
#pragma userControlDuration(120)

#include "Vex_Competition_Includes.c"   //Main competition background code...do not modify!
/////////////////////////////////////////////////////////////////////////////////////////
//
//                          Pre-Autonomous Functions
//
// You may want to perform some actions before the competition starts. Do them in the
// following function.
//
/////////////////////////////////////////////////////////////////////////////////////////

void pre_auton()
{
	bLCDBacklight = true;
  repeat(5)
  {
    displayLCDString(1,0,"Loading 8686K");
    	wait1Msec(500);
     displayLCDString(1,0,"Loading 8686K.");	
    		wait1Msec(500);
     displayLCDString(1,0,"Loading 8686K..");
     	wait1Msec(500);
     displayLCDString(1,0,"Loading 8686K...");
    wait1Msec(500);
    clearLCDLine(1);
  }
  
  // Set bStopTasksBetweenModes to false if you want to keep user created tasks running between
  // Autonomous and Tele-Op modes. You will need to manage all user created tasks if set to false.
  bStopTasksBetweenModes = true;

	// All activities that occur before the competition starts
	// Example: clearing encoders, setting servo positions, ...
}

/////////////////////////////////////////////////////////////////////////////////////////
//
//                                 Autonomous Task
//
// This task is used to control your robot during the autonomous phase of a VEX Competition.
// You must modify the code to add your own robot specific commands here.
//
/////////////////////////////////////////////////////////////////////////////////////////

task autonomous()
{
  // .....................................................................................
  // Insert user code here.
  // .....................................................................................

	AutonomousCodePlaceholderForTesting();  // Remove this function call once you have "real" code.
}

/////////////////////////////////////////////////////////////////////////////////////////
//
//                                 User Control Task
//
// This task is used to control your robot during the user control phase of a VEX Competition.
// You must modify the code to add your own robot specific commands here.
//
/////////////////////////////////////////////////////////////////////////////////////////

task usercontrol()
{
	// User control code here, inside the loop

	while (true)
	{
		bLCDBacklight = true;
			string mainBattery, backupBattery;

	while(true)														// An infinite loop to keep the program running until you terminate it
	{
		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, "Avg: ");
		sprintf(backupBattery, "%1.2f%c", nAvgBatteryLevel/1000.0, 'V');	//Build the value to be displayed
		displayNextLCDString(backupBattery);
{
		//Short delay for the LCD refresh rate
		motor[lwMotor] = vexRT[Ch3] / 2;
    motor[rwMotor] = vexRT[Ch2] / 2;
    if(vexRT[Btn6U] == 1)
    {
      motor[iMotor] = 127;
    }
    else if(vexRT[Btn6D] == 1)
    {
      motor[iMotor] = -127;
    }
    else
    {
      motor[iMotor] = 0;
    }

    if(vexRT[Btn5U] == 1)
    {
      motor[lfwMotor] = 0;
      motor[rfwMotor] = 0;
    }
    else if(vexRT[Btn5U] == 1)
    {
      motor[lfwMotor] = 0;
      motor[rfwMotor] = 0;
    }
    else
    {
      motor[lfwMotor] = 83;
      motor[rfwMotor] = 83;
 }}
	  // This is the main execution loop for the user control program. Each time through the loop
	  // your program should update motor + servo values based on feedback from the joysticks.

	  // .....................................................................................
	  // Insert user code here. This is where you use the joystick values to update your motors, etc.
	  // .....................................................................................
}}}

LCD:

#pragma config(Motor,  port3,           lfwBMotor,     tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port4,           rfwAMotor,     tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port5,           rfwBMotor,     tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port6,           iMotor,        tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port8,           lwMotor,       tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port9,           rwMotor,       tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#pragma platform(VEX)

//Competition Control and Duration Settings
#pragma competitionControl(Competition)
#pragma autonomousDuration(20)
#pragma userControlDuration(120)
const short leftButton = 1;
const short centerButton = 2;
const short rightButton = 4;
int count = 0;
//Wait for Press--------------------------------------------------
void waitForPress()
{
	while(nLCDButtons == 0){}
	wait1Msec(5);
}
//----------------------------------------------------------------

//Wait for Release------------------------------------------------
void waitForRelease()
{
	while(nLCDButtons != 0){}
	wait1Msec(5);
}
//----------------------------------------------------------------

#include "Vex_Competition_Includes.c"   //Main competition background code...do not modify!
void pre_auton()
{
	bLCDBacklight = true;
  bStopTasksBetweenModes = true;
  //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, "Blue Tile");
			displayLCDCenteredString(1, "<		 Select		>");
			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, "Red Tile");
			displayLCDCenteredString(1, "<		 Select		>");
			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, "Test Blue");
			displayLCDCenteredString(1, "<		 Select		>");
			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, "Autonomous 4");
			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;
		}
	//------------- End of User Interface Code ---------------------
}
}
task autonomous()
{
	//------------- Beginning of Robot Movement Code ---------------
	//Clear LCD
	clearLCDLine(0);
	clearLCDLine(1);
	//Switch Case that actually runs the user choice
	switch(count){
	case 0:
		//If count = 0, run the code correspoinding with choice 1
		displayLCDCenteredString(0, "Autonomous 1");
		displayLCDCenteredString(1, "is running!");
		wait1Msec(2000);						// Robot waits for 2000 milliseconds

		// Move forward at full power for 3 seconds
		break;
	case 1:
		//If count = 1, run the code correspoinding with choice 2
		displayLCDCenteredString(0, "Autonomous 2");
		displayLCDCenteredString(1, "is running!");
		wait1Msec(2000);						// Robot waits for 2000 milliseconds

		// Move reverse at full power for 3 seconds

		break;
	case 2:
		//If count = 2, run the code correspoinding with choice 3
		displayLCDCenteredString(0, "Autonomous 3");
		displayLCDCenteredString(1, "is running!");
		wait1Msec(2000);						// Robot waits for 2000 milliseconds

		//Turn right for 3 seconds

		break;
	case 3:
		//If count = 3, run the code correspoinding with choice 4
		displayLCDCenteredString(0, "Autonomous 4");
		displayLCDCenteredString(1, "is running!");
		wait1Msec(2000);						// Robot waits for 2000 milliseconds
SensorValue[dgtl1] = 1;
		//Turn left for 3 seconds

		break;
	default:
		displayLCDCenteredString(0, "No valid choice");
		displayLCDCenteredString(1, "was made!");
		break;

	//------------- End of Robot Movement Code -----------------------


	AutonomousCodePlaceholderForTesting();  // Remove this function call once you have "real" code.
}
}
task usercontrol()
{
	// User control code here, inside the loop

	while (true)
	{
			motor[lwMotor] = vexRT[Ch3] / 2;
    motor[rwMotor] = vexRT[Ch2] / 2;
    if(vexRT[Btn6U] == 1)
    {
      motor[iMotor] = 127;
    }
    else if(vexRT[Btn6D] == 1)
    {
      motor[iMotor] = -127;
    }
    else
    {
      motor[iMotor] = 0;
    }

    if(vexRT[Btn5U] == 1)
    {
      motor[lfwAMotor] = 0;
      motor[rfwAMotor] = 0;
      motor[lfwBMotor] = 0;
      motor[rfwBMotor] = 0;
    }
    else if(vexRT[Btn5U] == 1)
    {
      motor[lfwBMotor] = 0;
      motor[rfwBMotor] = 0;
      motor[lfwAMotor] = 0;
      motor[rfwAMotor] = 0;
    }
    else
    {
      motor[lfwAMotor] = 83;
      motor[rfwAMotor] = 83;
      motor[lfwBMotor] = 83;
      motor[rfwBMotor] = 83;
 }}

	  UserControlCodePlaceholderForTesting(); // Remove this function call once you have "real" code.
	}

Not exactly sure what you are trying to do but I will give it a shot.

You could make some functions out of the common tasks like the LCD battery voltage display and call it from user control or autonomous areas.

Once there, you will see the code looks smaller. You can move the functions to another file and #include that file. Stick it in the same directory as the current file so it knows where to load it from. Or leave it in one file, it does not matter that much.

Just remember the compiler is stupid is as stupid does - it reads from the top down so it needs to know about the function before you call it the first time. So define it or declare it up top.

Then once you have cleaned up the code into functions, you can see what you want to do more clearly. Just remember a loop in the function means nothing else gets done sicne control stays there. What you can do to alleviate that is you can set a variable first then call a set of functions to act on the variables.

Or you can look at background tasks. That will take those variables and act upon them in the tasks’ loops. But try cleaning up into functions first. One thing at a time.

I should probably reply before you try too hard, I figured it out last night. What I do need help with is making it so that if I press center button for more than say 5 seconds it will initiate that code, but if I press it for <5 seconds it just selects that for autonomous, or skills, I one of my options dedicated driver testing, aswell as with the battery display and other, I can post that here if you’d like, also sorry for not responding that I fixed it last night, I fell asleep.

Look for the button press, note the time it started in a variable. You could set a timer our use a variable. Your choice.

Now look for a button up. See how long the button was pressed and react accordingly.

Well, its kind of hard to do that, as it will automatically use the waitforpress function to set it up for autonomous and not continue with the sequence of code allowing it to be ran.

give me a second ill post it here.

#pragma config(Sensor, dgtl1,	 Bump,					 sensorTouch)
#pragma config(Motor,	 port2,						lfwMotor,			 tmotorVex393_MC29, openLoop)
#pragma config(Motor,	 port3,						rfwMotor,			 tmotorVex393_MC29, openLoop)
#pragma config(Motor,	 port4,						iMotor,				 tmotorVex393_MC29, openLoop)
#pragma config(Motor,	 port8,						rwMotor,			 tmotorVex393_MC29, openLoop)
#pragma config(Motor,	 port9,						lwMotor,			 tmotorVex393_MC29, openLoop, reversed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard							 !!*//

#pragma platform(VEX)

//Competition Control and Duration Settings
#pragma competitionControl(Competition)
#pragma autonomousDuration(20)
#pragma userControlDuration(120)
const short leftButton = 1;
const short centerButton = 2;
const short rightButton = 4;
int count = 0;
//Wait for Press--------------------------------------------------
void waitForPress()
{
	while(nLCDButtons == 0){}
	wait1Msec(5);
}
//----------------------------------------------------------------

//Wait for Release------------------------------------------------
void waitForRelease()
{
	while(nLCDButtons != 0){}
	wait1Msec(5);
}
//----------------------------------------------------------------
#include "Vex_Competition_Includes.c"		//Main competition background code...do not modify!

/////////////////////////////////////////////////////////////////////////////////////////
//
//													Pre-Autonomous Functions
//
// You may want to perform some actions before the competition starts. Do them in the
// following function.
//
/////////////////////////////////////////////////////////////////////////////////////////

void pre_auton()
{
	bLCDBacklight = true;
	repeat(5)
	{
		displayLCDString(1,0,"Loading 8686K");
		wait1Msec(500);
		displayLCDString(1,0,"Loading 8686K.");
		wait1Msec(500);
		displayLCDString(1,0,"Loading 8686K..");
		wait1Msec(500);
		displayLCDString(1,0,"Loading 8686K...");
		wait1Msec(500);
		clearLCDLine(1);
	}
	bStopTasksBetweenModes = true;
	//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, "Driver Control");
			displayLCDCenteredString(1, "< Start >");
			waitForPress();
			//Increment or decrement "count" based on button press
			if(nLCDButtons == leftButton)
			{
				waitForRelease();
				count = 3;
			}
			else if(nLCDButtons == rightButton)
			{
				waitForRelease();
				count++;
			}
			else if(centerButton)
			{
				bLCDBacklight = true;
				string mainBattery, backupBattery;

				while(true)														// An infinite loop to keep the program running until you terminate it
				{
					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, "Avg: ");
					sprintf(backupBattery, "%1.2f%c", nAvgBatteryLevel/1000.0, 'V');	//Build the value to be displayed
					displayNextLCDString(backupBattery);
					wait1Msec(5);
					{
						//Short delay for the LCD refresh rate
						motor[lwMotor] = vexRT[Ch3] / 2;
						motor[rwMotor] = vexRT[Ch2] / 2;
						if(vexRT[Btn6U] == 1)
						{
							motor[iMotor] = 127;
						}
						else if(vexRT[Btn6D] == 1)
						{
							motor[iMotor] = -127;
						}
						else
						{
							motor[iMotor] = 0;
						}
						if(vexRT[Btn5U] == 1)
						{
							motor[lfwMotor] = 0;
							motor[rfwMotor] = 0;
						}
						else if(vexRT[Btn5U] == 1)
						{
							motor[lfwMotor] = 0;
							motor[rfwMotor] = 0;
						}
						else
						{
							motor[lfwMotor] = 83;
							motor[rfwMotor] = 83;
			}}}}
			break;
		case 1:
			//Display second choice
			displayLCDCenteredString(0, "Skills");
			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, "Tile Left");
			displayLCDCenteredString(1, "< Enter >");
			waitForPress();
			if(nLCDButtons == leftButton)
			{
				waitForRelease();
				count--;
			}
			else if(nLCDButtons == rightButton)
			{
				waitForRelease();
				count++;
			}
			break;
		case 3: //Display fourth choice
			displayLCDCenteredString(0, "Tile Right");
			displayLCDCenteredString(1, "< Enter >");
			waitForPress();
			if(nLCDButtons == leftButton)
			{
				waitForRelease();
				count--;
			}
			else if(nLCDButtons == rightButton)
			{
				waitForRelease();
				count = 0;
			}
			break;
		default:
			count = 0;
			break;
	}}
	bStopTasksBetweenModes = true;
}
task autonomous()
{
	clearLCDLine(0);
	clearLCDLine(1);
	switch(count){
	case 0: //If count = 0, run the code correspoinding with choice 1
		displayLCDCenteredString(0, "Driver Control");
		displayLCDCenteredString(1, "no program!");
		wait1Msec(2000);						// Robot waits for 2000 milliseconds
		break;
	case 1: //If count = 1, run the code correspoinding with choice 2
		displayLCDCenteredString(0, "Skills");
		displayLCDCenteredString(1, "is selected!");
		wait1Msec(2000);						// Robot waits for 2000 milliseconds
		break;
	case 2: //If count = 2, run the code correspoinding with choice 3
		displayLCDCenteredString(0, "Tile Left");
		displayLCDCenteredString(1, "is selected!");
		wait1Msec(2000);						// Robot waits for 2000 milliseconds
		break;
	case 3: //If count = 3, run the code correspoinding with choice 4
		displayLCDCenteredString(0, "Tile Right");
		displayLCDCenteredString(1, "is selected!");
		wait1Msec(2000);						// Robot waits for 2000 milliseconds
		break;
	default:
		displayLCDCenteredString(0, "No Response");
		displayLCDCenteredString(1, "(Default)");
		break;
		AutonomousCodePlaceholderForTesting();	// Remove this function call once you have "real" code.
}}
task usercontrol()
{
		string mainBattery, backupBattery;
	while (true)
	{
		clearLCDLine(0);											// Clear line 1 (0) of the LCD
					clearLCDLine(1);											// Clear line 2 (1) of the LCD
					displayLCDString(0, 0, "Primary: ");
					sprintf(mainBattery, "%1.2f%c", nImmediateBatteryLevel/1000.0,'V'); //Build the value to be displayed
					displayNextLCDString(mainBattery);
					displayLCDString(1, 0, "Avg: ");
					sprintf(backupBattery, "%1.2f%c", nAvgBatteryLevel/1000.0, 'V');	//Build the value to be displayed
					displayNextLCDString(backupBattery);
					wait1Msec(1);
					{
						motor[lwMotor] = vexRT[Ch3] / 2;
						motor[rwMotor] = vexRT[Ch2] / 2;
						if(vexRT[Btn6U] == 1)
						{
							motor[iMotor] = 127;
						}
						else if(vexRT[Btn6D] == 1)
						{
							motor[iMotor] = -127;
						}
						else
						{
							motor[iMotor] = 0;
						}
						if(vexRT[Btn5U] == 1)
						{
							motor[lfwMotor] = 0;
							motor[rfwMotor] = 0;
						}
						else if(vexRT[Btn5U] == 1)
						{
							motor[lfwMotor] = 0;
							motor[rfwMotor] = 0;
						}
						else
						{
							motor[lfwMotor] = 83;
							motor[rfwMotor] = 83;
			}
		UserControlCodePlaceholderForTesting(); // Remove this function call once you have "real" code.
}}}

is there a way to make an if(centerButton) = true for more than 5 seconds have it turn on the code, but if its just a press and is released in less than 5 seconds it chooses the code for the autonomous portion of the match?

what about an if(centerButton) and a wait 5 second, and then have the waitforpress in the else? or would that just always have it for the functions way if it is pressed?

Ok, I have the wait 5 seconds and then activate code function, I just made this:


if)(centerButton)
{
motor[iMotor] = 127; //this is my intake motor turning on
}}
else
{
waitForPress();
}

the only thing that doesn’t work is the just setting the count for the autonomous selection.

im making a dedicated thread for this because this is kinda off topic

Don’t wait for a press, look to see if a press happened and react on the next loop around. Otherwise whatever the previous command for the motors were on everything else will continue to happen (like driving or intakes or whatever was previously turned on)

Have a look at some of the code in this thread, it may give you some ideas.
https://vexforum.com/t/robotc-lcd-autonomous-selection/24818/1

Also, when detecting short/long button presses it’s often the release of the button that triggers the action, not the press. Same is true when using a mouse, try that with the “Post a Reply” button below, notice how it’s the mouse release that triggers the action not the mouse press.