Button.pressed command

I’m currently trying to use the button pressed command to make my elevator move to a certain height on its own. I made some code, it did absolutely nothing, and I’m not sure why. If anyone would be able to take a look at what I’ve done and let me know if they see anything wrong with it that would be very much appreciated. Here’s what I’ve tried, not including where I defined the motors or the rest of my code for other things:

void elevatorheight1() {
elevatorMotor.rotateTo(-500, rotationUnits::deg, 80, velocityUnits::rpm);
elevatorMotor2.rotateTo(-500, rotationUnits::deg, 80, velocityUnits::rpm);
vex::task::sleep(1);
}

void usercontrol( void ) {
Controller1.ButtonUp.pressed(elevatorheight1);
}

I think your problem is that usercontrol() exits the program. Try this:

void elevatorheight1() {
  elevatorMotor.rotateTo(-500, rotationUnits::deg, 80, velocityUnits::rpm);
  elevatorMotor2.rotateTo(-500, rotationUnits::deg, 80, velocityUnits::rpm);
}

void usercontrol( void ) {
  while(true) {
     Controller1.ButtonUp.pressed(elevatorheight1);
     vex::task::sleep(1);
  }
}
1 Like

Yes, I forgot to include it here but I did have while true in my code. I also believe toward the bottom of user control the sleep task is automatically in there as well.

Does the elevator work if you controlled it manually? The code looks fine to me. Would need to see the entire code to be able to find whether it’s code or something else causing the problem.

Elevator works perfectly fine manually. Pressed the up button nothing happens

Try putting
elevatorMotor.rotateTo(-500, rotationUnits::deg, 80, velocityUnits::rpm);
elevatorMotor2.rotateTo(-500, rotationUnits::deg, 80, velocityUnits::rpm);
in place of the manual code and when you click the button, check the device manager on the brain, find the elevator motor and see what number is under command

1 Like

What do you mean by manually by hand? Trying running the default driver program and moving the motor for the elevator to see if it lifts there may not be enough torque.

Try:

void usercontrol( void ) 
{
    while(1)
    {
      if(Controller1.ButtonUp.pressing())
      {
        elevatorMotor.startRotateTo(-500, rotationUnits::deg, 80, velocityUnits::rpm);
        elevatorMotor2.rotateTo(-500, rotationUnits::deg, 80, velocityUnits::rpm);
      }
      vex::task::sleep(20);
    }
}

I added a startRotateTo because I assume you want both motors to lift simultaneously, so the command starts the rotation but doesn’t wait for it to end, then since the second function is a regular rotateTo the program waits for the command to complete. This works because both motors should take the same time to rotateTo that position.

1 Like

Ohhh that might be it. I’ll try it when later tonight! Wouldn’t one motor move at a time without that, because nothing happened at all before

By manually I mean when holding in a button with a totally different section of the code for the button.pressing, which works fine.

The use of the sensing command

if(Controller1.ButtonUp.pressing())

over

Controller1.ButtonUp.pressed(elevatorheight1);

fixes your sensing issue.

I need to use button pressed in this situation though. I’ve tried so many things but it doesn’t seem to be working.

Adding start rotate to didn’t work. Still nothing. I’ve tried taking it out of the while true loop, nothing. I have no idea at this point, no idea why it’s not working for me.

Could you send your entire code? Probably the best way to figure out the problem.

1 Like

I just realized that I had rpm instead of pct, try this

void usercontrol( void ) 
{
    while(1)
    {
      if(Controller1.ButtonUp.pressing())
      {
        elevatorMotor.startRotateTo(-500, rotationUnits::deg, 80, velocityUnits::pct);
        elevatorMotor2.rotateTo(-500, rotationUnits::deg, 80, velocityUnits::pct);
      }
      vex::task::sleep(20);
    }
}

Alternatively, is it possible that you were looking for rotateFor?

void usercontrol( void ) 
{
    while(1)
    {
      if(Controller1.ButtonUp.pressing())
      {
        elevatorMotor.startRotateFor(-500, rotationUnits::deg, 80, velocityUnits::pct);
        elevatorMotor2.rotateFor(-500, rotationUnits::deg, 80, velocityUnits::pct);
      }
      vex::task::sleep(20);
    }
}
1 Like

How do you make it so that when your pressing and holding a button, it runs, but when you release it, it stops?

Please don’t revive dead threads. @DRow or @Sidoti, please lock this thread.

1 Like