Button question

Hello forumers,
I’m coding my flywheel right now, and I’m trying to make it so R1 spins it up and R2 fires the indexer. This is my current code:
image
During tests, it didn’t seem to work. Am I doing this right? Is it even possible?

Is your code in a while loop? Also, you might want an else statement, so that the motors stop spinning when you release the buttons. This is a code I made just now in a few minutes. It allows you to press R1 once to start the flywheel and R2 to stop it. This makes it a lot easier on the driver. The indexer is on L1 now. I do not even know if this code will work, but I mean it as an example.

#pragma region VEXcode Generated Robot Configuration
// Make sure all required headers are included.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>


#include "vex.h"

using namespace vex;

// Brain should be defined by default
brain Brain;


// START V5 MACROS
#define waitUntil(condition)                                                   \
  do {                                                                         \
    wait(5, msec);                                                             \
  } while (!(condition))

#define repeat(iterations)                                                     \
  for (int iterator = 0; iterator < iterations; iterator++)
// END V5 MACROS


// Robot configuration code.
controller Controller1 = controller(primary);
motor Flywheel = motor(PORT1, ratio18_1, false);

motor indexer = motor(PORT2, ratio18_1, false);




// define variable for remote controller enable/disable
bool RemoteControlCodeEnabled = true;
#pragma endregion VEXcode Generated Robot Configuration
// Include the V5 Library
#include "vex.h"
  
// Allows for easier use of the VEX Library
using namespace vex;
void flyspin(){
  Flywheel.spin(forward);
}
void flystop(){
  Flywheel.stop();
}
float myVariable;

// "when started" hat block
int whenStarted1() {
  while (true) {
  Controller1.ButtonR1.pressed(flyspin);
  Controller1.ButtonR2.pressed(flystop);
  if(Controller1.ButtonL1.pressing()){
  indexer.spin(forward);  
  }
  else{
  indexer.stop();
  }
    
  }

  wait(5, msec);
  }

int main() {
  // post event registration

  // set default print color to black
  printf("\033[30m");

  // wait for rotation sensor to fully initialize
  wait(30, msec);

  whenStarted1();
}
1 Like

By it doesn’t seem to work, I am assuming that you mean the indexer isn’t firing when you click buttonR2 while holding down the flywheel button. My team encountered the same problem. I’m not exactly sure why it’s not able to detect two consecutive key presses,
but the way I approached it was by having one button wind up the flywheel, and another spin both the flywheel and indexer at the same time.
So the driver has to alternate between buttons to shoot. I don’t think this is the best way to do it but it worked for our team. Hope this helps.

Thank you! I will try this out when I have access to the robot tomorrow.