croboticsnxt

Touch sensor not working


I am attempting to create a very simple program in RobotC. In this program the robot will move forward until the touch sensor is hit.

#pragma config(Sensor, S2,     touchSensor,    sensorTouch)

void setMotors(int a, int b){
    motor[motorA] = a;
    motor[motorB] = b;
}

task main(){
    wait1Msec(100);//Wait for sensor to init

    setMotors(50, 50);

    while(sensorValue(touchSensor) == 0){
        //Do Nothing
    }

    setMotors(0, 0);
}

This code should make the robot move forward until the touch sensor is triggered. Whenever I try and do anything with the touch sensor it does not work. When I output the value to the debug log it shows 180 when pressed and 1024 when released. I have verified that it is working normally by viewing the value on the brick itself.

Robot C Version: 4.0


Solution

  • Apparently, your touch sensor is stuck in SensorRaw mode. It is unclear - from the documentation I could find - how this could be fixed in code, but a work-around would be to explicitly put the sensor into raw mode (in case the situation changes in the future), and then compute the boolean value with a function like this:

    bool sensorIsOn(short sensorRawValue)
    {
        bool isOn = false;
        if(sensorRawValue > 512)
        {
            isOn = true;
        }
        return isOn;
    }