ROBOTC Help

//Funtions
void DriveForward(int speed, int time)
{
	motor[RightFront] = speed;
	motor[RightBack] = speed;
	motor[LeftFront] = speed;
	motor[LeftBack] = speed;
	wait1Msec(time);
	motor[RightFront] = 0;
	motor[RightBack] = 0;
	motor[LeftFront] = 0;
	motor[LeftBack] = 0;

}

I made this function to use later on, but I get an error when I try to use it. What’s wrong?

Thanks!

Also…

task autonomous()
{
	while(SensorValue(LeftButton) == 0)
		{
		 //DriveStraight(127, 1000);
	  }

	  if(SensorValue(LeftButton) == 1) //Pick up bonus sack under trough with object and score in trough
	  	{
	  	motor[RightFront] = -127;
			motor[RightBack] = -127;
			motor[LeftFront] = -127;
			motor[LeftBack] = -127;
			wait1Msec(100);
			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;

			wait1Msec(1000);

			motor[IntakeLeft] = 127;
			motor[IntakeRight] = 127;

			motor[RightFront] = 127; //Drives forward full power
			motor[RightBack] = 127;
			motor[LeftFront] = 127;
			motor[LeftBack] = 127;

			wait1Msec(2500); //Drives forward at full power for 2 sec.

			motor[RightFront] = -127;
			motor[RightBack] = -127;
			motor[LeftFront] = -127;
			motor[LeftBack] = -127;

			wait1Msec(1000);

			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;

			motor[LiftLeft] = 127;
			motor[LiftRight] = 127;
			wait1Msec(1750);
			motor[LiftLeft] = 0;
			motor[LiftRight] = 0;

			motor[RightFront] = 127;
			motor[RightBack] = 127;
			motor[LeftFront] = 127;
			motor[LeftBack] = 127;

			wait1Msec(425);

			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;

			motor[IntakeLeft] = -127;
			motor[IntakeRight] = -127;
			wait1Msec(1000);
			motor[IntakeLeft] = 0;
			motor[IntakeRight] = 0;

			motor[LiftLeft] = 127;
			motor[LiftRight] = 127;
			wait1Msec(750);
			motor[LiftLeft] = 0;
			motor[LiftRight] = 0;

			motor[RightFront] = -127;
			motor[RightBack] = -127;
			motor[LeftFront] = -127;
			motor[LeftBack] = -127;

			wait1Msec(1200);

			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;

			motor[LiftLeft] = -127;
			motor[LiftRight] = -127;
		  wait1Msec(100);
		  motor[LiftLeft] = 0;
			motor[LiftRight] = 0;
		}

	else if(SensorValue(RightButton) == 0)
		{
			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;
		}

	else if(SensorValue(RightButton) == 1) //Score bonus sack in high goal
		{
			motor[IntakeLeft] = 127;
			motor[IntakeRight] = 127;

			motor[RightFront] = 127; //Drives forward full power
			motor[RightBack] = 127;
			motor[LeftFront] = 127;
			motor[LeftBack] = 127;
			wait1Msec(2200);
			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;

			motor[RightFront] = -63;
			motor[RightBack] = -63;
			motor[LeftFront] = -63;
			motor[LeftBack] = -63;
			wait1Msec(500);
			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;

			motor[RightBack] = 127;
			motor[RightFront] = 127;
			wait1Msec(500);
			motor[RightBack] = 0;
			motor[RightFront] = 0;

			motor[SWLeft] = 127;
			motor[SWRight] = 127;
			wait1Msec(400);
			motor[SWLeft] = 0;
			motor[SWRight] = 0;

			motor[RightFront] = -100;
			motor[RightBack] = -100;
			motor[LeftFront] = -100;
			motor[LeftBack] = -100;
			wait1Msec(100);
			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;

			motor[LiftLeft] = 127;
			motor[LiftRight] = 127;
		  wait1Msec(2050);
		  motor[LiftLeft] = 0;
			motor[LiftRight] = 0;

			motor[RightBack] = 0;
			motor[RightFront] = 0;
			wait1Msec(250);
			motor[RightBack] = 0;
			motor[RightFront] = 0;


			motor[RightFront] = 100;
			motor[RightBack] = 100;
			motor[LeftFront] = 100;
			motor[LeftBack] = 100;
			wait1Msec(1500);
			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;

			motor[IntakeLeft] = -127;
			motor[IntakeRight] = -127;

			wait1Msec(500);

			motor[RightFront] = -127;
			motor[RightBack] = -127;
			motor[LeftFront] = -127;
			motor[LeftBack] = -127;
			wait1Msec(1000);
			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;
		}
	}

