Weird Programming Error, Help!

Hi, I have some programming issues that I need help with. I need code so that when a limit switch is hit 10 times it turns on a LED and when the LED turns on I have another bumper switch to reset the counter and starts over the 10 times. I have posted some code below. The first set of code works with a vex controller. The second set of code does not work with a limit switch and bumper. Can anyone help?


#pragma config(Sensor, dgtl9,  bumper,         sensorNone)
#pragma config(Sensor, dgtl10, limitswitch,    sensorNone)
#pragma config(Sensor, dgtl11, led,            sensorNone)
#pragma config(Sensor, dgtl12, red,            sensorLEDtoVCC)
#pragma config(Motor,  port2,           TestMotor,     tmotorServoStandard, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

//!!Code automatically generated by 'ROBOTC' configuration wizard               !!//

task main()
{

int lastButtonPressed = 0;
int counter = 0;
while(1==1){
    if(vexRT[Btn6U]==1 && lastButtonPressed==0){
        lastButtonPressed=1;
counter=counter+1;
    }
    else if(vexRT[Btn6U]==0 && lastButtonPressed==1){
        lastButtonPressed=0;
    }
    if(vexRT[Btn6D]==1){
        counter=0;
    }
    if(counter>=10){
        turnLEDOn(red);
    }
    else{
        turnLEDOff(red);
    }

   
}
}







#pragma config(Sensor, dgtl9, bumper, sensorTouch)
#pragma config(Sensor, dgtl10, limitswitch, sensorTouch)
#pragma config(Sensor, dgtl11, led, sensorLEDtoVCC)
task main()
{

int lastButtonPressed = 0;
int counter = 0;
while(1==1){
if(SensorValue[limitswitch]==1 && lastButtonPressed==0){
lastButtonPressed=1;
counter=counter+1;
}
else if(SensorValue[limitswitch]==0 && lastButtonPressed==1){
lastButtonPressed=0;
}
if(SensorValue[bumper]==1){
counter=0;
}
if(counter>=10){
SensorValue[led]=1;
}
else{
SensorValue[led]=0;
}
}
}



Try this, although I think your code should have worked. It may be that you used turnLEDOn in your first code and you used =1 instead of =true or turnLEDOn in the second.


#pragma config(Sensor, dgtl9, bumper, sensorTouch)
#pragma config(Sensor, dgtl10, limitswitch, sensorTouch)
#pragma config(Sensor, dgtl11, led, sensorLEDtoVCC)

task main()
{

	bool buttonPressed=false;
	int counter = 0;
	while(true){
		if(SensorValue[limitswitch]==1 && buttonPressed==false){
			buttonPressed=true;
			counter+=1;
		}
		else if(SensorValue[limitswitch]==0 && buttonPressed==true){
			buttonPressed=false;
		}

		if(SensorValue[bumper]==1){
			counter=0;
		}

		if(counter>=10){
			SensorValue[led]=true;
		}
		else{
			SensorValue[led]=false;

		}

	}
}

No, it doesn’t work. The LED turns on after 1 click on the limit switch. It does turn off after the bumper switch it hit though…

No, it doesn’t work. The LED turns on after 1 click on the limit switch. It does turn off after the bumper switch it hit though…

@henryl Do you have any other ideas on why it doesn’t work? I am trying to use this device to count something every time it hits the switch but I could use other sensors? Maybe every time something passed a sensor it counted it? I don’t know what sensor would do it or how to program it. I know how to use this limit switch but the code doesn’t work for some reason.

Try downloading to robot and going to Robot>Debugger Windows>Global Variables and click “start” on debugging and see what happens when you hold down the limit switch. Does it just increase infinitely?

Doing it now.

It doesn’t show anything. It just turns on after 1 click.

Same Thing, shows nothing and turns on after 1 click.

insert


writeDebugStream("int counter is: %d", counter)

into the start of the while loop

then when you download go to robot>debugger windows> debugger stream when you download and it should print out the value of int “counter”

Here is the new code I am using when I added that line-



