Vex 2.0 Help Encoder Conveyer belt

I’m trying to make a conveyer belt that uses and encoder to travel a distance, stop, and then continue. The robot currently goes until the encoder reaches 300 and then it resets to 0 and stops. Can anybody help solve the problem?

#pragma config(Sensor, dgtl1, Button, sensorTouch)
#pragma config(Sensor, dgtl2, Limit, sensorTouch)
#pragma config(Sensor, dgtl3, Encoder, sensorRotation)
#pragma config(Motor, port1, Motor, tmotorVex393_HBridge, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task e_stop()
{
while(true)
{
if(SensorValue(Limit) == 1)
{
stopAllTasks();
}
wait1Msec(2000);
}
}

task main()
{
startTask(e_stop);
wait1Msec(2000);

if (SensorValue(Button)== 1)
{
	while(1 == 1)
	{
		if (SensorValue(Encoder) == 0)
		{
			motor[port1] =50;
		}
		if (SensorValue(Encoder) == 300)
		{
			SensorValue[Encoder]= 0;
			{
				stopMotor(port1);
				{
					wait1Msec(5000);
				}
			}
		}
	}
}

}

1 Like

I’ve formatted your code for readability. I encourage you to use proper indentation; it make your code much easier to understand. Additionally I encourage you to use the code tags which can be done by placing a ``` before and after your code.

#pragma config(Sensor, dgtl1, Button, sensorTouch)
#pragma config(Sensor, dgtl2, Limit, sensorTouch)
#pragma config(Sensor, dgtl3, Encoder, sensorRotation)
#pragma config(Motor, port1, Motor, tmotorVex393_HBridge, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task e_stop()
{
	while(true)
	{
		if(SensorValue(Limit) == 1)
		{
			stopAllTasks();
		}
		wait1Msec(2000);
	}
}

task main()
{
	startTask(e_stop);
	wait1Msec(2000);

	if (SensorValue(Button)== 1)
	{
		while(1 == 1)
		{
			if (SensorValue(Encoder) == 0)
			{
				motor[port1] =50;
			}
			if (SensorValue(Encoder) == 300)
			{
				SensorValue[Encoder]= 0;
				{
					stopMotor(port1);
					{
						wait1Msec(5000);
					}
				}
			}
		}
	}
}
4 Likes

Thank you for the format help! I will update it now.
Do you have any clue why my code doesn’t work?

At first glance it looks like the issue is that you reset the encoder to 0 before the motor has finished turning. This means that the motor will continue to coast for a short time after you have reset the encoder, leading to its value being somewhat greater than 0. Since you are checking only for the encoder value to be exactly 0 or exactly 300, it does not do what you want it to do. I suggest you utilize inequalities like so:

#pragma config(Sensor, dgtl1, Button, sensorTouch)
#pragma config(Sensor, dgtl2, Limit, sensorTouch)
#pragma config(Sensor, dgtl3, Encoder, sensorRotation)
#pragma config(Motor, port1, Motor, tmotorVex393_HBridge, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task e_stop()
{
	while(true)
	{
		if(SensorValue(Limit) == 1)
		{
			stopAllTasks();
		}
		wait1Msec(2000);
	}
}

task main()
{
	startTask(e_stop);
	wait1Msec(2000);

	if (SensorValue(Button)== 1)
	{
		while(1 == 1)
		{
			if (SensorValue(Encoder) < 300){
				motor[port1] =50;
			}
			else
			{
				stopMotor(port1);
				wait1Msec(5000);
				SensorValue[Encoder]= 0;
			}
			wait1Msec(20);
		}
	}
}

I’ve also removed some unnecessary brackets inside the second if statement, and have added a delay to your loop to prevent the main task from hogging the CPU.

2 Likes

Thank you so much!!! It worked like a charm.

1 Like