Ultrasonic sensor code problem

I am trying to program the ultrasonic sensor to initiate the front part of our feed mechanism on our robot, to both learn how to use it and to see if this application is practical.

The problem I am having is that when I run the code, the feed mech just runs, no matter what the sensor is reading. What it should be doing is running the feed mech anytime the sensor senses any something in that area, changing the value to something other than -1, and not run when it senses nothing, value equal -1. Can someone help me out here? I am running RobotC for Cortex and am fairly new to it coming from EasyC.

The Ultrasonic part

if(SensorValue[Sonar] != -1)  //ultrasonic to operate the front feed
		{
			motor[FF]=127;
		}
		else
		{
			motor[FF]=0;
		}

The Pragmas

#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, dgtl1,  Sonar,          sensorSONAR_inch)
#pragma config(Sensor, I2C_1,  RDIME,          sensorQuadEncoderOnI2CPort,    , AutoAssign)
#pragma config(Sensor, I2C_2,  LDIME,          sensorQuadEncoderOnI2CPort,    , AutoAssign)
#pragma config(Motor,  port2,           RD,            tmotorVex393_MC29, openLoop, driveRight, encoderPort, I2C_1)
#pragma config(Motor,  port3,           LD,            tmotorVex393_MC29, openLoop, driveLeft, encoderPort, I2C_2)
#pragma config(Motor,  port4,           RL,            tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port5,           LL,            tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port6,           MF,            tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port7,           FF,            tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port8,           RL,            tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port9,           LL,            tmotorVex393_MC29, openLoop)

The entire User Control

task usercontrol()
{
	//sets IME's to Zero
	nMotorEncoder[RD]=0;
	nMotorEncoder[LD]=0;

	while (true)
	{ 
		motor[LD]=.65*vexRT[Ch3];		//Drive 
		motor[RD]=vexRT[Ch2];   		//Drive

		motor[RL]=vexRT[Ch1];   //Launchers for test
		motor[LL]=vexRT[Ch1];   //Launchers for test

		if (vexRT[Btn5U]==1)   //Feed on Left Front Buttons
		{
			motor[MF]=127;
		}
		else if(vexRT[Btn5D]==1)
		{
			motor[MF]=-127;
		}
		else
		{
			motor[MF]=0;
		}

		if(SensorValue[Sonar] != -1)  //ultrasonic to operate the front feed
		{
			motor[FF]=127;
		}
		else
		{
			motor[FF]=0;
		}
	}
}

The problem you are running into may be with the sonar detecting values that are far away, but still in detection range. To test for this, try adding another condition to your if statement’s condition (see below for example) that checks to see if the sonar is both not equal to -1 AND less than a certain threshold value:


if(SensorValue(Sonar) != -1 && SensorValue(Sonar) < 35)

You can also use the Sensors Debug window in ROBOTC to see the Sonar sensor’s returned value (while the program is running and the Cortex is connected to the computer, either via direct USB A-to-A connection or through a VEXNet Joystick Controller’s Program port by using a Programming Hardware Kit), which should help you determine the exact cause of this issue.

I will try adding that second condition on Monday when I have access to the robot.

I was using the sensor debug to see the returned values and when there was nothing in the opening to the feed, I got -1. When there was a ball, it would be anywhere from 6 to 105 I believe was the highest value, depending on if the ball entered from the right, left, or center of the opening.

Thank you for the help

I tried that today and it seemed to have fix the problem, thanks for the help.