looking for code to use a momentary switch to keyboard input the power for the switch is hooked to Ground on the board and left is 14 and right is 18 Need it to when i hold left keyboard left is held when neutral nothing happens and when right is held keyboard right is held I have no idea what I'm doing any help?
Hello goodracing112
Welcome to the world's best Arduino forum ever.
Post your sketch, well formated, with well-tempered comments in so called code tags and schematic to see how we can help.
Have a nice day and enjoy coding in C++.
// This code is based on the Arduino Keyboard library
#include <Keyboard.h>
// Define the pins for the left and right keyboard inputs
#define LEFT_INPUT 14
#define RIGHT_INPUT 18
// Set the initial state of the keyboard inputs to inactive
bool leftInputActive = false;
bool rightInputActive = false;
void setup() {
// Initialize the keyboard library
Keyboard.begin();
// Set the input pins as inputs
pinMode(LEFT_INPUT, INPUT);
pinMode(RIGHT_INPUT, INPUT);
}
void loop() {
// Check if the left input is active
if (digitalRead(LEFT_INPUT) == HIGH) {
// If it is not already active, send the left arrow key press
if (!leftInputActive) {
Keyboard.press(KEY_LEFT_ARROW);
leftInputActive = true;
}
} else {
// If it is active, release the left arrow key
if (leftInputActive) {
Keyboard.release(KEY_LEFT_ARROW);
leftInputActive = false;
}
}
// Check if the right input is active
if (digitalRead(RIGHT_INPUT) == HIGH) {
// If it is not already active, send the right arrow key press
if (!rightInputActive) {
Keyboard.press(KEY_RIGHT_ARROW);
rightInputActive = true;
}
} else {
// If it is active, release the right arrow key
if (rightInputActive) {
Keyboard.release(KEY_RIGHT_ARROW);
rightInputActive = false;
}
}
}
looking for code to use a momentary switch to keyboard input the power for the switch is hooked to Ground on the board and left is 14 and right is 18 Need it to when i hold left keyboard left is held when neutral nothing happens and when right is held keyboard right is held I have no idea what I'm doing any help?
// This code is based on the Arduino Keyboard library
#include <Keyboard.h>
// Define the pins for the left and right keyboard inputs
#define LEFT_INPUT 14
#define RIGHT_INPUT 18
// Set the initial state of the keyboard inputs to inactive
bool leftInputActive = false;
bool rightInputActive = false;
void setup() {
// Initialize the keyboard library
Keyboard.begin();
// Set the input pins as inputs
pinMode(LEFT_INPUT, INPUT);
pinMode(RIGHT_INPUT, INPUT);
}
void loop() {
// Check if the left input is active
if (digitalRead(LEFT_INPUT) == HIGH) {
// If it is not already active, send the left arrow key press
if (!leftInputActive) {
Keyboard.press(KEY_LEFT_ARROW);
leftInputActive = true;
}
} else {
// If it is active, release the left arrow key
if (leftInputActive) {
Keyboard.release(KEY_LEFT_ARROW);
leftInputActive = false;
}
}
// Check if the right input is active
if (digitalRead(RIGHT_INPUT) == HIGH) {
// If it is not already active, send the right arrow key press
if (!rightInputActive) {
Keyboard.press(KEY_RIGHT_ARROW);
rightInputActive = true;
}
} else {
// If it is active, release the right arrow key
if (rightInputActive) {
Keyboard.release(KEY_RIGHT_ARROW);
rightInputActive = false;
}
}
}
Hello goodracing112
I have made a small code check.
Result:
The program is missing the debouncing of the momentary switch and a state change dedection to send the corresponding keycode.
yeah i have no idea what any of that is sorry
// Include the Keyboard library
#include <Keyboard.h>
// Define the pins for the momentary switches
#define RIGHT_PIN 18
#define LEFT_PIN 14
// Define the debounce delay in milliseconds
#define DEBOUNCE_DELAY 50
// Define the initial state of the switches
int rightState = LOW;
int leftState = LOW;
// Define the previous state of the switches
int prevRightState = LOW;
int prevLeftState = LOW;
// Define the current state of the switches
int currentRightState = LOW;
int currentLeftState = LOW;
// Define the keycode for the right and left keys
int rightKeycode = KEY_RIGHT;
int leftKeycode = KEY_LEFT;
// Setup function
void setup() {
// Initialize the keyboard
Keyboard.begin();
// Set the pins for the momentary switches as input
pinMode(RIGHT_PIN, INPUT);
pinMode(LEFT_PIN, INPUT);
}
// Loop function
void loop() {
// Read the current state of the switches
currentRightState = digitalRead(RIGHT_PIN);
currentLeftState = digitalRead(LEFT_PIN);
// Check for state change in the right switch
if (currentRightState != prevRightState) {
// Debounce the switch
delay(DEBOUNCE_DELAY);
// Update the previous state
prevRightState = currentRightState;
// Check if the switch is active
if (currentRightState == HIGH) {
// Send the right keycode
Keyboard.write(rightKeycode);
}
}
// Check for state change in the left switch
if (currentLeftState != prevLeftState) {
// Debounce the switch
delay(DEBOUNCE_DELAY);
// Update the previous state
prevLeftState = currentLeftState;
// Check if the switch is active
if (currentLeftState == HIGH) {
// Send the left keycode
Keyboard.write(leftKeycode);
}
}
}
Use code tags to post the sketch.
For posting code in code-tags take 60 seconds time to read this tutorial
then take 3 minutes time to re-edit your postings
so I have a three way momentary switch hooked up to pin 14 for left pin 18 for right and then center is on the ground trying to get it to control left and right keyboard input or any input really with this code i get just pretty uncontrolled input left and right ill press right and it may work it may not or it may just go left for whatever time it feels like could it be an issue with the switch or connections or just bad code?
#include <Keyboard.h>
// Define the input pins
const int inputLeft = 14; // Input for left key
const int inputRight = 18; // Input for right key
// Variables for debouncing and state change detection
bool lastStateLeft = LOW;
bool currentStateLeft = LOW;
bool lastStateRight = LOW;
bool currentStateRight = LOW;
unsigned long lastDebounceTimeLeft = 0;
unsigned long lastDebounceTimeRight = 0;
const unsigned long debounceDelay = 50; // Debounce time in milliseconds
void setup() {
// Initialize the Keyboard library
Keyboard.begin();
// Set the input pins as inputs
pinMode(inputLeft, INPUT);
pinMode(inputRight, INPUT);
}
void loop() {
// Read the current state of the inputs
int readingLeft = digitalRead(inputLeft);
int readingRight = digitalRead(inputRight);
// Debounce the left input
if (readingLeft != lastStateLeft) {
lastDebounceTimeLeft = millis();
}
if ((millis() - lastDebounceTimeLeft) > debounceDelay) {
if (readingLeft != currentStateLeft) {
currentStateLeft = readingLeft;
if (currentStateLeft == HIGH) {
Keyboard.press(KEY_LEFT_ARROW);
} else {
Keyboard.release(KEY_LEFT_ARROW);
}
}
}
lastStateLeft = readingLeft;
// Debounce the right input
if (readingRight != lastStateRight) {
lastDebounceTimeRight = millis();
}
if ((millis() - lastDebounceTimeRight) > debounceDelay) {
if (readingRight != currentStateRight) {
currentStateRight = readingRight;
if (currentStateRight == HIGH) {
Keyboard.press(KEY_RIGHT_ARROW);
} else {
Keyboard.release(KEY_RIGHT_ARROW);
}
}
}
lastStateRight = readingRight;
}
Have you tried using
pinMode(…,INPUT_PULLUP);
that was it thank you!
[sterretje edit]
I suspect my merging of topics did not go as expected; this is a duplicate of post #12.
[sterretje edit]
so I have a three way momentary switch hooked up to pin 14 for left pin 18 for right and then center is on the ground trying to get it to control left and right keyboard input or any input really with this code i get just pretty uncontrolled input left and right ill press right and it may work it may not or it may just go left for whatever time it feels like could it be an issue with the switch or connections or just bad code?
#include <Keyboard.h>
// Define the input pins
const int inputLeft = 14; // Input for left key
const int inputRight = 18; // Input for right key
// Variables for debouncing and state change detection
bool lastStateLeft = LOW;
bool currentStateLeft = LOW;
bool lastStateRight = LOW;
bool currentStateRight = LOW;
unsigned long lastDebounceTimeLeft = 0;
unsigned long lastDebounceTimeRight = 0;
const unsigned long debounceDelay = 50; // Debounce time in milliseconds
void setup() {
// Initialize the Keyboard library
Keyboard.begin();
// Set the input pins as inputs
pinMode(inputLeft, INPUT);
pinMode(inputRight, INPUT);
}
void loop() {
// Read the current state of the inputs
int readingLeft = digitalRead(inputLeft);
int readingRight = digitalRead(inputRight);
// Debounce the left input
if (readingLeft != lastStateLeft) {
lastDebounceTimeLeft = millis();
}
if ((millis() - lastDebounceTimeLeft) > debounceDelay) {
if (readingLeft != currentStateLeft) {
currentStateLeft = readingLeft;
if (currentStateLeft == HIGH) {
Keyboard.press(KEY_LEFT_ARROW);
} else {
Keyboard.release(KEY_LEFT_ARROW);
}
}
}
lastStateLeft = readingLeft;
// Debounce the right input
if (readingRight != lastStateRight) {
lastDebounceTimeRight = millis();
}
if ((millis() - lastDebounceTimeRight) > debounceDelay) {
if (readingRight != currentStateRight) {
currentStateRight = readingRight;
if (currentStateRight == HIGH) {
Keyboard.press(KEY_RIGHT_ARROW);
} else {
Keyboard.release(KEY_RIGHT_ARROW);
}
}
}
lastStateRight = readingRight;
}