I have tried several examples and while some work well but have terrible predictability for single press vs double press while others that have great button use but no double click if you will. I need something that I can piggyback an existing momentary switch with that when it's pressed once it does nothing and when pressed twice within 500ms, it will latch an output and when pressed twice again in quick succession, will unlatch the output. I've tried some of the multi switch examples without success. I thought about counting presses but I'm not having any success with it resetting after a second and then if it does work, the other part of it doesn't.
I played around with that sketch and made a few changes. I'm guessing that I will not be able just run a simple pin high/delay/low/delay to generate a frequency out so how would you recommend I continue. I tried to use the serial portion as well but the examples I've tried to combine are without success.
byte ButtonPin = 2; //digital pin 2
byte LEDpin = 13; //onboard LED
byte FREQpin = 10; //frequency output
int button = LOW;
int lastButtonState = LOW;
int type,lastType=0;
long time=0;
int flagged = 0;
long newT=0;
int output = 0, lastOut;
int cnt=0;
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup() {
pinMode(ButtonPin, INPUT);
pinMode(LEDpin, OUTPUT);
pinMode(FREQpin, OUTPUT);
//Serial.begin(9600);
//Serial.println("ready");
}
void loop() {
button = digitalRead(ButtonPin);
if (button != lastButtonState) {
lastDebounceTime = millis();
lastButtonState = button;
time = millis();
}
while( millis() - time < 1500){
type = check(time);
}
if(type != lastType){
lastType = type;
//Serial.println(type);
switch(type){
case 0:
break;
case 1:
//Serial.println("pressed 1 time(s)");
break;
case 2:
//Serial.println("pressed 2 time(s)");
digitalWrite(13, !digitalRead(13));
break;
case 3:
//Serial.println("pressed 3 time(s)");
break;
}
}
type = 0;
}
int check(long time){
newT=0;
if ((millis() - lastDebounceTime) > debounceDelay) { // debounce button
newT=(millis() - time);
if(newT < 500) // check if button is pressed within half a second
{
button = digitalRead(ButtonPin);
if(button == HIGH)
{
if(!flagged) // latch + lockout that prevents cnt to increase when button is held
{
cnt++;
flagged = 1;
output = cnt;
}
}
else {
flagged = 0;
}
}
else {
cnt=0;
if(output != lastOut) // prevents continuous stream of values, and only when the value changes, display it.
{
lastOut = output;
return output;//Serial.println(cnt);
}
}
}
}
Here is the sketch above with changes to change the LED state but I can't figure out how to add in what I need it to do like the legacy circuit to generate the frequency such as Circuit Simulator Applet
I know I can make that circuit work with the arduino as it is right now by using the arduino to drive a relay that would supply current to the 555 timer but I'd like to try to get this to work without the legacy components if possible as I think it will make it much easier to adapt it to other applications in the future.
I would tackle the problem in several parts, because you have several distinct algorithms to implement.
Firstly, debounce the raw switch state to get a clean switch state. The IDE comes with examples that demonstrate this.
Apply edge detection to the debounced signal to detect button presses.
Compare the time of the current click against the time of the preceding click to work out whether this press constitutes a double-click. if you want to handle single clicks too then I would implement this as a finite state machine (with two states) so that you can apply a timeout between clicks and infer a single click.
Use a boolean variable as a toggle that you can invert each time a double click is detected.
This doesn't require any looping control structures and everything you need to implement would be in loop(), or functions you define called from it.
Everyone else that has posted did so with the intent of trying to help me with this project but yours carries quite a negative vibe so please feel free to show everyone how you'd do what I'm trying to accomplish without doing any of what you posted in regards to.
The two codes I gave should work. The first one should toggle the LED when double clicked, and the second code shows you how to create a 1 Hz pulse @ 50% duty cycle.
Try the first code again, I added a new line that I forget to mention. Sorry about that. Make sure you button is wired properly so it's normally low then when pressed, goes high.