Inconsistent code, help!

Every time I push the limit switch the variable is a different number? Why? It should just add 1 every time? How can I fix this. I the LED is to turn on after 10 hits on the limit switch and the bumper resets the count and turns off the light.


#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(SensorValue[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;

		}
 
	}
}


Add a wait1Msec(100); at the end of the while loop.

Thank You, that fixed it but now it is not turning on the LED?? Here is the code-


#pragma config(Sensor, dgtl10, limitswitch,    sensorTouch)
#pragma config(Sensor, dgtl11, 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(SensorValue[limitswitch]==1){
			wait1Msec(100);

			}

		}
		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;

		}
 
	}
}




Never mind about the LED… I didn’t have my cortex battery on or on. Thank You for your help.

No probem, I’m glad you were able to figure it out.