one button start on timer with increase time for 2nd or more press

Hi All,

im new here, currently i'm working a timer project with only 1 button to start timer & increase time if current time not end . exp :on timer time set 20sec, if within 20sec i press again the button it will add another 20sec on current time.

but i try many way it cant work :'(

below my code please help & advise me Thanks:

define led 13

boolean Rled = false; long current = 0; long RoffTime = 0; boolean Rledon = false; boolean Rledoff = false;

const int buttonPin = 2; int buttonState = 0;

void setup() { pinMode(buttonPin, INPUT); pinMode(13, OUTPUT);

}

void loop() { if (digitalRead(buttonPin) == HIGH) { current = millis()+ 20000; Rled = false; Rledon = false; Rledoff = true; digitalWrite(led, HIGH);

} if((millis() > (current + RoffTime)) && Rledoff == true && Rledon == false){ Rledon = true;

} else { if(Rledon == true){ digitalWrite(led, LOW); Rledoff = true; } } }

couldn't follow logic of your code

i believe the following does what you intended, demonstrates detection of a button press (not simply held) and how to time events without blocking

#if 0
# define led 13
const int buttonPin = 2;

#else
# define led 10
const int buttonPin = A1;
#endif

#define ON      LOW
#define OFF     HIGH
#define PRESSED LOW

void setup () {
   pinMode (buttonPin, INPUT_PULLUP);

   pinMode (led, OUTPUT);
   digitalWrite (led, OFF);
}

// turn led on when button pressed
// extend time if button pressed while LED on
void loop () {
   // turn off LED when time expired
   static unsigned long msecLst = 0;
          unsigned long msec    = millis ();

#define ON_TIME  3000

   if (msecLst && msec - msecLst > ON_TIME)  {
       msecLst = 0;
       digitalWrite (led, OFF);
   }

   // detect button press
   static byte butLst = ! PRESSED;
          byte but    = digitalRead (buttonPin);

   if (butLst != but)  {
       butLst = but;

       if (PRESSED == but)  {
           msecLst = msec;
           digitalWrite (led, ON);
       }
   }
}

Hi gcjr,

Thanks for help, sorry for my bad to confuse you,

actually i need a timer :

1- preset 20sec once press led will on for 20sec 2- if within 20sec i press again it will extend another 20sec on the current time (example: current time balance 10sec it add 20sec = 30sec) same go if i press the button 3 times = 20sec x 3 + current balance time.

Thanks

when a button is pressed and msecLst is not set, it needs to be set to msec. If msecLst is set, add 20000 to it.

Hi Gcjr,

Sorry my bad, i have trying many way follow your advise but still unable, could you please advise.

Thanks

const byte ButtonPin = 2;


unsigned long StartTime = 0;
unsigned long Interval = 0;


void setup()
{
  pinMode(ButtonPin, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}


void loop()
{
  unsigned long currentMillis = millis();


  if (digitalRead(ButtonPin) == HIGH)
  {
    if (StartTime != 0)
    {
       // Timer already running
      Interval += 20000UL;   // Increase the timer interval by 20 seconds
    }
    else
    {
      StartTime = currentMillis;
      Interval = 20000UL;  // Start with a 20-second interval
      digitalWrite(LED_BUILTIN, HIGH);  // LED on
    }
  }


  // If the timer is running, test to see if the timer has expired
  if (StartTime != 0 && currentMillis - StartTime >= Interval)
  {
    // Timer has finished
    digitalWrite(LED_BUILTIN, LOW);  // LED off
    StartTime = 0; // Timer not running
  }
}

Hi Johnwasser.

Thanks, It work.

Really Appreciate your help