Hi all,
I programmed an Arduino with 3 Pins with the following use:
- Press Enter
- Shutdown after pressed time of 5 seconds
- 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;
}
}