Button presses made reletive to joystick position

This commit is contained in:
Michel committed 2023-10-25 00:20:34 +02:00
1 parent b4c95af1ee
commit d8dbfd78d3
1 file changed
+182 -38
+182 -38
View File
@@ -1,57 +1,201 @@
/*************************************
* arduino-joystick
* Author: Michel Bats
* Website: www.batssoft.nl
*************************************/
// Libraries
#include <Arduino.h>
#include <Keyboard.h>
// Defines
#define MAX_WAIT_CYCLES 100 // Maximum loop cycles for nex key press (time in ms = MAX_WAIT_CYCLES * (DELAY_BETWEEN_KEYS *2))
#define DELAY_BETWEEN_KEYS 5 // Milliseconds between key press/release
#define X_AXIS_PIN A0 // PIN with the X axis of the joystick
#define Y_AXIS_PIN A1 // PIN with the Y axis of the joystick
// Initialize variables
// Offset values to save the actual 0,0 position of the joystick
int sensorOffsetX;
int sensorOffsetY;
// Position of the joystick when in use
int sensorValueX;
int sensorValueY;
// Variables to set when one of the keys is currently pressed to later on release
bool pressedKeyLeftArrow = false;
bool pressedKeyRightArrow = false;
bool pressedKeyUpArrow = false;
bool pressedKeyDownArrow = false;
// Cycle count to wait before next key press
int waitValueX = 0;
int waitValueY = 0;
// Variable to save the new cycle count value to evaluate against the current one
int waitValueXNew = 0;
int waitValueYNew = 0;
long sensorValueX;
long sensorValueY;
int pressedKeyLeftArrow = 0;
int pressedKeyRightArrow = 0;
int pressedKeyUpArrow = 0;
int pressedKeyDownArrow = 0;
void setup() {
sensorOffsetX = analogRead(A0);
sensorOffsetY = analogRead(A1);
// the setup function runs once when you press reset or power the board
void setup()
{
// Read the current position of the joystick to use this as position 0,0
sensorOffsetX = analogRead(X_AXIS_PIN);
sensorOffsetY = analogRead(Y_AXIS_PIN);
// Start the keyboard library
Keyboard.begin();
}
void loop() {
sensorValueX = analogRead(A0) - sensorOffsetX;
sensorValueY = analogRead(A1) - sensorOffsetY;
// the loop function runs over and over again forever
void loop()
{
// Read the current position of the joystick
sensorValueX = analogRead(X_AXIS_PIN) - sensorOffsetX;
sensorValueY = analogRead(Y_AXIS_PIN) - sensorOffsetY;
if (sensorValueX < -100 && pressedKeyLeftArrow == 0) {
Keyboard.press(0xD8); //KEY_LEFT_ARROW
pressedKeyLeftArrow = 1;
} else if (sensorValueX > 100 && pressedKeyRightArrow == 0) {
Keyboard.press(0xD7); //KEY_RIGHT_ARROW
pressedKeyRightArrow = 1;
// Check for movement on the X axis
// Check if the joystick is moved to the left
if (sensorValueX < -10)
{
// Create a value for the number of cycles to wait before next key press
waitValueXNew = map(sensorValueX, 0, -sensorOffsetX, MAX_WAIT_CYCLES, 0);
// If the current cycle wait value is equal to 0 run this if.
// The contents of this if will immediately run when the joystick is comming from the 0 position on the X axis.
if (waitValueX == 0)
{
// Press the left arrow key
Keyboard.press(0xD8);
// Set a variable that the left arrow key is currently pressed
pressedKeyLeftArrow = true;
// Update the cycls wait count with the new value
waitValueX = waitValueXNew;
}
// If the current cycle wait count is larger than the new one (happens when the joystick is pushed further outside) run this if
else if (waitValueX > waitValueXNew)
{
// Lower the value to the new value
waitValueX = waitValueXNew;
}
}
// Check if the joystick is moved to the right
else if (sensorValueX > 10)
{
// Create a value for the number of cycles to wait before next key press
waitValueXNew = map(sensorValueX, 0, 1023 - sensorOffsetX, MAX_WAIT_CYCLES, 0);
// If the current cycle wait value is equal to 0 run this if.
// The contents of this if will immediately run when the joystick is comming from the 0 position on the X axis.
if (waitValueX == 0)
{
// Press the right arrow key
Keyboard.press(0xD7);
// Set a variable that the right arrow key is currently pressed
pressedKeyRightArrow = true;
// Update the cycls wait count with the new value
waitValueX = waitValueXNew;
}
// If the current cycle wait count is larger than the new one (happens when the joystick is pushed further outside) run this if
else if (waitValueX > waitValueXNew)
{
// Lower the value to the new value
waitValueX = waitValueXNew;
}
}
else
{
// If the joystick is in the center, set th cycle wait count back to 0
waitValueX = 0;
}
if (sensorValueY < -100 && pressedKeyUpArrow == 0) {
Keyboard.press(0xDA); //KEY_UP_ARROW
pressedKeyUpArrow = 1;
} else if (sensorValueY > 100 && pressedKeyDownArrow == 0) {
Keyboard.press(0xD9); //KEY_DOWN_ARROW
pressedKeyDownArrow = 1;
// Check for movement on the Y axis
// Check if the joystick is moved to the front
if (sensorValueY < -10)
{
// Create a value for the number of cycles to wait before next key press
waitValueYNew = map(sensorValueY, 0, -sensorOffsetY, MAX_WAIT_CYCLES, 0);
// If the current cycle wait value is equal to 0 run this if.
// The contents of this if will immediately run when the joystick is comming from the 0 position on the X axis.
if (waitValueY == 0)
{
// Press the up arrow key
Keyboard.press(0xDA);
// Set a variable that the up arrow key is currently pressed
pressedKeyUpArrow = true;
// Update the cycls wait count with the new value
waitValueY = waitValueYNew;
}
// If the current cycle wait count is larger than the new one (happens when the joystick is pushed further outside) run this if
else if (waitValueY > waitValueYNew)
{
// Lower the value to the new value
waitValueY = waitValueYNew;
}
}
// Check if the joystick is moved to the back
else if (sensorValueY > 10)
{
// Create a value for the number of cycles to wait before next key press
waitValueYNew = map(sensorValueY, 0, 1023 - sensorOffsetY, MAX_WAIT_CYCLES, 0);
// If the current cycle wait value is equal to 0 run this if.
// The contents of this if will immediately run when the joystick is comming from the 0 position on the X axis.
if (waitValueY == 0)
{
// Press the down arrow key
Keyboard.press(0xD9);
// Set a variable that the down arrow key is currently pressed
pressedKeyDownArrow = true;
// Update the cycls wait count with the new value
waitValueY = waitValueYNew;
}
// If the current cycle wait count is larger than the new one (happens when the joystick is pushed further outside) run this if
else if (waitValueY > waitValueYNew)
{
// Lower the value to the new value
waitValueY = waitValueYNew;
}
}
else
{
// If the joystick is in the center, set th cycle wait count back to 0
waitValueY = 0;
}
if (sensorValueX > -100 && pressedKeyLeftArrow == 1) {
Keyboard.release(0xD8); //KEY_LEFT_ARROW
pressedKeyLeftArrow = 0;
} else if (sensorValueX < 100 && pressedKeyRightArrow == 1) {
Keyboard.release(0xD7); //KEY_RIGHT_ARROW
pressedKeyRightArrow = 0;
// Time to wait before keys will be released (if needed)
delay(DELAY_BETWEEN_KEYS);
// Release left arrow key if pressed
if (pressedKeyLeftArrow == true)
{
Keyboard.release(0xD8);
pressedKeyLeftArrow = false;
}
// Release right arrow key if pressed
if (pressedKeyRightArrow == true)
{
Keyboard.release(0xD7);
pressedKeyRightArrow = false;
}
// Release up arrow key if pressed
if (pressedKeyUpArrow == true)
{
Keyboard.release(0xDA);
pressedKeyUpArrow = false;
}
// Release down arrow key if pressed
if (pressedKeyDownArrow == true)
{
Keyboard.release(0xD9);
pressedKeyDownArrow = false;
}
if (sensorValueY > -100 && pressedKeyUpArrow == 1) {
Keyboard.release(0xDA); //KEY_UP_ARROW
pressedKeyUpArrow = 0;
} else if (sensorValueY < 100 && pressedKeyDownArrow == 1) {
Keyboard.release(0xD9); //KEY_DOWN_ARROW
pressedKeyDownArrow = 0;
// Lower the wait cycles for the X axis if not lower than 0
if (waitValueX > 0)
{
waitValueX--;
}
// Lower the wait cycles for the Y axis if not lower than 0
if (waitValueY > 0)
{
waitValueY--;
}
// Time to wait for the nex cycle and next possible key press
delay(DELAY_BETWEEN_KEYS);
}