Skip to content

Instantly share code, notes, and snippets.

@mikeflynn mikeflynn/dnd_button-itsybitsy Secret
Created May 17, 2020

Embed
What would you like to do?
Arduino code for DND button
#include <Keyboard.h>
// DND Button Config
const int dndButtonPin = 13;
const int dndLedPin = 11;
int dndButtonState = 0;
int dndLastButtonState = HIGH;
int dndButtonStateCounter = 0;
void setup() {
pinMode(dndLedPin, OUTPUT);
pinMode(dndButtonPin, INPUT);
digitalWrite(dndButtonPin, HIGH); // Fix to stablize button state
Keyboard.begin();
}
// the loop function runs over and over again forever
void loop() {
// DND Button Loop
dndButtonState = digitalRead(dndButtonPin);
if (dndButtonState != dndLastButtonState) {
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 {
dndButtonStateCounter++;
if(dndButtonStateCounter == 10) {
if(dndButtonState == LOW) {
// Serial.println("START DND");
keyboardStartDND();
} else if (dndButtonState == HIGH) {
// Serial.println("END DND");
keyboardEndDND();
}
}
}
dndLastButtonState = dndButtonState;
delay(100);
}
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 keyboardStartDND() {
openTerminal();
Keyboard.println("dnd.sh on");
}
void keyboardEndDND() {
openTerminal();
Keyboard.println("dnd.sh off");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.