I want to be able to use two different autonomous routes in a match. It’s set up know to sit until I push the left button, then it will run that code. It will not let me push the RightButton to start the code. What is the problem? Why is it not letting me use the Rightbutton to start the code?

There is nothing wrong with the function. However, it depends on where you have created it in your code. If the code is of this general form.

task main()
{
    while(1)
        {
        // do stuff
        DriveForward(0, 0);
        // do more stuff
        }
}

void DriveForward(int speed, int time)
{
	motor[RightFront] = speed;
	motor[RightBack] = speed;
	motor[LeftFront] = speed;
	motor[LeftBack] = speed;
	wait1Msec(time);
	motor[RightFront] = 0;
	motor[RightBack] = 0;
	motor[LeftFront] = 0;
	motor[LeftBack] = 0;
}

You will get this error

**Error**:Undefined procedure 'DriveForward'.
**Error**:Calling procedure 'DriveForward'. Too many parameters specified. Parameter: 'N/A'. Expression: '0'. Type: 'char'

This is because when you reach the DriveForward call in the main task it does not know what the parameters to DriveForward should be. So, either create the function above main in the file or use a prototype.

Does that help?

Because this…

	while(SensorValue(LeftButton) == 0)
		{
		 //DriveStraight(127, 1000);
	  }

sits waiting for the left button.

So where do I need to put it for it to work?

Define the function before you call it, in the case of the example I posted, put it before the main task. If you want to call from autonomous, put it before that. Better still, put all your standard functions in another source file and include that at the beginning of the file where you want to use them.

How do I get it to not wait for one to be pressed, but for it to wait for any of the buttons to be pressed.

That can be your homework for today.

Alright, I will make a new file for the funtions.

I do have it above where I’m calling it. It is right below the main motors and sensors set-up thing. It is still giving me errors.

I’ll try, but I am still confused on how to program well.

Thanks for your help so far!

Here is an example where I include a file called “SmartMotorLib.c” (it can be called anything) at the top of a competition template, this just an excerp from the code, include line is highlighted in red.

#pragma config(Motor,  port3,  MotorLF,   tmotorVex393, openLoop,           encoder, encoderPort, I2C_2, 1000)
#pragma config(Motor,  port8,  MotorLB,   tmotorVex393, openLoop,           encoder, encoderPort, I2C_3, 1000)
#pragma config(Motor,  port9,  MotorRB,   tmotorVex393, openLoop, reversed, encoder, encoderPort, I2C_4, 1000)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "SmartMotorLib.c"

#pragma platform(VEX)

//Competition Control and Duration Settings
#pragma competitionControl(Competition)
#pragma autonomousDuration(15)
#pragma userControlDuration(105)

#include "Vex_Competition_Includes.c"   //Main competition background code...do not modify!

/////////////////////////////////////////////////////////////////////////////////////////
//
//                          Pre-Autonomous Functions
//

Here’s a hint in pseudo code.


While( (the left button in not pressed) AND (the right button is not pressed) )
    wait for a button press

if( the left button is pressed )
    do an autonomous
else
if( the right button is pressed )
    do some other autonomous
else
    why am I even here ?

I did that, and it still does not except it. I did

#include “Functions.c”

Still nothing.

Can I get a hint on how to get the code to watch for both buttons? Is it an else, if, or while statement?

Oh, ok, that makes sense. THANK you Mr. Jpearman!

What is the AND sign? || or &&?

	while(SensorValue(LeftButton) == 0) && (SensorValue(RightButton) == 0);
		{
			//be lazy
	  }
		

I have this, but this error comes up when I compile it:

Check your parentheses.

Post the error you are getting, we’ve never seen it.

It still has the error. Here is all of the code

