Limit Switch Counter Programming Help

Hi, I need to make a program that counts how many times the limit switch is hit. Every time the limit switch is hit I need it to add 1 to the toal and I need to be able to see the live total. I also need to be able to reset the count. How can I do this? This is what I have below but it doesn’t show the count. Please help. Thanks.



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

task main()
{
   int w = 0;

   while (1==1)
   {
      if (SensorValue(limitswitch) == 1)
         w = w + 1;


   }
}





 task main()
{
   int w = 0;
   bool reset;

   while (true)
   {
      if (SensorValue(limitswitch) == 1){
              w+=1;
	      while(SensorValue(limitswitch)==1){
	      	
	      }
       }
       if (reset==true){
	    	w=0;
	    	reset=false;
        }

   }
}


what the while loop does after counting up one is it lets the program count once when the limit switch is hit, then wait until it is released to go to the next count. Otherwise, it would continue to count while the limit switch is held down. Unfortunately, in this case, while the limit switch is held down, the count cannot reset.

Note also that something has to happen to make the boolean reset true.

I’d recommend using tasks instead of a one-string execution, or a boolean statement on the state of the limit switch if you want to be able to reset while the limit switch still held down.

probably not the best way of coding this, but here it is anyway.



task main()
{
   int w = 0;
   bool released = true;
   while (1==1)
   {
      if(SensorValue(limitswitch) == 1 && released){
            w++; //*same as w = w+1*//
            released = false;
      }
     //*makes it so you have to release the button and press it again to add to w, this stops the variable from increasing infinitley*//
      if(SensorValue(limitswitch) = 0){
         released = true;
      }

      //*reset code, replace "action" with whatever triggers the reset, example: vexRT[Btn5D]*//
    if(action){
      w = 0;
    }

   }
}
 

Hi, I just need something to count how many times the limit switch has been pressed and when it has been pressed 10 times it turns on an LED. Then I can hit a bumper switch and reset w to 0. Is this possible?

How do I see what w equals?

what do you mean? like an if statement to find when x >= 10? or something that outputs x to an LCD?

I am using this limit switch to count something repeatedly for testing purposes. I have it plugged into my computer. When the limit switch is hit 10 times I need a green LED to turn on. After the green LED turns on I need to be able to hit a bumper switch to reset w and restart. I do not have an LCD but can use my computer to see results. This is not going to be used on a Robot.

I am using this limit switch to count something repeatedly for testing purposes. I have it plugged into my computer. When the limit switch is hit 10 times I need a green LED to turn on. After the green LED turns on I need to be able to hit a bumper switch to reset w and restart. I do not have an LCD but can use my computer to see results. This is not going to be used on a Robot.

This might help. As for activating an LED once w hits 10, just add this to the loop.

 
//*>= is greater than/equal to*//
if(w >=10){
//*replace led with one of the digital ports, e.g. "dgtl5"*//
SensorValue[led] = true;
}

This is the code but it doesn’t work? Why?


#pragma config(Sensor, dgtl9,  bumper,         sensorTouch)
#pragma config(Sensor, dgtl10, limitswitch,    sensorTouch)
#pragma config(Sensor, dgtl11, led,            sensorLEDtoVCC)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
int w = 0;
bool released = true;
while (1==1)
{
if(SensorValue(limitswitch) == 1 && released){
w++; //*same as w = w+1*//
released = false;
}
//*makes it so you have to release the button and press it again to add to w, this stops the variable from increasing infinitley*//
if(SensorValue(limitswitch) = 0){
released = true;
}
//*reset code, replace "action" with whatever triggers the reset, example: vexRT[Btn5D]*//
if(SensorValue(bumper) = 1){
w = 0;




//*>= is greater than/equal to*//
if(w >=10){
//*replace led with one of the digital ports, e.g. "dgtl5"*//
SensorValue(led) = true;
}


}
}
}

some brackets in the wrong place, this should fix it

 
#pragma config(Sensor, dgtl9,  bumper,         sensorTouch)
#pragma config(Sensor, dgtl10, limitswitch,    sensorTouch)
#pragma config(Sensor, dgtl11, led,            sensorLEDtoVCC)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
int w = 0;
bool released = true;
while (1==1)
{
if(SensorValue(limitswitch) == 1 && released){
w++; //*same as w = w+1*//
released = false;
}
//*makes it so you have to release the button and press it again to add to w, this stops the variable from increasing infinitley*//
if(SensorValue(limitswitch) = 0){
released = true;
}
//*reset code, replace "action" with whatever triggers the reset, example: vexRT[Btn5D]*//
if(SensorValue(bumper) = 1){
w = 0;
}



//*>= is greater than/equal to*//
if(w >=10){
//*replace led with one of the digital ports, e.g. "dgtl5"*//
SensorValue(led) = true;
}


}
}

Also, the program is not writing to the console. Right now you will not be able to see the current value of W

I do not need to see the value of w as long as the led turns on when w hits 10. I just tried it and the code did not work.

Let me try changing it to my ports.

did you get the new code? also, if that doesn’t work, change


if(SensorValue(limitswitch) = 0){
released = true;
}

to


if(SensorValue(limitswitch) != 1){
released = true;
}

The LED does not turn on after 10 or more clicks. How can I fix that? The LED is in digital 11.

did you remember to switch dgtl11 to dgtl12? could you post your current code?

This code successfully turns on the LED



#pragma config(Sensor, dgtl11, led,            sensorLEDtoVCC)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{

turnLEDOff(led);


}

Here is the current code-




#pragma config(Sensor, dgtl9,  bumper,         sensorTouch)
#pragma config(Sensor, dgtl10, limitswitch,    sensorTouch)
#pragma config(Sensor, dgtl11, led,            sensorLEDtoVCC)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
int w = 0;
bool released = true;
while (1==1)
{
if(SensorValue(limitswitch) == 1 && released){
w++; //*same as w = w+1*//
released = false;
}
//*makes it so you have to release the button and press it again to add to w, this stops the variable from increasing infinitley*//
if(SensorValue(limitswitch) = 0){
released = true;
}
//*reset code, replace "action" with whatever triggers the reset, example: vexRT[Btn5D]*//
if(SensorValue(bumper) = 1){
w = 0;
}



//*>= is greater than/equal to*//
if(w >=10){
//*replace led with one of the digital ports, e.g. "dgtl5"*//
SensorValue(led) = true;
}


}
}

Sorry, I meant the LED is in digital 11 not 12.

try replacing


SensorValue(led) = true

with


turnLEDOn(dgtl11);