0

I’m trying to make a loop where a motor will run and lights will cycle indefinitely as long as the button is not pressed yet when the while(SensorValue[bump]==0) will only run once even if the button isn’t pressed.

#pragma config(Sensor, dgtl2,  bump, sensorNone)
#pragma config(Sensor, dgtl10, green,  sensorLEDtoVCC)
#pragma config(Sensor, dgtl11, red,            sensorLEDtoVCC)
#pragma config(Sensor, dgtl12, amber,          sensorLEDtoVCC)
#pragma config(Motor,  port2,  fan,tmotorVex393_MC29, openLoop)


task main()
   {
    while(true){                                    
       untilBump(bump); //waits until button is pressed
       while(SensorValue(bump)==0){                                 

        startMotor(fan,127);        //startmotor

        turnLEDOn(green);       //turn green on
        delay(2000);
        turnLEDOff(green);  //turn green off

        turnLEDOn(amber);   //turn amber on
        delay(2000);
        turnLEDOff(amber);  //turn amber off

        turnLEDOn(red);     //turn red on
        delay(2000);
        turnLEDOff(red);    //turn red off

    }

    stopMotor(fan);
}

}
  • Another thing to note is that you are never going to escape the first while loop without a `break` or `return`. – busybear Jan 18 '19 at 16:19
  • I don't know anything about `robotc`, but it looks to me like you have a few issues with these loops. Your motor will likely be told to start multiple times without stopping (the `stop` is outside the `while(true)` loop, even). Your trigger condition causes a process that takes _at least_ 6 seconds to complete in a (presumably) single-threaded system. I think you've assigned `bump` to `sensorNone` which seems logically incorrect and you should always expect the same value out of it (probably `0`). – Ian MacDonald Jan 18 '19 at 16:19
  • Robotc is just a really strange language, bump is mapped to port dgtl2 and will out put a 1 or a 0 depending on if it’s pressed, robot c also doesn’t use breaks. This is a example of the languages while loops http://staffordhs.ss8.sharpschool.com/common/pages/UserFile.aspx?fileId=24059775 – Collin Smallegan Jan 18 '19 at 16:23
  • My stop motor is also inside the while(true), basically after the button is pressed it will break the loop then the motor will stop, the code will then start at the top right at untilBump(bump) – Collin Smallegan Jan 18 '19 at 16:25

0 Answers0