Right now I have my competition robot so the arm raises until it hits a physical object which stops it. This works fine but I would like to have a limit switch there so when it hits the object and by proxy the switch it keeps the motors at a constant level of 75 (keeping the arm in the upward position) until I press the down button.
Some sort of until statement that reacts with the limit switch (arm_limit)
{
Keeps the motors at 75
}
Until the down button is pressed, then it sends the arm down.
I know it sounds confusing and for the most part that is why I am posting this. This is my first competition and I am still learning the ropes of RobotC. (Good thing I am a quick learner!)
To detect the switch you will use SensorValue], seems like you could just use an or statement with the up button. something like (pseude code…)
while(1)
{
if( (up_button_pressed OR switch_closed) AND NOT down_button_pressed)
{
drive motors up
}
if( down_button_pressed )
{
drive motors down
}
wait( a few mS )
}
It really depends on how you want the buttons to work, push and hold or latched where you can release the button once it is pushed.
Well the point of it is if I want to move it up and down just a random amount I can, yet if I bring it all the way to the top (tripping the switch) it will keep the motors going at a constant 75 so the arm stays up until I press the down button.
So the arm with freely raise and lower with the up and down buttons yet the second the arms is raised all the way up and the switch is tripped then the motors will lock at 75, until I hit the down button.
The code you gave me would one allow the arm to be raised and lowered, not have it stay at the top.
jpearman’s code should work, but it has a bias towards moving up - the motors will raise the arm if no button is pressed (even if the last button pressed was “down”). Once the limit switch is pressed, the arm should fall (if it’s heavy), then the limit switch is no longer pressed, and the arm continues up again, etc.
Maybe use a boolean flag? Ex:
bool raiseArm = false;
if (upButton == Pressed)
raiseArm = true;
else if (downButton == Pressed)
raiseArm = false;
if (raiseArm & !LimitPressed)
Drive arm up
else if (downButton == Pressed)
Lower arm
Well it was just pseudo code, the down switch being pressed would always override the up switch due to the AND NOT down_button_pressed in the first conditional statement. Neither of us handled the motor stop condition but I assumed the OP understood how to do that, anyway, here’s some real code that was tested. The assumption is that as soon as the arm moves down the limit switch will open. Also assumes that switch closed gives digital 0, ie. a normally open switch.
#pragma config(Sensor, dgtl1, limit_switch, sensorDigitalIn)
#pragma config(Motor, port10, arm_motor, tmotorNormal, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
#define PRESSED 1
#define NOT_PRESSED 0
#define ARM_STOP_SPEED 0
#define ARM_UP_SPEED 75
#define ARM_DOWN_SPEED -75
#define LIMIT_SWITCH_TRIPPED 0
task main()
{
int motor_speed = 0;
while(1)
{
// If arm down requested
if( vexRT[Btn5D] == PRESSED )
motor_speed = ARM_DOWN_SPEED;
else
// if arm up requested or the arm has tripped the limit switch
if( (vexRT[Btn5U] == PRESSED ) || (SensorValue limit_switch ] == LIMIT_SWITCH_TRIPPED))
motor_speed = ARM_UP_SPEED;
else
motor_speed = ARM_STOP_SPEED;
// Set arm motor
motor arm_motor ] = motor_speed;
// Do other things
// Don't hog CPU
wait1Msec(25);
}
}
And also, that code should work if your lift has enough torque to keep it up without any power. You should put rubber bands on your arm if your arm falls down when you lift it manually without turning the robot on.