not logical code acting help robotc hates me :(

@jpearman
hello all, today im having an extremely peculiar situation, whenever i click start, the wheels run full forward forever, however whenever i click “step intro” it goes backwards lick its supposed to, whenever i click start, it follows a preversion of logic, meaning that whenever it should enter the sensorvalue<targetvalue, (sensor is target) it enters the line of code that says sensorvalue>targetvalue please help, i dont understand

#pragma config(Sensor, in1,    lift,           sensorPotentiometer)
#pragma config(Sensor, in2,    clawS,          sensorPotentiometer)
#pragma config(Sensor, dgtl1,  LDsensor,       sensorQuadEncoder)
#pragma config(Sensor, dgtl3,  RDsensor,       sensorQuadEncoder)
#pragma config(Sensor, dgtl5,  topreset,       sensorTouch)
#pragma config(Sensor, dgtl6,  bottomreset,    sensorTouch)
#pragma config(Motor,  port1,           CR,            tmotorVex393HighSpeed_HBridge, openLoop)
#pragma config(Motor,  port2,           LD,            tmotorVex393_MC29, openLoop, encoderPort, dgtl1)
#pragma config(Motor,  port3,           LTM,           tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port4,           LMM,           tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port5,           LBM,           tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port6,           RTM,           tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port7,           RMM,           tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port8,           RBM,           tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port9,           RD,            tmotorVex393_MC29, openLoop, encoderPort, dgtl3)
#pragma config(Motor,  port10,          CL,            tmotorVex393_HBridge, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

//sensor not in sensorValue is what you want it to go to
	void hang (float sensor)
		{
			motor[LD]=127;
			motor[RD]=127;//drive forward
	while(SensorValue[lift]<sensor)
{
	motor[LTM]=127;
	motor[LMM]=127;
	motor[LBM]=127;
	motor[RTM]=127;
	motor[RMM]=127;
	motor[RMM]=127;// pull up
}

	while(SensorValue[lift]>sensor)
{
	motor[LTM]=0;
	motor[LMM]=0;
	motor[LBM]=0;
	motor[RTM]=0;
	motor[RMM]=0;
	motor[RMM]=0;
	wait1Msec(100);
}
}


	void turnR (float speed, float Lsensor, float Rsensor)
		{
			while(SensorValue[LDsensor]<.8*Lsensor& SensorValue[RDsensor]<.8*Rsensor)
	{
			motor[LD]=-speed;
			motor[RD]=speed;
	}
			while(SensorValue[LDsensor]>(.8*Lsensor+.8*Rsensor)*.5 & SensorValue[LDsensor]<Lsensor &SensorValue[RDsensor]<Rsensor)
	{
			motor[RD]=1/2*-speed;
			motor[LD]=1/2*speed;
	}
}


	void driveF (float speed, float sensor)
{		
		wait10Msec(10)
		if(sensorvalue[ldsensor]>sensor)
  {
  	motor[LD]=-speed;
  	motor[RD]=-speed;
  	waitUntil(sensorvalue[LDsensor+RDsensor]>1.75*sensor)
  	motor[LD]=3/4*-speed;
  	motor[RD]=3/4*-speed;
  	waitUntil(SensorValue[LDsensor+RDsensor]>2*sensor)
  	motor[ld]=0
  	motor[RD]=0
}
		
	else	if(sensorvalue[LDsensor]<sensor)
	{
    motor[LD]=speed;
		motor[RD]=speed;
	waitUntil(sensorvalue[LDsensor]>1.75*sensor);
		motor[LD]=3/4*speed;
		motor[RD]=3/4*speed;
	waitUntil(SensorValue[LDsensor]>sensor);
	motor[ld]=0;
	motor[rd]=0;
  }

   else if(sensorvalue[ldsensor]>sensor)
  {
  	motor[LD]=-speed;
  	motor[RD]=-speed;
  	waitUntil(sensorvalue[LDsensor+RDsensor]>1.75*sensor)
  	motor[LD]=3/4*-speed;
  	motor[RD]=3/4*-speed;
  	waitUntil(SensorValue[LDsensor+RDsensor]>2*sensor)
  	motor[ld]=0
  	motor[RD]=0
}
}
  	
  	
  	


	void liftup (float sensor)
	{
		while(SensorValue[lift]<sensor)
	{
		motor[LTM]=-127;
		motor[LMM]=-127;
		motor[LBM]=-127;
		motor[RTM]=-127;
		motor[RMM]=-127;
		motor[RMM]=-127;
	}
		while(SensorValue[lift]>sensor)
	{
		motor[LTM]=0;
		motor[LMM]=0;
		motor[LBM]=0;
		motor[RTM]=0;
		motor[RMM]=0;
		motor[RMM]=0;
	}
	}

	void liftdown (float sensor)
	{
		while(SensorValue[lift]<sensor)
	{
		motor[LTM]=127;
		motor[LMM]=127;
		motor[LBM]=127;
		motor[RTM]=127;
		motor[RMM]=127;
		motor[RMM]=127;
	}
		while(SensorValue[lift]>sensor)
	{
		motor[LTM]=0;
		motor[LMM]=0;
		motor[LBM]=0;
		motor[RTM]=0;
		motor[RMM]=0;
		motor[RMM]=0;
	}
	}



	void claw(float sensor)
{
	if(sensor>SensorValue[clawS])
	{
	motor[CR]=127;
	motor[CL]=127;
	}
	while(SensorValue[clawS]>sensor)
{
	motor[CR]=0;
	motor[CL]=0;
	}

	wait1Msec(0);
}
	void turnL(float speed, float sensorR, float sensorl)
		{
			motor[LD]=speed;
			motor[RD]=-speed;
			waitUntil(SensorValue[LDsensor]>sensorl & SensorValue[LDsensor]>sensorR);
			motor[RD]=0;
			motor[LD]=0;
}
task main()
{
	while(1==1)
	{
driveF(60, -320);
claw(1000);
driveF(127, -320);
claw(2300);//pick up first driver load cube
drivef(-127, -885)
liftup(247); // lift up to dump cub #1
claw(1000);// dump cube #1
liftdown(247);
driveF(127,-320); // drive forwards to grab another cube
claw(2300);// pickup cube
drivef(-127, -885)
liftup(247);
claw(1000); // dump cube #2
liftdown(247);
drivef(127, -320);
claw(2300);//grab 3 driver load stars
drivef(127, -885)
liftup(247);
claw(1000);// dump 3 stars
liftdown(247);
turnR(127, -1070, -700);// turn to grab 3 fence stars
SensorValue[RDsensor]==0;
SensorValue[LDsensor]==0;
drivef(127, 1500);
claw(2300);//grab 3 stars
turnl(127, 1692, 1290)
SensorValue[RDsensor]==0;
SensorValue[LDsensor]==0;
liftup(247);//lift to dump 3 fence stars
claw(1000);
liftdown(247)
turnR(80, -1069, -704);
SensorValue[RDsensor]==0;
SensorValue[LDsensor]==0;
claw(1000);//dump 3 stars
liftdown(247);
driveF(80, -160);// grab middle cube
claw(2300);//grab middle cube
liftup(247);
drivef(80, 400);
claw(1000);// dump middle  cube
liftdown(247);//reset arms
turnL(127, 38, -181);// aim at back cube
SensorValue[RDsensor]==0;
SensorValue[LDsensor]==0;
driveF(100, 420);
claw(2300);//grab back cube
liftup(247);
turnr(127, 332, 501);//aim at right fence
SensorValue[RDsensor]==0;
SensorValue[LDsensor]==0;
driveF(-100, -260);
claw(1000);//dump back cube
driveF(80, -330);//drive to middle of the feild
turnL(127, 917, 869);//aim at hang pole
SensorValue[RDsensor]==0;
SensorValue[LDsensor]==0;
driveF(-80, 000);//drive into pole
liftup(247);
liftdown(247);
}
}

The thing I see is in the following statement: while(SensorValue[lift]<sensor) , what value does sensor have? You are asking for a comparison between SensorValue[lift] and sensor. But nowhere to I see where the value of sensor comes from. Although, I am no programming expert either. Just what I see.

The (sensor) value is the height in which the the lift goes to, howevet what im asking about is for the drive, what happens is if i click start it drives forward forever, however whenever i click through the process, it works as intended

I’m not exactly sure why single step would behave completely differently from start but there are a number of issues with the code. Lets start by looking at a simplified version.

#pragma config(Sensor, in1,    lift,           sensorPotentiometer)
#pragma config(Sensor, in2,    clawS,          sensorPotentiometer)
#pragma config(Sensor, dgtl1,  LDsensor,       sensorQuadEncoder)
#pragma config(Sensor, dgtl3,  RDsensor,       sensorQuadEncoder)
#pragma config(Sensor, dgtl5,  topreset,       sensorTouch)
#pragma config(Sensor, dgtl6,  bottomreset,    sensorTouch)
#pragma config(Motor,  port1,           CR,            tmotorVex393HighSpeed_HBridge, openLoop)
#pragma config(Motor,  port2,           LD,            tmotorVex393_MC29, openLoop, encoderPort, dgtl1)
#pragma config(Motor,  port3,           LTM,           tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port4,           LMM,           tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port5,           LBM,           tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port6,           RTM,           tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port7,           RMM,           tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port8,           RBM,           tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port9,           RD,            tmotorVex393_MC29, openLoop, encoderPort, dgtl3)
#pragma config(Motor,  port10,          CL,            tmotorVex393_HBridge, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

void driveF (float speed, float sensor)
{   
  wait10Msec(10);
  if(SensorValue[LDsensor]>sensor)
  {
    motor[LD]=-speed;
    motor[RD]=-speed;
    waitUntil(SensorValue[LDsensor+RDsensor]>1.75*sensor);
    motor[LD]=3/4*-speed;
    motor[RD]=3/4*-speed;
    waitUntil(SensorValue[LDsensor+RDsensor]>2*sensor);
    motor[LD]=0;
    motor[RD]=0;
  }

  else  if(SensorValue[LDsensor]<sensor)
  {
    motor[LD]=speed;
    motor[RD]=speed;
    waitUntil(SensorValue[LDsensor]>1.75*sensor);
    motor[LD]=3/4*speed;
    motor[RD]=3/4*speed;
    waitUntil(SensorValue[LDsensor]>sensor);
    motor[LD]=0;
    motor[RD]=0;
  }

  else if(SensorValue[LDsensor]>sensor)
  {
    motor[LD]=-speed;
    motor[RD]=-speed;
    waitUntil(SensorValue[LDsensor+RDsensor]>1.75*sensor);
    motor[LD]=3/4*-speed;
    motor[RD]=3/4*-speed;
    waitUntil(SensorValue[LDsensor+RDsensor]>2*sensor);
    motor[LD]=0;
    motor[RD]=0;
  }
}
void 
claw(float sensor)
{
  // does nothing for this test
}

task main()
{
    driveF(60, -320);
    claw(1000);
    driveF(127, -320);
    claw(2300);//pick up first driver load cube
    driveF(-127, -885);

    while(1)
      wait1Msec(10);
}

That’s just the driveF function and a dummy claw function. I had to fix a whole bunch of warnings just to get that to compile correctly, lots of missing semi-colons and capitalization of SensorValue etc. although RobotC will try and repair the program (add the missing semi-colons etc.) you should really try and write code that does not cause any warnings at all.

The driveF function has some issues. This line of code will not work


waitUntil(SensorValue[LDsensor+RDsensor]>1.75*sensor);

You are trying to read a sensor that does not really exist “LDsensor+RDsensor”.
secondly you have a test on the SensorValue three times trying to (presumably) determine direction.

You are not clearing the encoders to zero anywhere, if you want the driveF function to move the robot a relative amount, the you should clear the encoder each time you call it.


    driveF(-127, -885);

This also seems to be an issue. Should the speed be negative? You seem to take care of that in the driveF function as well (by reversing speed if the robot should travel backwards).

next issue


		waitUntil(SensorValue[LDsensor]>1.75*sensor);

lets look at what this means
“wait until the value of the encoder is greater than 1.75 times the distance we want to travel”

I suspect you mean


		waitUntil(SensorValue[LDsensor]>0.75*sensor);

“wait until the value of the encoder is greater than three quarters the distance we want to travel”

or perhaps you were trying to add the two encoder values together, in which case the code would be

		waitUntil( (SensorValue[LDsensor] + SensorValue[RDsensor]) > 1.75*sensor);

So you have a few things to fix, let us know how it works out.

Yeah i fixed most of the yellow errors on my updated code, the main probelm i think was sensorvalue[rdsensor+ldsensor] but yeah i fixed a couple. Of the things you mention on my own though thank you so much, but yeah start and step behaaved super differently, idk why and thats mostly what was frustrating me. Thank you so much for everything you do for this community

Obviously when you step through the code everything happens very slowly, you would have started the motors and the encoders would have counted many steps before you executed the next line of code. Without having an exact copy of your robot it’s almost impossible for me to debug that for you.

Yeah its fine, just a little frustrating is all, when the robot works like you want it to, then when you run it it goes wonky, thanks anyways