Rotary Jog Keyboard

Pictures, plans, gcode, projects, tutorials, ...
All stuff that you do with your machine.

Rotary Jog Keyboard

Postby GTOhlms » 18 Nov 2016, 16:06

Here is a rotary jogging keyboard that I made using an Adafruit Trinket, a rotary encoder, a couple push buttons, and a selector switch. Below is the code I used. I did not develop all of this code myself, some it was found on the internet and modified for my needs.

I do not have schematics for the wiring, but it really isn't that complicated.

Here is a video of it working as well:
https://www.youtube.com/watch?v=vhojpB1eLmA

/*Reads rotary encoder and moves machine one step at
a time. Also has jog buttons for full speed jog mode.
Uses axis selector knob to choose axis.
Also has jog speed knob as well
*/

#include <ProTrinketKeyboard.h>

#define PIN_ENCODER_A 3
#define PIN_ENCODER_B 4
#define TRINKET_PINx PIND
#define xaxisswitch 5
#define yaxisswitch 6
#define jogneg_button 9
#define jogpos_button 10

static uint8_t enc_prev_pos = 0;
static uint8_t enc_flags = 0;
static char sw_was_pressed = 0;
volatile int axistype;

void setup()
{
// set pins as input with internal pull-up resistors enabled
pinMode(PIN_ENCODER_A, INPUT_PULLUP);
pinMode(PIN_ENCODER_B, INPUT_PULLUP);
pinMode(xaxisswitch, INPUT);
pinMode(yaxisswitch, INPUT);
pinMode(jogpos_button, INPUT);
pinMode(jogneg_button, INPUT);

digitalWrite(xaxisswitch, HIGH);
digitalWrite(yaxisswitch, HIGH);
digitalWrite(jogpos_button, HIGH);
digitalWrite(jogneg_button, HIGH);

TrinketKeyboard.begin();

// get an initial reading on the encoder pins
if (digitalRead(PIN_ENCODER_A) == LOW) {
enc_prev_pos |= (1 << 0);
}
if (digitalRead(PIN_ENCODER_B) == LOW) {
enc_prev_pos |= (1 << 1);
}
}

void loop(){
if(digitalRead(xaxisswitch) == LOW) {
axistype = 1;}
else if (digitalRead(yaxisswitch) == LOW) {
axistype = 2;}
else if (digitalRead(xaxisswitch) == HIGH && digitalRead(yaxisswitch) == HIGH){
axistype = 3;}

if (digitalRead(jogpos_button) == LOW){
if(axistype == 1) {
TrinketKeyboard.pressKey(0, KEYCODE_ARROW_RIGHT);} // X Positive
else if (axistype == 2) {
TrinketKeyboard.pressKey(0, KEYCODE_ARROW_UP);} //Y Positive
else if (axistype == 3) {
TrinketKeyboard.pressKey(0, KEYCODE_PAGE_UP);} //Z Positive
}
if (digitalRead(jogneg_button) == LOW){
if(axistype == 1) {
TrinketKeyboard.pressKey(0, KEYCODE_ARROW_LEFT);} // X Negative
else if (axistype == 2) {
TrinketKeyboard.pressKey(0, KEYCODE_ARROW_DOWN);} //Y Negative
else if (axistype == 3) {
TrinketKeyboard.pressKey(0, KEYCODE_PAGE_DOWN);} //Z Negative
}
if (digitalRead(jogpos_button) == HIGH && digitalRead(jogneg_button) == HIGH){
TrinketKeyboard.pressKey(0, 0);}

int8_t enc_action = 0; // 1 or -1 if moved, sign is direction
uint8_t enc_cur_pos = 0;
// read in the encoder state first
if (bit_is_clear(TRINKET_PINx, PIN_ENCODER_A)) {
enc_cur_pos |= (1 << 0);
}
if (bit_is_clear(TRINKET_PINx, PIN_ENCODER_B)) {
enc_cur_pos |= (1 << 1);
}

// if any rotation at all
if (enc_cur_pos != enc_prev_pos){
if (enc_prev_pos == 0x00)
{
// this is the first edge
if (enc_cur_pos == 0x01) {
enc_flags |= (1 << 0);
}
else if (enc_cur_pos == 0x02) {
enc_flags |= (1 << 1);
}
}

if (enc_cur_pos == 0x03)
{
// this is when the encoder is in the middle of a "step"
enc_flags |= (1 << 4);
}
else if (enc_cur_pos == 0x00)
{
// this is the final edge
if (enc_prev_pos == 0x02) {
enc_flags |= (1 << 2);
}
else if (enc_prev_pos == 0x01) {
enc_flags |= (1 << 3);
}

// check the first and last edge
// or maybe one edge is missing, if missing then require the middle state
// this will reject bounces and false movements
if (bit_is_set(enc_flags, 0) && (bit_is_set(enc_flags, 2) || bit_is_set(enc_flags, 4))) {
enc_action = 1;
}
else if (bit_is_set(enc_flags, 2) && (bit_is_set(enc_flags, 0) || bit_is_set(enc_flags, 4))) {
enc_action = 1;
}
else if (bit_is_set(enc_flags, 1) && (bit_is_set(enc_flags, 3) || bit_is_set(enc_flags, 4))) {
enc_action = -1;
}
else if (bit_is_set(enc_flags, 3) && (bit_is_set(enc_flags, 1) || bit_is_set(enc_flags, 4))) {
enc_action = -1;
}
enc_flags = 0; // reset for next time
}
}

enc_prev_pos = enc_cur_pos;

if (enc_action > 0) { //Clockwise
if(axistype==1) {
TrinketKeyboard.pressKey(KEYCODE_MOD_LEFT_SHIFT, KEYCODE_ARROW_RIGHT);
delay(50);} // X Positive
else if (axistype ==2) {
TrinketKeyboard.pressKey(KEYCODE_MOD_LEFT_SHIFT, KEYCODE_ARROW_UP);
delay(50);} //Y Positive
else if (axistype ==3) {
TrinketKeyboard.pressKey(KEYCODE_MOD_LEFT_SHIFT, KEYCODE_PAGE_UP);
delay(50);} //Z Positive
TrinketKeyboard.pressKey(0, 0);
}
else if (enc_action < 0) { //CounterClockwise
if(axistype==1) {
TrinketKeyboard.pressKey(KEYCODE_MOD_LEFT_SHIFT, KEYCODE_ARROW_LEFT);
delay(50);} // X Negative
else if (axistype ==2) {
TrinketKeyboard.pressKey(KEYCODE_MOD_LEFT_SHIFT, KEYCODE_ARROW_DOWN);
delay(50);} //Y Negative
else if (axistype ==3) {
TrinketKeyboard.pressKey(KEYCODE_MOD_LEFT_SHIFT, KEYCODE_PAGE_DOWN);
delay(50);} //Z Negative
TrinketKeyboard.pressKey(0, 0);}

}
GTOhlms
 
Posts: 6
Joined: 21 Nov 2014, 19:01
Location: St. Charles MO
Controller: Mk2

Return to Various projects



Who is online

Users browsing this forum: No registered users and 1 guest