Memory permission error using limit switch

This is my first year trying to use a limit switch but as soon as I try to use it after initializing, it is causing a memory permission error 0380114C. How can I fix this problem?
motor a (PORT20);
limit b (Brain.ThreeWirePort.H);

using namespace vex;

int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
while(true) {
if (b.pressing()){
a.spin(directionType::fwd, 100, velocityUnits::pct);
}
else a.stop();
}
}

If I remember right this is caused by trying to use the brain’s three wire ports before VexCode actually processes they exist. You can fix it by making a triport on port 22. This is because vexcode treats the three wire ports on the brain like a three wire port expander on the non-existent port 22. Here’s your code modified to do this:

triport ThreeWirePort = triport(PORT22);
motor a (PORT20);
limit b (ThreeWirePort.H);

using namespace vex;

int main() {
    // Initializing Robot Configuration. DO NOT REMOVE!
    vexcodeInit();
    while(true) {
        if (b.pressing()) {
            a.spin(directionType::fwd, 100, velocityUnits::pct);
        }
        else a.stop();
    }
}
3 Likes

Thank you! The memory permission error is fixed!