Programming Help: Bumper Switch not Activating

Hey guys, we were working on an autonomous code for vex, but we’re running into issues.

The robot is supposed to execute a sequence after interacting with a touch sensor in the autonomous period.

#pragma config(Sensor, dgtl1,  rightencoder,   sensorQuadEncoder)
#pragma config(Sensor, dgtl5,  liftencoder,    sensorRotation)
#pragma config(Sensor, dgtl6,  leftencoder,    sensorRotation)
#pragma config(Sensor, dgtl7,  funnelencoder,  sensorQuadEncoder)
#pragma config(Sensor, dgtl11, caurax,         sensorTouch)
#pragma config(Sensor, dgtl12, touch,          sensorTouch)
#pragma config(Motor,  port1,           RightLift2,    tmotorVex393, openLoop)
#pragma config(Motor,  port2,           FrontRight,    tmotorVex393, openLoop)
#pragma config(Motor,  port3,           BackRight,     tmotorVex393, openLoop)
#pragma config(Motor,  port4,           FrontLeft,     tmotorVex393, openLoop)
#pragma config(Motor,  port5,           BackLeft,      tmotorVex393, openLoop)
#pragma config(Motor,  port6,           Funnel,        tmotorVex269, openLoop)
#pragma config(Motor,  port7,           LeftLift,      tmotorVex393, openLoop)
#pragma config(Motor,  port8,           RightLift1,    tmotorVex393, openLoop)
#pragma config(Motor,  port9,           Intake,        tmotorVex393, openLoop, reversed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#pragma platform(VEX)

#pragma competitionControl(Competition)
#pragma autonomousDuration(15)
#pragma userControlDuration(120)

#include "Vex_Competition_Includes.c"

void pre_auton()
{
	// Set bStopTasksBetweenModes to false if you want to keep user created tasks running between
	// Autonomous and Tele-Op modes. You will need to manage all user created tasks if set to false.
	bStopTasksBetweenModes = true;
	SensorValue(leftencoder) = 0;
	SensorValue(rightencoder) = 0;
	SensorValue(funnelencoder) = 0;
	SensorValue(liftencoder) = 0;
	// All activities occur before the competition starts
	// Example: clearing encoders, setting servo positions, ...
}

int bursts = 0;
int mode = 0;
int keep = 1;
void refresh()
{
	SensorValue(leftencoder) = 0;
	SensorValue(rightencoder) = 0;
}

void armcontrol(int c)
{
	motor[LeftLift] = -c;
	motor[RightLift1] = c;
	motor[RightLift2] = c;
}

void setMotors(int l,int r, int c, int f, int intk)
{
	motor[FrontRight]	=	-r;		//This should go Backwards
	motor[FrontLeft]	=	l;		//This should go Backwards
	motor[BackRight]	=	-r;		//This should go Backwards
	motor[BackLeft]		=	l;
	motor[LeftLift] = -c;
	motor[RightLift1] = c;
	motor[RightLift2] = c;
	motor[Funnel] = f;
	motor[Intake] = intk;
}

void StraightMotors(int l, int r)
{
	motor[FrontRight]	=	-r;		//This should go Backwards
	motor[FrontLeft]	=	l;		//This should go Backwards
	motor[BackRight]	=	-r;		//This should go Backwards
	motor[BackLeft]		=	l;
}

void Straight (int b)
{
	if(abs(SensorValue(rightencoder)/2) < SensorValue(leftencoder))
	{
		StraightMotors(b*5/6 ,b);
	}

	if(abs(SensorValue(rightencoder)/2) > SensorValue(leftencoder))
	{
		StraightMotors(b ,b*5/6);
	}
	if(abs(SensorValue(rightencoder)/2) == SensorValue(leftencoder))
	{
		StraightMotors(b ,b);
	}
}

void Foward(int x, int z, int v)
{
	motor[Funnel] = z;
	motor[Intake] = v;

	if(SensorValue(leftencoder) < (x-180) )
	{
		Straight(127);
	}

	if(SensorValue(leftencoder) < (x-90))
	{
		Straight(63);
	}

	if(SensorValue(leftencoder) > (x-90) && SensorValue(leftencoder) < x)
	{
		Straight(30);
	}
}

