. . . in essence, I need for my Leonardo to take the direction: "if (and only if) Event A AND Event B occurs, then Event C will occur": I have given a Keystroke combination to a Rotary Encoder (Enter+Up/Down), but I'd like for it to occur only if and after a button (button1) has been pressed. Button 1 is, itself, another Keystroke (the Pause Key).
I read various threads last night, and learned about &&, and tried it out extensively but didn't achieve my goal.
Here is the entire sketch (it derives from TTapa's Control-Surface):
#include <RotaryEncoder.h>
#include <Keyboard.h>
// Rotary Encoder
#define RE_A 7 // "CLK"
#define RE_B 6 // "DT"
#define RE_SW 2 // "SW"
RotaryEncoder encoder(RE_A, RE_B, RotaryEncoder::LatchMode::TWO03);
int last_sw;
int sw_cnt;
// Button Stuff
// constants won't change. They're used here to set pin numbers:
const int button1Pin = 9; // the number of the pushbutton pin
const int button2Pin = 14; // the number of the pusbutton pin
const int button3Pin = 15; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int button1State = 0; // variable for reading the pushbutton status
int button2State = 0; // variable for reading the pushbutton status
int button3State = 0; // variable for reading the pushbutton status
void setup() {
// Initialise the encoder switch
pinMode(RE_SW, INPUT_PULLUP);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pins as an input:
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
}
int re_pos = 0;
void loop() {
// Read the rotary encoder
encoder.tick();
int new_pos = encoder.getPosition();
if (new_pos != re_pos) {
int re_dir = (int)encoder.getDirection();
if (re_dir < 0) {
Keyboard.press(KEY_RIGHT_CTRL);
Keyboard.press(KEY_UP_ARROW);
Keyboard.release(KEY_RIGHT_CTRL);
Keyboard.release(KEY_UP_ARROW);
}
else if (re_dir > 0) {
Keyboard.press(KEY_RIGHT_CTRL);
Keyboard.press(KEY_DOWN_ARROW);
Keyboard.release(KEY_RIGHT_CTRL);
Keyboard.release(KEY_DOWN_ARROW);
}
else {
// if re_dir == 0; do nothing
}
re_pos = new_pos;
}
// read the state of the pushbutton value:
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
button3State = digitalRead(button3Pin);
// check if the pushbutton 1 is pressed. If it is, the buttonState is LOW:
if (button1State ==LOW) {
// initiates the pressing of the Pause Key:
Keyboard.press(KEY_PAUSE);
Keyboard.release(KEY_PAUSE);
}
else {
// turn LED off:
digitalWrite(ledPin, HIGH);
// check if the pushbutton 2 is pressed. If it is, the buttonState is LOW:
if (button2State ==LOW) {
// initiates the pressing of the Pause Key:
Keyboard.press(KEY_RETURN);
Keyboard.press(KEY_KP_1);
Keyboard.release(KEY_RETURN);
Keyboard.release(KEY_KP_1);
}
else {
// turn LED off:
digitalWrite(ledPin, HIGH);
// check if the pushbutton 3 is pressed. If it is, the buttonState is LOW:
if (button3State ==LOW) {
// initiates the pressing of the Pause Key:
Keyboard.press(KEY_RETURN);
Keyboard.press(KEY_KP_1);
Keyboard.release(KEY_RETURN);
Keyboard.release(KEY_KP_1);
}
// Check the encoder switch with some software debouncing
int re_sw = digitalRead(RE_SW);
if (re_sw == HIGH) {
// No switch pressed yet
sw_cnt = 0;
last_sw = HIGH;
} else {
sw_cnt++;
}
if ((last_sw==HIGH) && (sw_cnt >= 1000)) {
// Switch is triggered!
sw_cnt = 0;
last_sw = LOW;
Keyboard.press(KEY_RETURN);
Keyboard.release(KEY_RETURN);
}
}
}
}
. . . and here are the things I've tried:
int new_pos = encoder.getPosition();
if (new_pos != re_pos) {
int re_dir = (int)encoder.getDirection ();
if (re_dir < 0 && (button1State ==LOW)) {
Keyboard.press(KEY_RIGHT_CTRL);
Keyboard.press(KEY_UP_ARROW);
Keyboard.release(KEY_RIGHT_CTRL);
Keyboard.release(KEY_UP_ARROW);
}
else if (re_dir > 0 && (button1State ==LOW)) {
Keyboard.press(KEY_RIGHT_CTRL);
Keyboard.press(KEY_DOWN_ARROW);
Keyboard.release(KEY_RIGHT_CTRL);
Keyboard.release(KEY_DOWN_ARROW);
}
else {
// if re_dir == 0; do nothing
}
re_pos = new_pos;
}
Nope-didn't work.
Tried this:
int new_pos = encoder.getPosition();
if (new_pos != re_pos && (button1State ==LOW)) {
int re_dir = (int)encoder.getDirection ();
if (re_dir < 0 ) {
Keyboard.press(KEY_RIGHT_CTRL);
Keyboard.press(KEY_UP_ARROW);
Keyboard.release(KEY_RIGHT_CTRL);
Keyboard.release(KEY_UP_ARROW);
}
else if (re_dir > 0) {
Keyboard.press(KEY_RIGHT_CTRL);
Keyboard.press(KEY_DOWN_ARROW);
Keyboard.release(KEY_RIGHT_CTRL);
Keyboard.release(KEY_DOWN_ARROW);
}
else {
// if re_dir == 0; do nothing
}
re_pos = new_pos;
}
. . . still a no.
And I tried a whole bunch of other permutations, involving actually entering the Keystrokes that make up the macro button1State (Keyboard.press(KEY_PAUSE) and Keyboard.release(KEY_PAUSE)) as additional actions preceeding the whole KEY_RIGHT_CTRL, KEY_UP_ARROW, etc. in both the re_dir < 0 and re_dir > 0 sections. Still nothing.
Can someone out there help me figure out how to achieve this. I sense that it has to be possible and I'm just not yet knowledgeable enough to figure it out on my own.