I'm building a Teensy based jog controller for my CNC. It will take the place of the arrow keys on the regular USB keyboard. Pressing the button once jogs the CNC one step, but holding the button down needs to continually move the CNC.
I have a prototype working kinda. It does what I want when a button is pressed, but I need for it to continue sending the button press when I hold the button down. Right now it's a one-shot deal. Holding down the button sends one command and one only.
How do I get the button press to send one command when I press and release, but to send a string of commands when I press and hold the button?
The code I'm using is this:
I have a prototype working kinda. It does what I want when a button is pressed, but I need for it to continue sending the button press when I hold the button down. Right now it's a one-shot deal. Holding down the button sends one command and one only.
How do I get the button press to send one command when I press and release, but to send a string of commands when I press and hold the button?
The code I'm using is this:
Code:
#include <Bounce.h>
/* Create Bounce objects for each button. The Bounce object automatically deals with contact
chatter or "bounce", and it makes detecting changes very simple.*/
Bounce button0 = Bounce(0, 10); // 10 = 10 ms debounce time which is appropriate for most
Bounce button1 = Bounce(1, 10); // mechanical pushbuttons. If a button is too sensitive to rapid touch
Bounce button2 = Bounce(2, 10); // you can increase this time.
Bounce button3 = Bounce(3, 10);
Bounce button4 = Bounce(4, 10);
Bounce button5 = Bounce(5, 10);
/*
Bounce button6 = Bounce(6, 10);
Bounce button7 = Bounce(7, 10);
Bounce button8 = Bounce(8, 10);
Bounce button9 = Bounce(9, 10);
*/
void setup() {
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
/*
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
*/
Keyboard.begin();
}
void loop() {
/* Update all the buttons. There should not be any long delays in loop(), so this runs
repetitively at a rate faster than the buttons could be pressed and released.*/
button0.update();
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
/*
button6.update();
button7.update();
button8.update();
button9.update();
*/
// Check each button for "falling" edge.
if (button0.fallingEdge()) {
Keyboard.set_modifier(MODIFIERKEY_GUI); // Mac "Command" key press and hold
Keyboard.send_now();
Keyboard.set_key1(KEY_LEFT); // Press "Left Arrow" key Moves X- (LEFT)
Keyboard.send_now();
// release all the keys at the same instant
Keyboard.set_modifier(0);
Keyboard.set_key1(0);
Keyboard.send_now();
}
if (button1.fallingEdge()) {
Keyboard.set_modifier(MODIFIERKEY_GUI); // Mac "Command" key press and hold
Keyboard.send_now();
Keyboard.set_key2(KEY_RIGHT); // Press "Right Arrow" key Moves X+ (RIGHT)
Keyboard.send_now();
// release all the keys at the same instant
Keyboard.set_modifier(0);
Keyboard.set_key2(0);
Keyboard.send_now();
}
if (button2.fallingEdge()) {
Keyboard.set_modifier(MODIFIERKEY_GUI); // Mac "Command" key press and hold
Keyboard.send_now();
Keyboard.set_key3(KEY_DOWN); // Press "Down Arrow" key Moves Y- (DOWN)
Keyboard.send_now();
// release all the keys at the same instant
Keyboard.set_modifier(0);
Keyboard.set_key3(0);
Keyboard.send_now();
}
if (button3.fallingEdge()) {
Keyboard.set_modifier(MODIFIERKEY_GUI); // Mac "Command" key press and hold
Keyboard.send_now();
Keyboard.set_key4(KEY_UP); // Press "Up Arrow" key Moves Y+ (UP)
Keyboard.send_now();
// release all the keys at the same instant
Keyboard.set_modifier(0);
Keyboard.set_key4(0);
Keyboard.send_now();
}
if (button4.fallingEdge()) {
Keyboard.set_modifier(MODIFIERKEY_SHIFT); // Press and hold "Shift"
Keyboard.send_now();
Keyboard.set_modifier(MODIFIERKEY_SHIFT | MODIFIERKEY_GUI); // Press Mac "Command" while still holding "Shift"
Keyboard.send_now();
Keyboard.set_key5(KEY_DOWN); // Press "Down Arrow" key Moves Z- (DOWN)
Keyboard.send_now();
// release all the keys at the same instant
Keyboard.set_modifier(0);
Keyboard.set_key5(0);
Keyboard.send_now();
}
if (button5.fallingEdge()) {
Keyboard.set_modifier(MODIFIERKEY_SHIFT); // Press and hold "Shift"
Keyboard.send_now();
Keyboard.set_modifier(MODIFIERKEY_SHIFT | MODIFIERKEY_GUI); // Press Mac "Command" while still holding "Shift"
Keyboard.send_now();
Keyboard.set_key6(KEY_UP); // Press "Up Arrow" key Moves Z+ (UP)
Keyboard.send_now();
// release all the keys at the same instant
Keyboard.set_modifier(0);
Keyboard.set_key6(0);
Keyboard.send_now();
}
}