Protobot program help!!! Asap!!!!

Hi I built the Vex robotics Protobot using the cortex. I have the arm programmed to move up and down with buttons and the driving and the front wheels all programmed with the Joystick. But I have limit switches to limit the arm so it doesn’t go to far. How do I program the limit switches to work with the Joystick code so it the arm hits the limit switch it goes back and stops so I can try again? I have attached the code.

#pragma config(Sensor, dgtl1, limitswitch1, sensorTouch)
#pragma config(Sensor, dgtl2, limitswitch2, sensorTouch)
#pragma config(Motor, port3, rightmotor, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port4, Lift, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port6, leftmotor, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port7, Grab, tmotorVex393_MC29, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

/*
Project Title: PROTOBOT
Team Members: John
Date:
Section:

Task Description: PROTOBOT

Pseudocode:

*/

task main()
{ //Program begins, insert code within curly braces

while (true)
{
motor(leftmotor) = vexRT[Ch3];
motor(rightmotor) = vexRT[Ch2];

	if (vexRT [Btn5D] ==1)
		stopAllTasks();

		if (vexRT [Btn6D] ==1)
			stopAllTasks();

			if (vexRT [Btn6U] ==1)
		stopMotor(Lift);

		if (vexRT [Btn5U] ==1)
			stopMotor(Grab);

			if (vexRT [Btn7U] ==1)
				motor(Grab) = 100;

				if (vexRT [Btn7D] ==1)
					motor(Grab) = -100;

if (vexRT [Btn8U] ==1)
{
motor[Lift] = 140;
}

else    if (vexRT [Btn8D] ==1)
{
    motor[Lift] = -140;
}

else
{
    motor[Lift] = 0; //If no button is pressed, stop motor
}

}
}

For this particular question, please see the ‘Remote Control -> Buttons -> Controlling The Arm’ section of the VEX Cortex Video Trainer using ROBOTC as it covers the specific type of programming (multiple conditions in a control structure) that you will need to stop the robot’s arm with a limit switch (while still having joystick control).

VEX Cortex Video Trainer using ROBOTC: http://www.education.rec.ri.cmu.edu/products/teaching_robotc_cortex/index.html

In the future, please wrap your code in [noparse]




[/noparse] tags so that it’s formatted correctly.

I’m pretty sure I understand what you’re asking for, but for me to reiterate what you’re asking: you want to be able to move your arm up/down until a limit switch that signals the top/bottom range of motion on the lift is hit. When that limit switch is hit, the arm can either be stopped or move down, which will prevent the robot from accidentally breaking itself.

Normally, I would set up a function that wraps the lift set function and that’s where I would have my final fail safe that you have described, but for simplicity’s sake, you can just use a large if/else if/else branch.

Just replace your lift control with the following code:

if (SensorValue[limitswitch1] == 1)
{ // Assuming limitswitch1 is the top limit switch, flip around as needed
	if (vexRT[Btn8D] == 1) // Want to go down when all the way up - OK
		motor[Lift] = -127;
	else
		motor[Lift] = 0;
}
else if (SensorValue[limitswitch2] == 1)
{ // Only way for lift to move when lift is all the way down is up or stopped
	if (vexRT[Btn8U] == 1)
		motor[Lift] = 127;
	else 
		motor[Lift] = 0;
}
else
{ // Neither limit switch is pressed, operate normally
	if (vexRT[Btn8U] == 1)
		motor[Lift] = 127;
	else if (vexRT[Btn8D] == 1)
		motor[Lift] = -127;
	else
		motor[Lift] = 0;
}

I don’t know what the official ROBOTC trainer has you do, but I’d be willing to be that does a better job explaining that I just did. Also, my ROBOTC calls are a little rusty, but I’m pretty sure I called everything correctly.

To prevent making you watch through all of the videos for the answer to this one: the ROBOTC Video Trainer introduces multiple conditional statements in control statements through the use of the boolean AND and OR operators. For instance, if you want to check to see if a touch sensor is not pressed AND an encoder has not reached a target value:

	//Clear the left motor's encoder to 0
	nMotorEncoder[leftMotor] = 0;

	/*
	While the left motor has not reached its target value
	AND the touch sensor is not pressed, the  loop will continue iterating.
	It will continue looping while both the left side of the condition
	(encoder target is not reached) AND the right side of the condition
	(touch sensor not pressed) equates to true. As soon as either side
	equates to false, the loop exits.
	*/
	while(nMotorEncoder[leftMotor] < 1000 && sensorValue(touchSensor) ==0)
	{
		//Keep moving forward
		motor[leftMotor] = 50;
		motor[rightMotor] = 50;

	}

	//Once the loop exits, stop the motors
	motor[leftMotor] = 0;
	motor[rightMotor] = 0;

There’s a bit more to it that the Video Trainer covers, but essentially using multi-conditional statements can help eliminate nesting if/else statements too deep and allows you to check for multiple conditions in one control statement.

When I downloaded the code and tried it when the limitswitch1 was hit nothing happened??? I want when limitswitch 1 is hit for the lift motor to do -30 for 2 senconds then stop so I can control it agian same with limitswitch2 just positive power supply.

