Code:
#include <Bounce.h>
#include <Encoder.h>
int rot_led = 9;
int rot_btn = 14;
int top_left_btn = 17;
int top_right_btn = 16;
int bot_btn = 15;
int enc_a = 20;
int enc_b = 19;
int enc_val = 0;
int enc_divider = 7;
int ms_delay = 1;
int debounce = 10;
bool led_on = false;
bool last = false;
Bounce btn1 = Bounce(rot_btn, debounce);
Bounce btn2 = Bounce(top_left_btn, debounce);
Bounce btn3 = Bounce(top_right_btn, debounce);
Bounce btn4 = Bounce(bot_btn, debounce);
Encoder enc = Encoder(enc_a, enc_b);
void setup() {
pinMode(rot_led, OUTPUT);
pinMode(rot_btn, INPUT_PULLUP);
pinMode(top_left_btn, INPUT_PULLUP);
pinMode(top_right_btn, INPUT_PULLUP);
pinMode(bot_btn, INPUT_PULLUP);
}
void send_key(uint8_t key, uint8_t modifier) {
Serial.print(key);
Serial.print(" ");
Serial.println(modifier);
Keyboard.set_key1(key);
Keyboard.set_modifier(modifier);
Keyboard.send_now();
Keyboard.set_key1(0);
Keyboard.set_modifier(0);
Keyboard.send_now();
}
void send_media(uint8_t key) {
Keyboard.set_media(key);
Keyboard.send_now();
Keyboard.set_media(0);
Keyboard.send_now();
}
// the loop routine runs over and over again forever:
void loop() {
btn1.update();
btn2.update();
btn3.update();
btn4.update();
enc_val = enc.read();
if (enc_val/enc_divider > 0) {
Serial.println("volume up");
send_media(KEY_MEDIA_VOLUME_INC);
enc.write(enc_val-enc_divider);
} else if (enc_val/enc_divider < 0) {
Serial.println("volume down");
send_media(KEY_MEDIA_VOLUME_DEC);
enc.write(enc_val+enc_divider);
}
if (led_on && led_on != last) {
last = led_on;
digitalWrite(rot_led, HIGH);
Serial.println("on");
} else if (led_on != last) {
last = led_on;
digitalWrite(rot_led, LOW);
Serial.println("off");
}
if (btn1.fallingEdge()) {
led_on = !led_on;
send_media(KEY_MEDIA_MUTE);
}
if (btn2.fallingEdge()) {
send_media(KEY_MEDIA_PREV_TRACK);
}
if (btn3.fallingEdge()) {
send_media(KEY_MEDIA_PLAY_PAUSE);
}
if (btn4.fallingEdge()) {
send_media(KEY_MEDIA_NEXT_TRACK);
}
delay(ms_delay);
}
I am using a Teensy 3.0, Teensyduino 1.15, OSX 10.7. I can get other keycodes to send, but I can't seem to get the media ones to fire.
Any help with this would be welcomed.
Thanks,
Wraithan
Last edited: