How do i use button.pressed for my fly wheel

I want to push the X button once and my flywheel to spin until I push the B button to stop it. can someone advise on how to use the buttonX.pressed function?

Use pressing instead of pressed

4 Likes

image

1 Like

A way to do this would be to have a boolean to set to true when that button is pressed and false when the other one is pressed.

bool flywheelSpin = false;

If(controller1.buttonX.pressing){
bool flywheelSpin = true;
}

When(flywheelSpin){

Flywheelmotor.spin(forward, 100, percent);

If(controller1.buttonA.pressing){

bool flywheelSpin = false
}

Return 1;
}


This should work idk tho (,coding off the top of my head

@jpearman who do I have to talk to to file a complaint that pressed in text is callback function setup and “pressed?” in blocks returns a boolean. Note I am in Pittsburgh and can find Tim and Jacob.

5 Likes

image

i had to make a function and call on it when i pressed the button. you can just change the controller button in the if statement.

1 Like

I don’t like that function, but here you go:

Code in blocks:
image

Generated code in code viewer:

// "when started" hat block
int whenStarted1() {
  flywheel.setVelocity(100.0, percent);
  return 0;
}

// "when Controller ButtonEUp pressed" hat block
void onevent_ControllerButtonEUp_pressed_0() {
  flywheel.spin(forward);
}

// "when Controller ButtonEDown pressed" hat block
void onevent_ControllerButtonEDown_pressed_0() {
  flywheel.stop();
}


int main() {
  // register event handlers
  Controller.ButtonEUp.pressed(onevent_ControllerButtonEUp_pressed_0);
  Controller.ButtonEDown.pressed(onevent_ControllerButtonEDown_pressed_0);

  wait(15, msec);
  whenStarted1();
}

If you want one button to both start and stop it:
image

In code:

bool flywheelIsSpinning;

// "when started" hat block
int whenStarted1() {
  flywheel.setVelocity(100.0, percent);
  flywheelIsSpinning = false;
  flywheel.stop();
  return 0;
}

// "when Controller ButtonEUp pressed" hat block
void onevent_ControllerButtonEUp_pressed_0() {
  if (flywheelIsSpinning) {
    flywheel.stop();
  }
  else {
    flywheel.spin(forward);
  }
  flywheelIsSpinning = !flywheelIsSpinning;
}


int main() {
  // register event handlers
  Controller.ButtonEUp.pressed(onevent_ControllerButtonEUp_pressed_0);

  wait(15, msec);
  whenStarted1();
}

There’s a trick; if you forget how to do something, do the same thing in blocks and look at the code.

The biggest rule about when button pressed functions is only register them once! Don’t call Controller.ButtonEUp.pressed(function) in a loop; call it once in int main before the rest of your code.

2 Likes