I tried this code and it worked but when I did all the commands on the joystick they lagged???

#pragma config(Sensor, dgtl1, limitswitch1, sensorTouch)
#pragma config(Sensor, dgtl2, limitswitch2, sensorTouch)
#pragma config(Motor, port3, rightmotor, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port4, Lift, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port6, leftmotor, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port7, Grab, tmotorVex393_MC29, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

/*
Project Title: PROTOBOT
Team Members: John
Date:
Section:

Task Description: PROTOBOT

Pseudocode:

*/

task main()
{ //Program begins, insert code within curly braces

while (true)
{
motor(leftmotor) = vexRT[Ch3];
motor(rightmotor) = vexRT[Ch2];

	if (vexRT [Btn5D] ==1)
		stopAllTasks();

		if (vexRT [Btn6D] ==1)
			stopAllTasks();

			if (vexRT [Btn6U] ==1)
		stopMotor(Lift);

		if (vexRT [Btn5U] ==1)
			stopMotor(Grab);

			if (vexRT [Btn7U] ==1)
				motor(Grab) = 100;

				if (vexRT [Btn7D] ==1)
					motor(Grab) = -100;

if (SensorValue[limitswitch1] == 1)
startMotor(Lift, -40);
wait(1.5);
stopMotor(Lift);

{ // Assuming limitswitch1 is the top limit switch, flip around as needed
if (vexRT[Btn8D] == 1) // Want to go down when all the way up - OK
motor[Lift] = -127;
else
motor[Lift] = 0;
}
if (SensorValue[limitswitch2] == 1)
startMotor(Lift, 40);
wait(1.5);
stopMotor(Lift);
{ // Only way for lift to move when lift is all the way down is up or stopped
if (vexRT[Btn8U] == 1)
motor[Lift] = 127;
else
motor[Lift] = 0;
}

{ // Neither limit switch is pressed, operate normally
if (vexRT[Btn8U] == 1)
motor[Lift] = 127;
else if (vexRT[Btn8D] == 1)
motor[Lift] = -127;
else
motor[Lift] = 0;
}

}
}

I’m not sure what you’re trying to do, but your coding looks a little suspicious to me.

For example, in the following:


if (SensorValue[limitswitch1] == 1)
startMotor(Lift, -40);
wait(1.5);
stopMotor(Lift);

I’m going to take a wild guess that what you really want to do is this:


if (SensorValue[limitswitch1] == 1)
{
startMotor(Lift, -40);
wait(1.5);
stopMotor(Lift);
}

In the second example, when limitswitch1 gets pressed, you run startMotor(Lift, -40); then you wait 1.5 seconds, then you run stopMotor(Lift);.

In the first example, I think you will wait 1.5 seconds and run stopMotor(Lift); even when you don’t press the limitswitch1.

I didn’t even know you could use something like wait(1.5); Is that an actual RobotC thing?

Yes it is what is does it that startmotor then it wait for 1.5 seconds then it stops the motor.

…umm… uhhhh… :confused:

Do you want it to do that even when you don’t press the limitswitch1 ???

I want it to do nothing.

Is this the sort of control you’re looking for?


If the lift is bottomed out:
     Lift up for 2.5 seconds
If the lift is topped out:
     Lift down for 2.5 seconds  
Otherwise:
      Always have up/down/stop control of the lift

Awaiting confirmation.

Guys Watch these videos and it helps!!!
Thanks for all your guys help. I go it to work so when The limitswitch is hit it stops the arm from going down anyfather and same with the other limit switch
!!!

Code-

#pragma config(Sensor, dgtl1, limitswitch1, sensorTouch)
#pragma config(Sensor, dgtl2, limitswitch2, sensorTouch)
#pragma config(Motor, port3, rightmotor, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port4, Lift, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port6, leftmotor, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port7, Grab, tmotorVex393_MC29, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

/*
Project Title: PROTOBOT
Team Members: John
Date:
Section:

Task Description: PROTOBOT

Pseudocode:

*/

task main()
{ //Program begins, insert code within curly braces

while (true)
{
motor(leftmotor) = vexRT[Ch3];
motor(rightmotor) = vexRT[Ch2];

	if (vexRT [Btn5D] ==1)
		stopAllTasks();

		if (vexRT [Btn6D] ==1)
			stopAllTasks();

			if (vexRT [Btn6U] ==1)
		stopMotor(Lift);

		if (vexRT [Btn5U] ==1)
			stopMotor(Grab);

			if (vexRT [Btn7U] ==1)
				motor(Grab) = 100;

				if (vexRT [Btn7D] ==1)
					motor(Grab) = -100;

					
				if (vexRT [Btn8U] ==1 && SensorValue(limitswitch1) ==0)
				{ 
					motor[Lift] = 100;
				}
				else if (vexRT[Btn8D] ==1 && SensorValue(limitswitch2) ==0)
				{
					motor(Lift) = -100;
				}
				else
				{
					motor(Lift) = 0;
					{

}
}
}
}