How to make cooldown timer for button

I am looking for a way to add a 4 second cool down on using the button ‘w’. How do I achieve this?

1 Like

You need 3 things:

  • A variable to store the state of the button:

    • Active
    • Not active
  • Some sort of a clock to measure the time elapsed after a hit on the button: you can use the elapsedTime() function or use a library (I think @GoToLoop has one available)

  • An update method that change the state of the button by checking the elapsed time since the last hit and the 4 seconds threshold you want to apply

Without any code we can’t really help you more.


button ‘w’ or key ‘w’ ??
use time from

long nowpress, lastpress, cooldown=4000;

void setup() {
void draw() {
void keyReleased() {
  if ( key == 'w' ) {
    nowpress = millis();
    if (nowpress > ( lastpress + cooldown )) {
      println("ok, got it");
      lastpress = nowpress;
      // need more of your code what to do at key 'w'
    } else {
      println("not now");
1 Like

You can also create a general-purpose cool-down timer for any number of buttons by creating a HashMap. The key is the character, the value is the time (millis or frame) in the future when the cooldown expires. When you get a new keypress, check the map.

  1. If the key exists and the timer hasn’t expired, ignore it.
  2. Else put the key/value and do the keypress.