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?
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.
#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
#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;
}
}
}