potentiometer if-else

  1. 4 months ago

    I'm trying to get code that mixes if else statements and potentiometer positions.
    hereit is:
    #pragma config(Sensor, in1, Potentiometer, sensorPotentiometer)
    #pragma config(Sensor, dgtl1, Green, sensorLEDtoVCC)
    #pragma config(Sensor, dgtl12, yellow, sensorLEDtoVCC)
    #pragma config(Motor, port2, one, tmotorVex393HighSpeed_MC29, openLoop)
    //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

    task main()
    {
    while(1==1)
    {
    if(Potentiometer >= 0 && Potentiometer <= 800)
    {
    turnLEDOn(Green);
    startMotor(port2, 126);
    wait1Msec(2000);
    stopMotor(port2);
    turnLEDOff(Green);
    }
    else if(Potentiometer >= 1200 && Potentiometer <= 4000)
    {
    turnLEDOn(yellow);
    startMotor(port2, -126);
    wait1Msec(2000);
    stopMotor(port2);
    turnLEDOff(yellow);
    }
    else
    {
    stopMotor(port2);
    }

    }

    }

    am I doing something wrong because it is not working.

  2. callen

    Mar 13 Braintree, MA, USA

    What is it you want to happen? It looks like your code is set to have it run at nearly full speed in one direction or the other, with the green LED always on paired with one direction and the yellow LED always on paired with the other direction. That is, unless you have the potentiometer in a certain region, in which case nothing happens at all. You can manually change which it's doing in the middle of the program. It doesn't look like what you intend because you've got some other commands in there that essentially do nothing (two stopMotor commands). Is this what the robot's doing? If so, it's following your code fine. So how does this differ from what you want? What exactly do you want?

 

or Sign Up to reply!