#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, in1,    LeftPot,        sensorPotentiometer)
#pragma config(Sensor, in2,    RightPot,       sensorPotentiometer)
#pragma config(Sensor, dgtl7,  RightButton,    sensorTouch)
#pragma config(Sensor, dgtl8,  LeftButton,     sensorTouch)
#pragma config(Sensor, dgtl10, OARight,        sensorTouch)
#pragma config(Sensor, dgtl11, DALeft,         sensorTouch)
#pragma config(Sensor, dgtl12, DARight,        sensorTouch)
#pragma config(Sensor, I2C_1,  ,               sensorQuadEncoderOnI2CPort,    , AutoAssign)
#pragma config(Sensor, I2C_2,  ,               sensorQuadEncoderOnI2CPort,    , AutoAssign)
#pragma config(Sensor, I2C_3,  ,               sensorQuadEncoderOnI2CPort,    , AutoAssign)
#pragma config(Sensor, I2C_4,  ,               sensorQuadEncoderOnI2CPort,    , AutoAssign)
#pragma config(Motor,  port1,           RightFront,    tmotorVex393, openLoop)
#pragma config(Motor,  port2,           RightBack,     tmotorVex393, openLoop, reversed, encoder, encoderPort, I2C_1, 1000)
#pragma config(Motor,  port3,           IntakeLeft,    tmotorVex393, openLoop)
#pragma config(Motor,  port4,           IntakeRight,   tmotorVex393, openLoop)
#pragma config(Motor,  port5,           SWLeft,        tmotorVex393, openLoop, encoder, encoderPort, I2C_3, 1000)
#pragma config(Motor,  port6,           SWRight,       tmotorVex393, openLoop, encoder, encoderPort, I2C_2, 1000)
#pragma config(Motor,  port7,           LiftRight,     tmotorVex393, openLoop, reversed)
#pragma config(Motor,  port8,           LiftLeft,      tmotorVex393, openLoop, reversed)
#pragma config(Motor,  port9,           LeftFront,     tmotorVex393, openLoop, reversed)
#pragma config(Motor,  port10,          LeftBack,      tmotorVex393, openLoop, encoder, encoderPort, I2C_4, 1000)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#pragma platform(VEX)

//Competition Control and Duration Settings
#pragma competitionControl(Competition)
#pragma autonomousDuration(15) //Autonomous is 15 seconds long
#pragma userControlDuration(105) //Driver control is 105 (1min 45sec) seconds long


#include "Vex_Competition_Includes.c"   //Main competition background code...do not modify!
#include "Functions.c"



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

