Skip to content

Instantly share code, notes, and snippets.

@mikeflynn
Last active June 17, 2020 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeflynn/7152549417f26e8c7a5d3cc23822b66e to your computer and use it in GitHub Desktop.
Save mikeflynn/7152549417f26e8c7a5d3cc23822b66e to your computer and use it in GitHub Desktop.
Arduino code for DND button
#include <Keyboard.h>
#include <EEPROM.h>
// DND Button Config
const int dndButtonPin = 13;
const int dndLedPin = 11;
const int eepromKey = 0;
int lastButtonState = HIGH;
int dndButtonState = 0;
int dndButtonStateCounter = 11; // Starting at 11 to avoid the first ghostly press
unsigned long previousMillis = 0;
int firstButtonPress = 0;
void setup() {
pinMode(dndLedPin, OUTPUT);
pinMode(dndButtonPin, INPUT);
lastButtonState = EEPROM.read(eepromKey);
Keyboard.begin();
digitalWrite(dndButtonPin, HIGH); // Fix to stablize button state
while (!Serial) ;
}
// the loop function runs over and over again forever
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 100) {
previousMillis = currentMillis;
// DND Button Loop
dndButtonState = digitalRead(dndButtonPin);
if (dndButtonState != lastButtonState) {
Serial.println("DIFFERENT STATE DETECTED");
dndButtonStateCounter = 0;
if(dndButtonState == LOW) {
// Serial.println("LOW");
// Turn on LED
digitalWrite(dndLedPin, HIGH);
} else {
// Serial.println("HIGH");
// Turn off LED
digitalWrite(dndLedPin, LOW);
}
} else {
if(dndButtonStateCounter == 10) {
Serial.println("Action starting...");
dndButtonStateCounter++;
if(dndButtonState == LOW) {
Serial.println("START DND");
keyboardStartDND();
} else if (dndButtonState == HIGH) {
Serial.println("END DND");
keyboardEndDND();
}
} else if (dndButtonStateCounter < 10) {
Serial.print("COUNTER: ");
Serial.println(dndButtonStateCounter);
dndButtonStateCounter++;
}
}
lastButtonState = dndButtonState;
EEPROM.write(eepromKey, lastButtonState);
}
}
void openTerminal() {
Keyboard.press(KEY_LEFT_GUI);
delay(100);
Keyboard.press(' ');
delay(100);
Keyboard.releaseAll();
delay(500);
Keyboard.print("terminal");
delay(500);
Keyboard.press(KEY_RETURN);
Keyboard.release(KEY_RETURN);
delay(500);
}
void keyboardEndDND() {
openTerminal();
Keyboard.println("dnd.sh off");
}
void keyboardStartDND() {
openTerminal();
Keyboard.println("dnd.sh on");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment