Here is what you have asked for:
I am making home coming/leaving light for car. First i want that
whenver i press car key lock a timer will start for 10 second.And if i
press button twice within 10 second pin 13 will high remain for 10
second and if i cant press the button it return to the void loop aor
condition may get false. I am working on it but dont getting the
correct approach . But i am not able to use millis twicely in this
program.
So, I have refactored, and done my best to interpret your words, and the code that you wrote.
You will see that I broke down quite a bit the deep nesting of your code into separate methods. I hope that you see that it makes it more readable and understandable.
I have replaced the "magic numbers" like 10000
with more readable code like tenSeconds
. And I hope the code does what you want now, but I'm not sure. Hopefully the readability has improved enough that you can massage and reshape the code into what you want it to do, as well as shown you what direction to head when you don't understand your own code because it has grown (deeper, longer, more complicated, etc.) as most code does.
In the professional coding world, allowing code to remain like that is called "not paying down your design debt" and you (or your organization) will pay the consequences, with interest, just like a credit card.
Finally, note that my comments don't restate the code, but tries to add something (mine often tries to finish sentences involving readable code). My comments also tend to be on the ends of lines to try to stay out of the way.
This code compiles, but has not been tested on an actual Arduino. Please let me know if this meets your need, and please choose this as the answer if it does answer your question, and at least please vote up the answer if it doesn't quite answer you... ...and give me a second chance, if possible. Thanks.
// pins defined and named
int pin_button = 8; // button Pin
int pin_LED = 13; // LED Pin
int currentButtonState = 0; // current state of the button
int lastButtonState = 0;
int buttonPushCounter = 0; // counter for the number of button presses
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
const unsigned int tenSeconds = 10000; // 10,000 milliseconds
void setup()
{
pinMode(pin_button, INPUT_PULLUP);
pinMode(pin_LED, OUTPUT);
}
void loop()
{
currentMillis = GetCurrentMillis();
if (ButtonIsToggled() == 1)
{
CountButtonPushes();
}
}
int ButtonIsToggled()
{
int isToggled = 0;
currentButtonState = digitalRead(pin_button);
if (currentButtonState != lastButtonState)
{
lastButtonState = currentButtonState;
isToggled = 1;
}
return isToggled;
}
void CountButtonPushes()
{
int valueToWrite = LOW; // LED-OFF is default value unless overridden
if (currentButtonState == LOW) // we just toggled from HIGH to LOW...
{
buttonPushCounter++; // We've successfully completed another button-press
if (buttonPushCounter % 3 == 0) // if (buttonPushCounter==3) should be faster
{ // because compare is faster than division
buttonPushCounter = 0; // Reset
valueToWrite = HIGH; // LED-ON
}
}
if (valueToWrite == HIGH) // LED is ON
{
digitalWrite(pin_LED, HIGH); // valueToWrite
while (currentMillis - previousMillis < tenSeconds)
{
// do nothing -- wait while LED is on
// busy wait. Consider sleeping instead.
currentMillis = GetCurrentMillis();
}
previousMillis = currentMillis;
digitalWrite(pin_LED, LOW);
}
}
unsigned long GetCurrentMillis()
{
return millis();
}
millis()
at all in this program. You call it once at save the time stamp into a variable that you don't use afterwards.