void Backwards(int x, int z, int v )
{
	motor[Funnel] = z;
	motor[Intake] = v;

	if(SensorValue(leftencoder) < (x-180) )
	{
		Straight(-127);
	}

	if(SensorValue(leftencoder) < (x-90))
	{
		Straight(-63);
	}

	if(SensorValue(leftencoder) > (x-90) && SensorValue(leftencoder) < x)
	{
		Straight(-40);
	}
}
task autonomous()
{
	pre_auton();

	while(bursts==0)
	{
		refresh();

		if(SensorValue(caurax) == 1)
		{
			while(abs(SensorValue(rightencoder)/2) < 210)
			{
				setMotors(-127,127,-10,0,-127);
			}

			while( bursts == 0)
			{
				setMotors(0,0,-10,0,-127);

				if(SensorValue(touch) == 1)
				{
					bursts = bursts + 1;
					wait1Msec(300);
				}
			}
		}

		if(SensorValue(caurax) == 0)
		{
			while(SensorValue(leftencoder) < 210)
			{
				setMotors(127,-127,-10,0,-127);
			}

			while( bursts == 0)
			{
				setMotors(0,0,-10,0,-127);

				if(SensorValue(touch) == 1)
				{
					bursts = bursts + 1;
					wait1Msec(300);
				}
			}
		}
	}

	while(bursts==1)
	{
		refresh();

		ClearTimer( time1[T1]);

		while(time1[T1] < 500)
		{
			Foward((510+90),0,-10);
			armcontrol(60);
		}
		while(SensorValue(leftencoder) < 510)
		{
			Foward((510+90),0,-10);
			armcontrol(15);
		}

		refresh();

		while(SensorValue(leftencoder) < 510)
		{
			Backwards((510+90),0,0);
			armcontrol(0);
		}

		while(bursts==1)
		{
			setMotors(0,0,0,0,0);

			if(SensorValue(touch) ==1)
			{
				bursts ++;
			}
		}
	}

However, when pressing the touch sensor, the robot does not respond. We checked through the debugger window and we know that the touch sensor can be sensed. Additionally, we originally used an && statement, but we found out that wasn’t the problem either.

any help would be greatly appreciated :slight_smile:

Since the bumper switch is working and the Cortex isn’t shot (it wouldn’t give you debugging feedback) my guess is that you left out a semicolon or there is a more complex error going on. If the button doesn’t serve some higher purpose it could just be replaced by a Wait1MSec(1000); that would allow you to automatically run the code after one second. Although quickly reading through the code (nice work by the way) it would require modification to make it work.

We tried this, but the code was still getting stuck somewhere. There has been some talk about us getting stuck in the global timer variable or encoder values for the lift, but we’re still uncertain

Why are you calling the pre-auton within your autonomous code?

IIRC, once your robot is connected to field control and on, pre-auton automatically runs.

This line

		ClearTimer( time1[T1]);

is incorrect and will crash ROBOTC

should be this

		ClearTimer( T1 );

Actually, we didn’t know that. Thanks for the tip

We took the line out and it looks like it works :). We’re curious though, why does cleartimer ( timer1(T1)) compile but not work?

You do want to keep ClearTimer(T1); in there, this will set timer 1 to 0.

ClearTimer wants a variable (a number) as it’s input. If you look in the header files you will see that T1 is really 0, T2 is 1 etc.

typedef enum short
{
  T1          = 0,
  timer1      = 0,
  T2          = 1,
  timer2      = 1,
  T3          = 2,
  timer3      = 2,
  T4          = 3,
  timer4      = 3
} TTimers;

You were sending it “time1[T1]” which is also a number, but by the time you got to that line of code it would have been larger than 3 (remember time1[T1] returns the current value of the timer), ClearTimer should really ignore this incorrect value but instead throws an exception and the program stops.

That makes sense. Thanks a bunch!!