Created
March 20, 2018 04:10
-
-
Save heaversm/d0ed430fe6f126edd5b4bf7abd821913 to your computer and use it in GitHub Desktop.
Arduino sketch to handle input from a grove push button, light up an LED bar, and send messages via HID to Slack
This file contains hidden or 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
/* | |
Grove LED Bar | |
dec hex binary | |
0 = 0x0 = 0b000000000000000 = all LEDs off | |
5 = 0x05 = 0b000000000000101 = LEDs 1 and 3 on, all others off | |
341 = 0x155 = 0b000000101010101 = LEDs 1,3,5,7,9 on, 2,4,6,8,10 off | |
1023 = 0x3ff = 0b000001111111111 = all LEDs on | |
| | | |
10 1 | |
The bits >10 are ignored, shown here as x: 0bxxxxx0000000000 | |
*/ | |
#include <Grove_LED_Bar.h> | |
Grove_LED_Bar bar(A2, A3, 0); // Clock pin, Data pin, Orientation | |
const int buttonPin = A5; // The number of the pushbutton pin | |
// Variables will change: | |
int buttonPushCounter = 0; // Counter for the number of button presses | |
int buttonState = 0; // Current state of the button | |
int lastButtonState = 0; // Previous state of the button | |
unsigned long buttonDownTime; // How long since the button was first pressed | |
unsigned long buttonUpTime; // How long since the button was first pressed | |
#define debounce 20 // ms debounce period | |
#define holdTime 500 // ms hold period for press and hold | |
int ledBits = 0b000000000000000; // Keep track of the LEDs to light | |
void setup() { | |
BeanHid.enable(); | |
pinMode(buttonPin, INPUT); // Initialize the pushbutton pin as an input | |
bar.begin(); | |
bar.setBits(0); // turn off all LEDs | |
} | |
void loop() { | |
// Read the pushbutton input pin | |
buttonState = digitalRead(buttonPin); | |
// Compare the buttonState to its previous state | |
if (buttonState == HIGH && lastButtonState == LOW && (millis() - buttonUpTime) > long(debounce)) { | |
buttonPushCounter++; | |
buttonDownTime = millis(); | |
} | |
// Check for press and release | |
if(buttonState == LOW && lastButtonState == HIGH && (millis() - buttonDownTime) > long(debounce)) { | |
lightLedBar(buttonPushCounter); | |
buttonUpTime = millis(); | |
} | |
// Check for press and hold | |
if(buttonState == HIGH && (millis() - buttonDownTime) > long(holdTime)) { | |
sendMessage(buttonPushCounter); | |
buttonDownTime = millis(); | |
} | |
// Reset the counter if pressed over 10 times | |
if(buttonPushCounter > 10) { | |
buttonPushCounter = 0; | |
} | |
lastButtonState = buttonState; | |
} | |
//================================================= | |
// Cycle through LEDs on LED bar | |
//================================================= | |
void lightLedBar(int ledToLight) { | |
if(ledToLight == 0) { | |
bar.setBits(0); // Turn off all LEDs | |
ledBits = 0; | |
} | |
else { | |
ledBits = ledBits | ( 0b000010000000000 >> ledToLight); | |
bar.setBits(ledBits); | |
} | |
} | |
//================================================= | |
// Send HID message | |
//================================================= | |
void sendMessage(int messageNumber) { | |
if (messageNumber != 1){ //not sure why the count is off... | |
BeanHid.sendKeys(" "); | |
BeanHid.holdKey(KEY_LEFT_GUI); | |
BeanHid.sendKeys(" "); | |
BeanHid.releaseKey(KEY_LEFT_GUI); //bring up spotlight | |
BeanHid.sendKeys("slack-front"); //load the script which brings slack to the front window | |
BeanHid.sendKeys("\r\n"); | |
BeanHid.holdKey(KEY_LEFT_GUI); //option key will usually brint the cursor to the message input field - unless it's in another text field | |
BeanHid.releaseKey(KEY_LEFT_GUI); | |
} | |
delay(1000); | |
if(messageNumber == 1){ | |
ledBits = 0b000001000000000; | |
bar.setBits(ledBits); | |
} | |
if(messageNumber == 2) { | |
BeanHid.sendKeys("/dnd for 5 minutes"); | |
BeanHid.sendKeys("\r\n"); | |
} | |
else if(messageNumber == 3) { | |
BeanHid.sendKeys("/dnd for 10 minutes"); | |
BeanHid.sendKeys("\r\n"); | |
} | |
else if(messageNumber == 4) { | |
BeanHid.sendKeys("/dnd for 15 minutes"); | |
BeanHid.sendKeys("\r\n"); | |
} | |
else if(messageNumber == 5) { | |
BeanHid.sendKeys("/dnd for 30 minutes"); | |
BeanHid.sendKeys("\r\n"); | |
} | |
else if(messageNumber == 6) { | |
BeanHid.sendKeys("/dnd for 1 hour"); | |
BeanHid.sendKeys("\r\n"); | |
} | |
else if(messageNumber == 7) { | |
BeanHid.sendKeys("/dnd for 2 hours"); | |
BeanHid.sendKeys("\r\n"); | |
} | |
else if(messageNumber == 8) { | |
BeanHid.sendKeys("/dnd until 17:00"); | |
BeanHid.sendKeys("\r\n"); | |
} | |
else if(messageNumber == 9) { | |
BeanHid.sendKeys("/dnd until tonight"); | |
BeanHid.sendKeys("\r\n"); | |
} | |
else if(messageNumber == 10) { | |
BeanHid.sendKeys("/dnd until tomorrow morning"); | |
BeanHid.sendKeys("\r\n"); | |
} | |
else if(messageNumber == 0) { | |
BeanHid.sendKeys("/dnd "); //turn dnd off if it is on | |
BeanHid.sendKeys("\r\n"); | |
} | |
if (messageNumber == 0){ | |
resetBar(); | |
} else if (messageNumber == 1) { | |
return; | |
} else { | |
setBarFull(); | |
} | |
} | |
void setBarFull(){ | |
delay(1000); | |
buttonPushCounter = 10; | |
ledBits = 0b000001111111111; | |
bar.setBits(ledBits); | |
} | |
void resetBar(){ | |
delay(1000); | |
buttonPushCounter = 0; | |
ledBits = 0b000000000000000; | |
bar.setBits(ledBits); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment