I am tinkering on clickbutton library.(https://code.google.com/p/clickbutton/) In the usage page,it says to avoid using delay because of interrupts.So it has to be delay without "delay".I have tried some but it doesn't work.Can you check it?
#include "ClickButton.h"
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds)
const int buttonPin1 = 4;
ClickButton button1(buttonPin1, LOW, CLICKBTN_PULLUP);
int LEDfunction = 0;
void delay8(){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// set the LED with the ledState of the variable:
digitalWrite(8, LOW);
}
}
void setup()
{
pinMode(8,OUTPUT);
// Setup button timers (all in milliseconds / ms)
// (These are default if not set, but changeable for convenience)
button1.debounceTime = 20; // Debounce timer in ms
button1.multiclickTime = 250; // Time limit for multi clicks
button1.longClickTime = 1000; // time until "held-down clicks" register
}
void loop()
{
// Update button state
button1.Update();
// Save click codes in LEDfunction, as click codes are reset at next Update()
if (button1.clicks != 0) LEDfunction = button1.clicks;
// Simply toggle LED on single clicks
// (Cant use LEDfunction like the others here,
// as it would toggle on and off all the time)
if(LEDfunction == 2) {
digitalWrite (8,HIGH);
delay8();
digitalWrite (8,LOW);
}
}
When you enter a double click the LED will go ON, then after a time out period it will go OFF,
however, since LEDfunction == 2 will still be true the LED will immediately turn back ON in the next loop.
This repeats over and over as long as LEDfunction == 2 is ture.
Your eye never sees the LED go off since things happen so fast. If you had a logic probe you would see it pulse on pin 8. Put at Serial.print after previousMillis = currentMillis and you will see it executed. OR you can but a delay(100); after the write low to stop the code for a while and you will see the led will go out.
After the delay8() in loop() why are you doing a digitalWrite, isn't that the job of your function?
You should get into the habit of always using unsigned long for variables that have something to with
millis() example: instead of long previousMillis = 0; use unsigned long previousMillis = 0;
Also IMO long interval = 1000; should be const unsigned long interval = 1000UL;
"digitalWrite (8,LOW);"This line is unnecessary.My mistake.
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// set the LED with the ledState of the variable:
digitalWrite(13, LOW);
delay(100);
}
Yes the led blinks.I understood the problem.How to solve it?Reset the ledfunction function?or buttonupdate after delay8 function?or any other solution?
Two things you have to do:
A. You have to clear the value of LEDfunction once there is a time out in delay8().
B. Then add the line:
// Save click codes in LEDfunction, as click codes are reset at next Update()
if (button1.clicks != 0)
{
LEDfunction = button1.clicks;
previousMillis = millis(); <<<<<< add this line so there is an initialized value once there is a new detection
}
EDIT:
Some advice, clean up your code a bit as it is hard to look at, remove some blank lines.
Place { on a new line.
I always use { and } in if statements rather than including code on the same line as the if(......)
Use Ctrl t to format your code into a more readable sketch.
I have added this code according to your last post and it works fine.
void delay8(){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// set the LED with the ledState of the variable:
digitalWrite(8, LOW);
LEDfunction = 0;
if (button1.clicks != 0)
{
LEDfunction = button1.clicks;
previousMillis = millis();
}
}
}