VCS Code Not Functioning as Intended

So long story, but in my code I have these statements in the driver control section (these were tested and found to be working):

Controller1.ButtonL1.pressed(intakeToggle);
Controller1.ButtonR1.pressed(systemIn);
Controller1.ButtonL2.pressed(intakeReverse);

Now, the functions themselves are as follows: (Or at least the ones that are acting up)

void intakeToggle(){
if(intake){
IntakeL.stop();
IntakeR.stop();
intake = false;
}
else{
intake = true;
IntakeL.spin(forward);
IntakeR.spin(forward);
}
}

and

void intakeReverse(){
if(intakeREV){
IntakeL.stop();
IntakeR.stop();
intakeREV = false;
}
else{
IntakeL.spin(reverse);
IntakeR.spin(reverse);
intakeREV = true;
}
}

When I run this, my intakes will reverse and go forwards, but when going forward, they won’t stop when the button is toggled again, in addition, going from forward to reverse doesn’t work and stops the intakes, but only every other call, any idea what is wrong here? Thanks in advance.

well, it looks to me like in `intakeToggle` you're just starting and stopping your intakes instantly. because as soon as intake is set to false by the first if statement, it immediately gets set to true by the else statement. which in turn gets immediately set to false by the if statement.

same with the other function.

EDIT: never mind, I see you’re using .pressed, not .pressing, so it should only run the code once per button press. In that case idk why it doesn’t work. This is why I don’t usually answer programming questions lol.

Also please tell me you aren’t actually using VCS.

3 Likes

“Also please tell me you aren’t actually using VCS.”

Yeah ik, I’m lazy. Didn’t feel like downloading a new IDE. Thanks for pointing out my error, I always screw up with else and else if.

vcs is super outdated and bad. it is well worth your time to download vexcode, trust me.

10 Likes

NVM I did a dumb, I am using vex code.

1 Like

Now I’m wondering if this is a VEXCode issue, when I move the systemIn button command to the top of the three, it won’t work but the intakes will… Is there any VexCode rep here who can tell if this is some sort of glitch, or is this a really weird C++ behavior?

Why don’t you try displaying your 2 global state variables (either to the Brain or the Controller) and see if that can help you track down whether this is a logic problem in your code.

Write out, in words, what you expect to happen in various scenarios. Such as:

  1. If the motors are not spinning and I press Button1, they should move forward
  2. If the motors are not spinning and I press Button2, they should move reverse
  3. If the motors are spinning forward and I press Button1, they should stop

Etc.

Then, make sure the states indicated by your global variable map to your scenarios properly.

Other teams have implemented this functionality. I am reasonably confident there is no VEXCode issue, nor any “weird C++ behavior” at play.

1 Like

would need to all the code, perhaps attach the project to this topic.
It’s probably just a coding issue.
The most common problem is putting statements like this.

Controller1.ButtonL1.pressed(intakeToggle);

inside a while loop.

3 Likes

@Mentor_355v good idea, I’ll try
@jpearman I’ll attach the code here as a zip, can you elaborate on the issue with the button pressed state in while loops?

v5code-project-ChangeUp_8780A (1).zip (16.4 KB)

In this version, the intakes work fine but now the flywheel and rollers won’t move (labeled as system)

This

void usercontrol(void) {
  // User control code here, inside the loop
  while (1) {
    //My own drive code -gavin
    FR.spin(forward, Controller1.Axis3.value() - Controller1.Axis1.value() - Controller1.Axis4.value(), percent);
    RR.spin(forward, Controller1.Axis3.value() - Controller1.Axis1.value() + Controller1.Axis4.value(), percent);
    FL.spin(forward, Controller1.Axis3.value() + Controller1.Axis1.value() + Controller1.Axis4.value(), percent);
    RL.spin(forward, Controller1.Axis3.value() + Controller1.Axis1.value() - Controller1.Axis4.value(), percent);
    
    //Reverse intake isn't working, I'll take a look to see what is wrong but for now it isn't functioning.
    Controller1.ButtonR1.pressed(systemIn);
    Controller1.ButtonL1.pressed(intakeToggle);

    

    Controller1.ButtonL2.pressed(intakeReverse);

    wait(20, msec); // Sleep the task for a short amount of time to
                    // prevent wasted resources.
  }
}

you are registering event handler in the while loop, it should be like this.

void usercontrol(void) {
    Controller1.ButtonR1.pressed(systemIn);
    Controller1.ButtonL1.pressed(intakeToggle);

    Controller1.ButtonL2.pressed(intakeReverse);

  // User control code here, inside the loop
  while (1) {
    //My own drive code -gavin
    FR.spin(forward, Controller1.Axis3.value() - Controller1.Axis1.value() - Controller1.Axis4.value(), percent);
    RR.spin(forward, Controller1.Axis3.value() - Controller1.Axis1.value() + Controller1.Axis4.value(), percent);
    FL.spin(forward, Controller1.Axis3.value() + Controller1.Axis1.value() + Controller1.Axis4.value(), percent);
    RL.spin(forward, Controller1.Axis3.value() + Controller1.Axis1.value() - Controller1.Axis4.value(), percent);
    

    wait(20, msec); // Sleep the task for a short amount of time to
                    // prevent wasted resources.
  }
}

and even that may cause issues depending on how/when (and how many times) usercontrol is started. Generally, event handlers should be registered once and only once.

3 Likes