I'm working on a closed loop code executing specific tasks automatically , but there is a possibility to errors during one of these tasks .
so i need a reset button (only one ) to restart the function i need depending on the number of presses.
as an example :
3 presses perform function 1
5 presses perform function 2
7 presses perform function 3
9 presses (Terminate) the function u r doing now , (Reset) the counter, and (count again) to perform another one.
after several trials the best result i reached to, was a push button with a timer and counter
using goto(); function , but the problem is, it was not accurate and i failed to do this line
" 9 presses (Terminate) the function u r doing now , (Reset) the counter, and (count again) to perform another one. "
as when i press the pushbutton during the function , there is no attention at all, i must wait until the function ends and then press again and count ,and also it was not working well .
So i need an efficient way to do that (urgently) , and if i need an interrupt please explain how to do it with details in this case because I've never used it before .
Sgermany:
so i need a reset button (only one ) to restart the function i need depending on the number of presses.
as an example :
3 presses perform function 1
5 presses perform function 2
7 presses perform function 3
9 presses (Terminate) the function u r doing now , (Reset) the counter, and (count again) to perform another one.
In my view this is a completely impractical way to do what you want. The user will INEVITABLY press the button the wrong number of times.
there is no attention at all, i must wait until the function ends and then press again
This strongly suggests that the code is poorly planned and will need to be re-written so that the functions do not prevent the button being detected. Look at the demo several things at a time.
If you really want help post your code - and use code tags so it looks like this. If the code is very long it would be a great help if you create a short program that demonstrates the problem. For a long program be sure to provide a good plain-language description of how all the parts work.
★ LED on Arduino shows button presses, remains illuminated after timeout.
★ Print monitor shows detected presses on pin2
★ Can use jumper wire on pin2 to test (touch to GND).
// constants
const byte button = 2, led = 13;
// variables
unsigned long buttonState = 0xFFFFFFFF;
unsigned long buttonDebounce = 20; //response
byte count = 0, presses = 0;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(button, INPUT_PULLUP);
digitalWrite(led, LOW);
}
void loop() {
buttonFilter();
// your code starts here
}
void buttonFilter(void) {
if (millis() - buttonDebounce >= 40) // shift interval
{
buttonState = (buttonState << 1) | digitalRead(button); // shift and read
if ((buttonState & B111) == B011) // if rising and high for 2 stable reads
{
digitalWrite(led, LOW);
}
if ((buttonState & B111) == B100) // if falling and low for 2 stable reads
{
digitalWrite(led, HIGH);
count++;
}
buttonDebounce = millis();
}
if (buttonState == 0x7FFFFFFF && count > 0) // timeout
{
presses = count;
count = 0;
digitalWrite(led, HIGH);
Serial.print("presses = ");
Serial.println(presses);
}
}
but the problem is i need to terminate the function by pressing the switch for one time as an example and reset the counter and recount again
Yep, I know what you mean. The example I provided, after about 1 sec timeout, automatically resets the counter then recounts presses again (detected presses are displayed on the print monitor).
To help with your code specifically ... well, I think the problem is on line 31. (just a rough guess).
Sgermany:
but the problem is i need to terminate the function by pressing the switch for one time as an example .
and reset the counter and recount again
Sounds like you want to press the switch multiple times to select a function, then press it one more time to execute it? How are you going to be able to tell if a button press is the one press to execute the function, or just another press to select the function?
You need some other way to detect the end of the counting sequence, and the trigger for the function:
use another button to trigger the function
use a timeout so if a button isn't pressed within X milliseconds, the function is triggered
use a long press of the button to trigger the function
At the heart of the problem is that when you have recieved 3 button presses, you don't know if there's going to be a 4th. So the way that is determined is by setting a timeout to wait for the next button press. Unfortunately you cannot capture this "timeout" within an interrupt that is triggered by button presses. (the very act of NOT pressing the button ensures that the interrupt doesn't occur).
So besides your ISR that services the button presses (presumably incrementing a buttonCount and storing the time the last button was pressed) You would have another ISR based on the timer. This time dependant ISR would simply check if the timeout has passed since the last button press. If it has (AND buttonCount >0) it would then set another global variable (maybe called "Command") and reset buttonCount.
Any running function will need to periodically check the "command" variable to abort if necessary.