ButtonPressTime Reset doesn't work

Hi all,

I programmed an Arduino with 3 Pins with the following use:

  1. Press Enter
  2. Shutdown after pressed time of 5 seconds
  3. Restart after pressed time of 5 seconds

The program successfully notice the right pressed time for the buttons 2 und 3 but only once. If I want to use this again (e.g. after restart) a short press starts the routine.
Any idea? Thanks
My code:

#include <Keyboard.h>

#define BUTTON_ENTER_PIN 3
#define BUTTON_SHUTDOWN_PIN 6
#define BUTTON_REBOOT_PIN 9
#define REQUIRED_TIME 5000 // 5 Sekunden
#define KEY_KP_MINUS 0xDE

const char RunSequence[] = {
  KEY_LEFT_GUI, // Left-Windows-Key
  'r'
};
void setup() {
  // put your setup code here, to run once:
  pinMode(BUTTON_ENTER_PIN, INPUT_PULLUP);
  pinMode(BUTTON_SHUTDOWN_PIN, INPUT_PULLUP);
  pinMode(BUTTON_REBOOT_PIN, INPUT_PULLUP);
  digitalWrite(BUTTON_ENTER_PIN, HIGH);
  digitalWrite(BUTTON_SHUTDOWN_PIN, HIGH);
  digitalWrite(BUTTON_REBOOT_PIN, HIGH);
  Keyboard.begin();
}
void loop() {
  // put your main code here, to run repeatedly:
  // ENTER  
  static bool buttonEnterState = false;
  if (digitalRead(BUTTON_ENTER_PIN) == LOW)
  {
    if (!buttonEnterState)
    {
        // TODO: keyboard
        // Keyboard.press(KEY_ENTER);
		Keyboard.press(' ');
		delay(200);
		Keyboard.releaseAll();
        buttonEnterState = true;
    }
  }
  else
  {
    buttonEnterState = false;
  }
  // SHUTDOWN
  static bool buttonShutdownState = false;
  static int buttonShutdownPressTime = 0;
  if (digitalRead(BUTTON_SHUTDOWN_PIN) == LOW)
  {
    if (!buttonShutdownState)
    {
        buttonShutdownPressTime = millis();
        buttonShutdownState = true;
    }

    if (millis() - buttonShutdownPressTime > REQUIRED_TIME)
    {
        // TODO: keyboard
		for (int i = 0; i < 2; i++)
		  {
			Keyboard.press(RunSequence[i]);
    	}
    	delay(500);
    	Keyboard.releaseAll();
    	Keyboard.print("shutdown ");
      Keyboard.press(KEY_KP_MINUS);
      Keyboard.release(KEY_KP_MINUS);
      Keyboard.print("s ");
      Keyboard.press(KEY_KP_MINUS);
      Keyboard.release(KEY_KP_MINUS);
      Keyboard.print("t 5");
      Keyboard.releaseAll();
      delay(1000);
      buttonShutdownState = false;
    }
  }
  else
  {
    buttonShutdownState = false;
  }
  static bool buttonRebootState = false;
  static int buttonRebootPressTime = 0;
  if (digitalRead(BUTTON_REBOOT_PIN) == LOW)
  {
    if (!buttonRebootState)
    {
        buttonRebootPressTime = millis();
        buttonRebootState = true;
    }
    if (millis() - buttonRebootPressTime > REQUIRED_TIME)
    {
        // TODO: keyboard
        for (int i = 0; i < 2; i++)
		  {
			Keyboard.press(RunSequence[i]);
    	}
    	delay(500);
    	Keyboard.releaseAll();
      Keyboard.print("shutdown ");
      Keyboard.press(KEY_KP_MINUS);
      Keyboard.release(KEY_KP_MINUS);
      Keyboard.print("r ");
      Keyboard.press(KEY_KP_MINUS);
      Keyboard.release(KEY_KP_MINUS);
      Keyboard.print("t 5");
      Keyboard.releaseAll();
      delay(1000);
      buttonRebootState = false;
    }
  }
  else
  {
    buttonRebootState = false;
  }
}

Hi korki,

welcome to the arduino-forum.

Well done posting code as a code-section.

which exact type of arduino are you using?

my idea is to add more serial debug-output to trace what your code is really doing

another idea
using a state-machine instead of so many if-conditions

Hi StefanL38,

my board is Arduino Leonardo

Ok if it is a leonardo use a state-machine instead of so many if-conditions.
a state-machine reduces the number of if-conditions because each state (= mode of operation) is executed mutually exclusive.

This means code that should not be executed in a certain phase of your sequence will not executed.

modes of operation:

  • waiting for any keypress

  • if a keypress is detected start measuring time

  • check if key is pressed for 5 seconds continously

  • if pressed for 5 seconds continously send keyboard-presses
    if pressed for less than 5 seconds return to "waiting for any keypress"

the non-blocking nature of a state-machine will even enable to do things "in parallel"
like driving a display showing time = use as a clock

best regards Stefan

I used a tutorial to implement a state-machine. Works fine!
Great job. Thank you very much.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.