Expansion coding help

Hello, I need some help on coding the expansion for our robot. I want to make it so that the expansion of our robot will only be able to be activated after 1 minute and 35 seconds of the driver control period have passed. I was thinking of making some sort of timer that sets the value of a Boolean variable to true after the certain amount of time has passed and then having the program check whether the button has been pressed and if the variable is true. I can code the button pressed part, but I need some help with the timer, as I have never used a timer inside of a program before. Any help would be greatly appreciated!

We used jpearman’s answer here to come up with something. See if it helps.

1 Like

I am trying to do something similar where the controller vibrates at certain times. i will let you know if I get anywhere, but personally I think it would be best to manually activate your expansion mech, for accuracy, and so you don’t hit anyone.

A timer is a reasonable idea but it does take the control away from your driver. There may be a circumstance where the timed event is not the desired result.

I would suggest you use a button combination - IE: the driver has to hold B and then both up trigger buttons - or something similar. This way they can’t accidentally fire the endgame without explicitly doing the correct sequence but they can make the decision to do it should the field clock not match the clock on the brain.


I agree, for our code we use the Brian timer to not allow expansion until 1:35 into the match. Once a 1:35 has elapsed we give the driver the option to fire them by holding down the right arrow and button Y. This way the driver has to take their thumbs of the joystick to fire but if we are in a position where expanding would get us disqualified we can chose not expand.

Just be aware that if during the match your robot disconnects and reconnects your timer may have the incorrect match time


What would cause this? In our code we reset the brain timer when driver control starts and then check is the timer value is greater than or equal to 95 seconds and both buttons are being pressed. Does the controller disconnecting affect the brain timer?

It is possible for driver control to be called multiple times during a match (though this is not common).

Suppose we have the following driver control function:

void drivercontrol() {
  int driverStart = Brain.getTime();  // Assume seconds for this example, probably is actually milliseconds
  // ...
  while(1) {
    // ... Driver control stuff
    if(Brain.getTime() - driverStart >= 95) {
      // Autodeploy endgame
    delay(20, ms);

Could scenarios:

  1. You get to the field, turn on your robot and run your program without plugging into Field Control
    a. drivercontrol will get started immediately
    b. You forget to plug into field control because the previous match is taking a while
    c. 95 seconds later, your endgame deploys
  2. You get to the field, plug into Field Control, then start your program
    a. drivercontrol will not start until Field Control tells it to
    b. Auton triggers
    c. Auton completes
    d. FC triggers drivercontrol and you start driving
    e. During the match for some reason you need to restart your program 30 seconds into the match. FC is in driver control, so drivercontrol runs.
    f. Your autodeploy doesn’t autodeploy because drivercontrol thinks only 65 seconds have happened

Given these plausible scenarios, how would one ensure that the endgame mech only deploys in the last 10 seconds? (Hint, it’s possible)

1 Like

Yes as previously stated we reset the brain timer every time driver control is run.

Our code looks similar to this:

int expansionTimeLimit = 95000; // One min 35 secs in miliseconds
int timerVal;

void usercontrol () {
Brain.Timer.clear(); // Sets the brain timer to 0;

  while (true){
    // Drive control stuff

    timerVal = Brain.Timer.time(msec); // Sets var to timer value 
    if (controller1.ButtonRight.pressing() == true && // Runs if both buttons pressed and timer val is above limit
    controller1.ButtonY.pressing() == true && 
    timerVal >= expansionTimeLimit){

       expansion.set(true); // Releases expansion

This way no matter how many times or when the function is run it gives the correct time of how long user control has been running.

Please remember that if you are going to use this that you create a seperate code for skills that doesn’t go off after a minute seeing as skills is only a minute.

This code assumes that usercontrol is only called once per match which is usually, but not always, correct.

Yes, especially as the endgame period can be triggered at any point during a robot skills match, so having this time-based limiter for skills is not a good idea.

1 Like

Oh, I know understand how this comes into play with our code. Our team hasn’t run into this situation before. Thank you for the input and we will be fixing this in future revisions of our code. :+1: