Skip to content

Instantly share code, notes, and snippets.

@mikeflynn

mikeflynn/dnd_button-itsybitsy Secret

Last active Jun 17, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.