Callback Commands

Hi. I need some help programming (Im TRASH). How can i get it in my programming to do the following:

-When Button Up Pressed change velocity of two moters, Lift1 and Lift2, to 10%

Then when Button Down Pressed, change velocity o both motors back to original 75%.

PRESSED NOT PRESSING

I CANT FIGURE IT OUT!!!

Help me please.

What do you have so far? Also, is it like a situation where you are controlling the lift with other buttons/joysticks and the up and down buttons are purely for velocity control?

I think you should stick with pressing for that and follow this example as the base code for toggle and latch.

You only need to use one button. Each time it is pressed it can toggle speed to the next value.

3 Likes

v5code-project-Tower_Takeover_Freshman_Olymics.zip (16.0 KB)

This is what I have so far. :frowning:

This doesn’t help if you are trying to learn how callbacks work but it is an alternative solution.

Add the latch variables before the while in your user control.

bool toggle = false;
bool latch = false;

while (1) {
 //User control code ...

Then replace your “Untested Code” with this:

  Drivetrain.setDriveVelocity(50,percent);

  if (Controller1.ButtonUp.pressing()) {
	if(!latch){ //flip the toggle one time and set the latch
	  toggle = !toggle;
	  latch = true;
	}
  } else {
	latch = false;
  }

 if (toggle){
    Lift1.setVelocity(10,percent);
    Lift2.setVelocity(10,percent);
 } else {
    Lift1.setVelocity(50,percent);
    Lift2.setVelocity(50,percent);
 }
6 Likes

Thanks! I tried it today and it worked perfectly!!

1 Like

Yes, different buttons control the lift, and up and down buttons are purely for velocity control.

Personally, this is how I would code it, but everyone has their way of coding something.

int armVelocity = 0;

if(Controller1.ButtonUp.pressing()){

  armVelocity = 10;

}

else if(Controller1.ButtonDown.pressing()){

  armVelocity = 75;

}

if(Controller1.ButtonR1.pressing()){

  Lift1.spin(directionType::fwd,armVelocity,velocityUnits::pct);
  Lift2.spin(directionType::fwd,armVelocity,velocityUnits::pct);

}

else if(Controller1.ButtonR2.pressing()){

  Lift1.spin(directionType::rev,armVelocity,velocityUnits::pct);
  Lift2.spin(directionType::rev,armVelocity,velocityUnits::pct);

}

else{

  Lift1.stop();
  Lift2.stop();

}
1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.