I am trying to create a program in which the following happens
if limit switch pressed 2 times within 4 seconds, increase speed
if limit switch pressed 3 times within 5 seconds, decrease speed
(ik how to increase/decrease my speeds but idk how to handle the press count and timers)
Please let me know if there is anyway to write this in robotc (not v5)
there is a lot that goes into something like this. First you need a variable that, once the limit switch is pressed, starts integrating itself each time the program makes a full cycle. Then you would probably want a variable for storing the total number of presses of the limit switch. You also would want a boolean variable to log the last state of the limit switch
Now you need something to check if the limit switch has been pressed or not using the currently state of the limit switch and the previous state as saved in your logging variable.
Psuedo code
bool timerOn = false;
bool previousState = false;
while(1){
if(timerOn == true){
int timer =+ 20; //add by the number of milliseconds waited at the end of the while loop
}
if(limitswitch == 1 && previousState == false){//if the switch has just now been pressed
int pressCount ++;
timerOn = true;
}
if(time <= 3000 && pressCount == 2){
execute action 1
}
if(time <= 5000 && pressCount == 3){
execute action 2
}
if(timer > 5000){ //this value would be the maximum amount of time (in milliseconds) the longest lasting action would need to read for IE. 3 presses within 5 seconds
timer = 0;
timerOn = false;
pressCount = 0;
}
waitMs(20);
}
!* Note: there is still an issue inside of this code that you will need to solve. That issue being that once the switch is pressed twice the first action will already execute, so you need to find a way to allow the second action to also execute *!