#pragma config(Sensor, dgtl1,  limitswitch,    sensorTouch)
#pragma config(Sensor, dgtl2,  bumper,         sensorTouch)
#pragma config(Sensor, dgtl12, led,            sensorLEDtoVCC)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{

	bool buttonPressed=false;
	int counter = 0;
	while(true){
		if(SensorValue[limitswitch]==1 && buttonPressed==false){
			buttonPressed=true;
			counter+=1;
			writeDebugStream("int counter is: %d", counter); 
		}
		else if(SensorValue[limitswitch]==0 && buttonPressed==true){
			buttonPressed=false;
		}

		if(SensorValue[bumper]==1){
			counter=0;
		}

		if(counter>=10){
			SensorValue[led]=true;
		}
		else{
			SensorValue[led]=false;

		}

	}
}


To get the variables to show I have to click the “step” button and then it shows the variables but then they don’t update until I click stop, start, click limit switch, then click “step”.

Here is after 1 click, it seems to be inconsistent…

I am sorry, I don’t think I quite understand where to put it,
Here is the code I have now…



#pragma config(Sensor, dgtl1,  limitswitch,    sensorTouch)
#pragma config(Sensor, dgtl2,  bumper,         sensorTouch)
#pragma config(Sensor, dgtl12, led,            sensorLEDtoVCC)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
 
	bool buttonPressed=false;
	int counter = 0;
	while(true){writeDebugStream("int counter is: %d", counter); 
		if(SensorValue[limitswitch]==1 && buttonPressed==false){
			buttonPressed=true;
			counter+=1;

		}
		else if(SensorValue[limitswitch]==0 && buttonPressed==true){
			buttonPressed=false;
		}

		if(SensorValue[bumper]==1){
			counter=0;
		}

		if(counter>=10){
			SensorValue[led]=true;
		}
		else{
			SensorValue[led]=false;

		}
 
	}
}





			while(limitswitch==1){
				
			}

add this right on the line right after counter +=1

and that placement works
It seems that the counter is ticking up without bounds.

This gives me compiling errors-



#pragma config(Sensor, dgtl1,  limitswitch,    sensorTouch)
#pragma config(Sensor, dgtl2,  bumper,         sensorTouch)
#pragma config(Sensor, dgtl12, led,            sensorLEDtoVCC)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
 
	bool buttonPressed=false;
	int counter = 0;
	while(true){writeDebugStream("int counter is: %d", counter); 
		if(SensorValue[limitswitch]==1 && buttonPressed==false){
			buttonPressed=true;
			counter+=1;
			while(limitswitch)==1{
				
			}

		}
		else if(SensorValue[limitswitch]==0 && buttonPressed==true){
			buttonPressed=false;
		}

		if(SensorValue[bumper]==1){
			counter=0;
		}

		if(counter>=10){
			SensorValue[led]=true;
		}
		else{
			SensorValue[led]=false;

		}
 
	}
}





File “.\SourceFile002.c” compiled on Dec 30 2017 14:31:52
Error:Unexpected ‘==’ during parsing
Error:Unexpected ‘1’ during parsing

made a change to the conditional after while, check my last post

this post?

yeah, look at the parentheses placement

This code gives me these compiling errors now-



#pragma config(Sensor, dgtl1,  limitswitch,    sensorTouch)
#pragma config(Sensor, dgtl2,  bumper,         sensorTouch)
#pragma config(Sensor, dgtl12, led,            sensorLEDtoVCC)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
 
	bool buttonPressed=false;
	int counter = 0;
	while(true){writeDebugStream("int counter is: %d", counter); 
		if(SensorValue[limitswitch]==1 && buttonPressed==false){
			buttonPressed=true;
			counter+=1;
	while(limitswitch==1){
				
			}

		}
		else if(SensorValue[limitswitch]==0 && buttonPressed==true){
			buttonPressed=false;
		}

		if(SensorValue[bumper]==1){
			counter=0;
		}

		if(counter>=10){
			SensorValue[led]=true;
		}
		else{
			SensorValue[led]=false;

		}
 
	}
}

File “.\SourceFile002.c” compiled on Dec 30 2017 14:38:14
Warning:Mismatched typedefs. Converting typedef ‘char’ to typedef ‘tSensors’, value ‘in2’
Warning:‘while’ expression is constant ‘false’. Loop never executed.

Sorry, replace the new while loop with


while(SensorValue[limitswitch]==1){

			}

I forgot it was a sensor XD.