Why doesn't my code run fully?

My code will only run the reload aspect, and nothing after that. If I just run the wait and firing aspect, then it works fine. It just won’t work when combined. Does anyone have any advice on what to change?

#include “vex.h”

using namespace vex;

void reload(int turningDegrees) {
Motor1.setVelocity(20, percent);
Motor1.spinFor(reverse, turningDegrees, degrees);
Motor2.setVelocity(20, percent);
Motor2.spinFor(reverse, turningDegrees, degrees);
Motor1.stop();
Motor2.stop();
}

void fire(int forwardDegrees) {
Motor3.setVelocity(100, percent);
Motor3.spinFor(forward, forwardDegrees, degrees);
}

int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();

reload(4000);
wait(2, seconds);
fire(300);
}

#include “vex.h”

using namespace vex;

void reload(int turningDegrees) {
Motor1.setVelocity(20, percent);
Motor1.spinFor(reverse, turningDegrees, degrees);
Motor2.setVelocity(20, percent);
Motor2.spinFor(reverse, turningDegrees, degrees);
Motor1.stop();
Motor2.stop();
}

void fire(int forwardDegrees) {
Motor3.setVelocity(100, percent);
Motor3.spinFor(forward, forwardDegrees, degrees);
}

int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();

reload(4000);
wait(2, seconds);
fire(300);
}

Reformatted for everyone else.

4 Likes

I don’t know a lot about vex code, but do you need to return an int in the main function?

As this is Programming Support, I believe @jpearman can help you with this. Now before we wait I am going to provide an unofficial response.
I believe the command SpinFor is a yielding function, which means that your code waits until a motor has spinned the designated degree before continuing.
More information about this:

2 Likes

If Motor1 and Motor2 are physically connected then it is likely the first call to Motor1.spinFor() blocks and never returns, because it cannot quite reach its destination.

Consider using nonblocking version of the Motor1.spinFor()/rotateFor() with last argument waitForCompletion=false and also setTimeout() for both motors to prevent them from hanging.

https://api.vexcode.cloud/v5/html/classvex_1_1motor.html#a9d60dc336810ea4d8aef35ee042f0d68
https://api.vexcode.cloud/v5/html/classvex_1_1motor.html#a56b3b114a36270dd2cb7922459c6f59d

void reload(int turningDegrees) {
Motor1.setTimeout(2, sec);
Motor2.setTimeout(2, sec);
Motor1.rotateFor(turningDegrees, degrees, 20, percent, false);
Motor2.rotateFor(turningDegrees, degrees, 20, percent, true);
}
1 Like