Counting time of multiple button press to the maximum time

Hello,

I am a prety new in the world of arduino, my standard job is PLC programmer and developer of industrial automation, but arduino and microcontrollers is quite new world. I eed to used get familiar withthe understanding arduino programming.

I want to messure time of multiple button press.
For example when I start aplication the time is 0. I sub current millis from beginmillis, and I get some value. Now my question is how to add this value to the next button press, because I want to limit all presses to the 5000ms.

Example: First press for 1500ms + second pres for 1000ms = 2500ms.
So when I go to automatic mode or I make third press we have only 2500ms time lapsed.

Thank you for your help.

Best regards
Andrej

I think you can:

  1. detect the pressing event and save millis to a variable: ex. pressed_time
  2. detect the releasing event and save millis to a variable: ex. released_time
  3. calculate the pressing time: pressing_time = released_time - pressed_time.

You can detect the pressing and releasing event by detecting the state change. See this tutorial

Make all time measurements relative to the current value of millis(). You never need to know the actual value, just how long has passed since a previous action

action 1 happens at x
action 2 happens at y
action 3 happens at z

first period = y - x
second period = z - y
total elapsed time = z - x

// A basic retentive timer.  Runs when enabled.
// Holds value when enable false.  Reset when
// resetPin goes low.  Reset overrides enable

uint32_t accValue;
uint32_t preset = 3000; // 3 seconds
uint32_t previousMillis;
uint32_t currentMillis;

const byte enablePin = 8;
const byte resetPin = 10;
const byte doneLED = 13;

void setup() {
  Serial.begin(115200);
  pinMode(enablePin, INPUT_PULLUP);
  pinMode(resetPin, INPUT_PULLUP);
  pinMode(doneLED, OUTPUT);
}

void loop() {
  currentMillis = millis();
  if (digitalRead(enablePin) == LOW) { // pushbutton pressed, run timer
    accValue = accValue + currentMillis - previousMillis;
  }
  
  if (accValue >= preset) {
    accValue = preset;
    digitalWrite(doneLED, HIGH);
  }
  else digitalWrite(doneLED, LOW);
  previousMillis = currentMillis;
  
  if (digitalRead(resetPin) == LOW) accValue = 0;

  Serial.println(accValue); // if needed
}

first of all what are you trying to acheive? i would guess you want different results from a single button. are you looking to simply detect double/tripple clicks? normally you would reset a small timer for every consecutive click and use a click counter varible. otherwise, if you try measure all the clicks in one time frame it would get difficult for the user be cause you would have to click faster as you wanted to give more clicks.

another consideration is weather you want a result for every click or just the last. if you use my mentioned approach the delay would be shorter because obviosly you would need to wait a moment before you "do stuff" to be sure the multiple clicks are done.

another easy way to get different results from the same button is to measure how long it is held down.

Anyways, if you post what you have so far for a basic push detection I can help you add to it.
Also please include what you want your button to do and why

Thank you all for your answers.

This counting time is only beggining of my project.
With my project I would like to control blinds in two ways (this is only motor without switches so only cloasing and opening walue is time).
One is automatic way which is more simple when I make automatic blind open and counting time when time elapsed relay goes off same situation is with closig blinds ( in future I would like to store opening and closing time to eepron or flash automatic lerning closing and opening time).
Second is manual control with button so when person hold button there is no insurance that blind will be completely open or closed, and because of this I need to count time and when time elapsed even if the button is pressed relay signal must go off.

I saw there is also blinds library but I dont know how to use it or implement to program because of this I start my program from beggining.

This is only beggining of automation which I would like to build.

Thank you for all of your help

Best regards

I saw there is also blinds library

Where did you see this library ?

I get library here:

But I do not know how to use it. Because in house I have 11 blinds to control.

That library seems overly complicated for what you want to do

what do you have so far? do you have a board? do you have a button ?

if you are unfamiliar with arduino programming, Google a basic button tutorial and get it working.
you could use an LED for testing in replace of the motors. When that is working post the code that you have and i can show you how to detect your double clicks and holding a button down.