-1

I'm new in coding with Arduino, and I am trying to make a one hand keyboard with joystick as personal project. I tried to make it print some ASCII characters, but I don't know why the button keeps pushed and don't show the ASCII I declare.

#include <Keyboard.h>
#include <Mouse.h>
//IndexFinger=IF,MiddleFinger=MF,RingFinger=RF
//LittleFinger=LF
int fingerCodes[3][4]={
  {LF_Up, RF_Up, MF_Up, IF_Up},
  {LF_Mid, RF_Mid, MF_Mid, IF_Mid},
  {LF_Down, RF_Down, MF_Down, IF_Down}
}
int asciiCodes[3][4]={
  {33, 34, 35, 36},
  {37, 38, 39, 40},
  {41, 42, 43, 44}
}
//a
int x=0;
int y=0;
const int joystickVRx1 = A0;
const int joystickVRy1 = A1;
const int joystickButton1 = 14;

// Define the FPS
const int FPS = 120;

// Initialize the last time variable
unsigned long lastTime = 5;

// Define the functions
void finger_ButtonsControl(int x, int y);
void joystickMovementControl(int joystickvrx, int joystickvry, int joystickButton1);
void setup() {
  // Initialize the serial port
  Serial.begin(115200);
  // Set the button pins as input
  pinMode(joystickVRx1,INPUT);
  pinMode(joystickVRy1,INPUT);
  for (int i = 2;i<11;i++){
    pinMode(i,OUTPUT);
  }
  Keyboard.begin();
  Mouse.begin();
}

void loop() {
  // Check if it's time to update the controls
  if ((millis() - lastTime) > 5) {
    // Update finger buttons
    finger_ButtonsControl(x,y);
    // Update the joystick movement
    joystickMovementControl(joystickVRx1, joystickVRy1, joystickButton1);
    // Reset the last time variable
    lastTime = millis();
  }
  Keyboard.releaseAll();
}

// Function to control finger buttons
void finger_ButtonsControl(int x, int y) {
  // Check if a button is pressed
  for (int x=2;x<5;x++){
    for (int y=5;y<9;y++){
      buttonState_x = digitalRead(x);
      buttonState_y = digitalRead(y);
      if (buttonState_x == HIGH && buttonState_y == HIGH){
        x=x-2;
        y=y-5;
        Keyboard.write(asciiCodes[x][y]);
      }
}

// Function to control the joystick movement
void joystickMovementControl(int joystickvrx, int joystickvry, int joystickButton1) {
  // Read the joystick values
  int x = analogRead(joystickvrx);
  int y = analogRead(joystickvry);
  if (digitalRead(joystickButton1) == LOW) {
    Keyboard.write(245);
  }
}


1
  • try defining joystick pins as INPUT_PULLUP Commented Jul 13, 2024 at 1:49

1 Answer 1

0

Not sure this is your issue, but there is definitely a problem here:

void finger_ButtonsControl(int x, int y) {
    // Check if a button is pressed
    for (int x = 2; x <5; x++) {
        for (int y = 5; y < 9; y++) {
            buttonState_x = digitalRead(x);
            buttonState_y = digitalRead(y);
            if (buttonState_x == HIGH && buttonState_y == HIGH) {
                x = x - 2;  // <-- problem here...
                y = y - 5;  // <-- ...and here
                Keyboard.write(asciiCodes[x][y]);
            }
        }
    }
}

Think of what would happen if you press buttons 2 and 5. On the first iteration of the loops, you will have x = 2 and y = 5, the if condition will be true, and the problematic lines will turn both x and y to 0. Then, the inner loop will increment y and, on the next iteration, you will read the pins x = 0 and y = 1. Since this is a (presumably idle) serial port, both pins will read HIGH. Then the problematic lines will turn x to −2 and y to −4. At this point reading asciiCodes[x][y] invokes undefined behavior and anything can happen.

I believe what you intended to write was:

void finger_ButtonsControl(int x, int y) {
    // Check if a button is pressed
    for (int x = 2; x <5; x++) {
        for (int y = 5; y < 9; y++) {
            buttonState_x = digitalRead(x);
            buttonState_y = digitalRead(y);
            if (buttonState_x == HIGH && buttonState_y == HIGH) {
                Keyboard.write(asciiCodes[x-2][y-5]);
            }
        }
    }
}
1
  • :0 Wow, I got days thinking of what to do, and you answer was so good. Thanks for helping me. I never thought that the code would take the result of the substraction as a "parameter-ish" in the loop, and giving me those negative values led to wrong values Commented Jul 27, 2024 at 4:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.