void pre_auton()
{
  // 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, ...
  nMotorEncoder[RightBack] = 0;
	nMotorEncoder[LeftBack] = 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
//
//                                 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()
{
	while(SensorValue(LeftButton) == 0) && (SensorValue(RightButton) == 0);
			{
				//be lazy
	 		}
		
	if(SensorValue(LeftButton) == 1) //Pick up bonus sack under trough with object and score in trough
	 {
	  	motor[RightFront] = -127;
			motor[RightBack] = -127;
			motor[LeftFront] = -127;
			motor[LeftBack] = -127;
			wait1Msec(100);
			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;

			wait1Msec(1000);

			motor[IntakeLeft] = 127;
			motor[IntakeRight] = 127;

			motor[RightFront] = 127; //Drives forward full power
			motor[RightBack] = 127;
			motor[LeftFront] = 127;
			motor[LeftBack] = 127;

			wait1Msec(2500); //Drives forward at full power for 2 sec.

			motor[RightFront] = -127;
			motor[RightBack] = -127;
			motor[LeftFront] = -127;
			motor[LeftBack] = -127;

			wait1Msec(1000);

			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;

			motor[LiftLeft] = 127;
			motor[LiftRight] = 127;
			wait1Msec(1750);
			motor[LiftLeft] = 0;
			motor[LiftRight] = 0;

			motor[RightFront] = 127;
			motor[RightBack] = 127;
			motor[LeftFront] = 127;
			motor[LeftBack] = 127;

			wait1Msec(425);

			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;

			motor[IntakeLeft] = -127;
			motor[IntakeRight] = -127;
			wait1Msec(1000);
			motor[IntakeLeft] = 0;
			motor[IntakeRight] = 0;

			motor[LiftLeft] = 127;
			motor[LiftRight] = 127;
			wait1Msec(750);
			motor[LiftLeft] = 0;
			motor[LiftRight] = 0;

			motor[RightFront] = -127;
			motor[RightBack] = -127;
			motor[LeftFront] = -127;
			motor[LeftBack] = -127;

			wait1Msec(1200);

			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;

			motor[LiftLeft] = -127;
			motor[LiftRight] = -127;
		  wait1Msec(100);
		  motor[LiftLeft] = 0;
			motor[LiftRight] = 0;
		}
		
		else
			
	  if(SensorValue(RightButton) == 1) //Score bonus sack in high goal
		{
			motor[IntakeLeft] = 127;
			motor[IntakeRight] = 127;

			motor[RightFront] = 127; //Drives forward full power
			motor[RightBack] = 127;
			motor[LeftFront] = 127;
			motor[LeftBack] = 127;
			wait1Msec(2200);
			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;

			motor[RightFront] = -63;
			motor[RightBack] = -63;
			motor[LeftFront] = -63;
			motor[LeftBack] = -63;
			wait1Msec(500);
			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;

			motor[RightBack] = 127;
			motor[RightFront] = 127;
			wait1Msec(500);
			motor[RightBack] = 0;
			motor[RightFront] = 0;

			motor[SWLeft] = 127;
			motor[SWRight] = 127;
			wait1Msec(400);
			motor[SWLeft] = 0;
			motor[SWRight] = 0;

			motor[RightFront] = -100;
			motor[RightBack] = -100;
			motor[LeftFront] = -100;
			motor[LeftBack] = -100;
			wait1Msec(100);
			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;

			motor[LiftLeft] = 127;
			motor[LiftRight] = 127;
		  wait1Msec(2050);
		  motor[LiftLeft] = 0;
			motor[LiftRight] = 0;

			motor[RightBack] = 0;
			motor[RightFront] = 0;
			wait1Msec(250);
			motor[RightBack] = 0;
			motor[RightFront] = 0;


			motor[RightFront] = 100;
			motor[RightBack] = 100;
			motor[LeftFront] = 100;
			motor[LeftBack] = 100;
			wait1Msec(1500);
			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;

			motor[IntakeLeft] = -127;
			motor[IntakeRight] = -127;

			wait1Msec(500);

			motor[RightFront] = -127;
			motor[RightBack] = -127;
			motor[LeftFront] = -127;
			motor[LeftBack] = -127;
			wait1Msec(1000);
			motor[RightFront] = 0;
			motor[RightBack] = 0;
			motor[LeftFront] = 0;
			motor[LeftBack] = 0;
		}
	}

/////////////////////////////////////////////////////////////////////////////////////////
//
//                                 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)
	{
    motor[LeftFront] = vexRT[Ch3]; //Left joystick drives left side
    motor[LeftBack] = vexRT[Ch3]; //Left joystick drives left side
    motor[RightFront] = vexRT[Ch2]; //Right joystick drives left side
    motor[RightBack] = vexRT[Ch2]; //Right joystick drives left side
    motor[SWLeft] = (vexRT[Btn6U] - vexRT[Btn5U])*127; //Left/Right top/back buttons strafe left/right
    motor[SWRight] = (vexRT[Btn6U] - vexRT[Btn5U])*127; //Left/Right top/back buttons strafe left/right
    motor[LiftLeft] = (vexRT[Btn7UXmtr2] - vexRT[Btn7DXmtr2])*127;
    motor[LiftRight] = (vexRT[Btn7UXmtr2] - vexRT[Btn7DXmtr2])*127;
    motor[IntakeLeft] = (vexRT[Btn8DXmtr2] - vexRT[Btn8UXmtr2])*127;
    motor[IntakeRight] = (vexRT[Btn8DXmtr2] - vexRT[Btn8UXmtr2])*127;
  }
}

About the function error. Alright, never mind. I just forgot to space out the ( ). Everything’s fine with that.

Does anyone know why this code:

while(SensorValue(LeftButton) == 0) && (SensorValue(RightButton) == 0);
			{
				//be lazy
	 		}

is not working?

Jesse

I said already “check your parenthesis”

This is how it should be

while( (SensorValue(LeftButton) == 0) && (SensorValue(RightButton) == 0) )
    {
    // Don't hog the CPU
    wait1Msec( 2 );
    }

The compiler thought your code was as follows.


while(SensorValue(LeftButton) == 0)
    && (SensorValue(RightButton) == 0);
        {
        //be lazy
	}

It though the && was the beginning of a new statement when it matched the first “(” with the “)” after 0.

Sorry about that, I was thinking these { }. I didn’t check the ( ). It all works now.

How would I get it so say if the jumper was in port 3, the autonomous would be for the LEFT side of the field, and when the jumper is port 4, it would be for the RIGHT side. How would I accomplish this? I have already tried to think through it, but I got nothing.

Thank you SO much for your help so far!

At the beginning of the “autonomous” task, you could do a simple check to see which port has a jumper in it, and set a variable accordingly. For example:

int start_side = 0;
const int left = 1;
const int right = 2;

task autonomous()
{
  if(SensorValue[in3] = 1)
    start_side = left;
  else if(SensorValue[in4] = 1)
    start_side = right;

  // Autonomous using "start_side" value to determine which way to turn/exactly what to do.
}

~Jordan