Skip to content

Instantly share code, notes, and snippets.

@andymasteroffish
Created March 18, 2021 16:53
Show Gist options
  • Save andymasteroffish/71aabc1d9b9a95b63062a2e0a36c221e to your computer and use it in GitHub Desktop.
Save andymasteroffish/71aabc1d9b9a95b63062a2e0a36c221e to your computer and use it in GitHub Desktop.
#include <Keyboard.h>
//Code by Mark Kleback for IMS Jam Jam Revolution
//http://www.kleebtronics.com/
//button pins
int button1 = 9;
int button2 = 10;
//storing the button state to detect changes
int prevButton1State;
int prevButton2State;
int debounce = debounce;
//encoder stuff
int val;
int encoderPinA = 6;
int encoderPinB = 12;
int encoderPos = 0;
int encoderPinALast = LOW;
// put your setup code here, to run once:
void setup() {
Serial.begin(9600);
Keyboard.begin();
//set input pins
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
//starting values
prevButton1State = HIGH;
prevButton2State = HIGH;
}
// put your main code here, to run repeatedly:
void loop() {
//get the current value of the buttons
int button1state = digitalRead(button1);
int button2state = digitalRead(button2);
//button 1 = x ----------------------
if(button1state == LOW && prevButton1State == HIGH){
Keyboard.press('x');
prevButton1State = LOW;
delay(debounce);
//Keyboard.release('x'); //line removed to allow the button to be held down
}
if (button1state == HIGH && prevButton1State == LOW){
Keyboard.release('x');
prevButton1State = button1state;
}
//button 2 = z ----------------------
if(button2state == LOW && prevButton2State == HIGH){
Keyboard.press('z');
prevButton2State = LOW;
delay(debounce);
//Keyboard.release('z'); //line removed to allow the button to be held down
}
if (button2state == HIGH && prevButton2State == LOW){
Keyboard.release('z');
prevButton2State = button2state;
}
//check Encoder ----------
int n = digitalRead(encoderPinA);
if (encoderPinALast == LOW && n == HIGH){
if (digitalRead(encoderPinB) == LOW){
//moving clockwise
encoderPos++;
Keyboard.press(KEY_RIGHT_ARROW);
delay(10);
Keyboard.release(KEY_RIGHT_ARROW);
}
else{
//moving counter clockwise
encoderPos--;
Keyboard.press(KEY_LEFT_ARROW);
delay(10);
Keyboard.release(KEY_LEFT_ARROW);
}
}
encoderPinALast = n;
//uncomment this to check the encoder value in the Serial Monitor
//Serial.println(encoderPos);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment