Instantly share code, notes, and snippets.
mikeflynn/dnd_button-itsybitsy Secret
Last active Jun 17, 2020
Arduino code for DND button
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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