Why does this code not work?

#include <Keyboard.h>
#include <Encoder.h>
#include <Joystick.h>
//button press assignment
int button0 = 0;   
int button1 = 1;   
int button2 = 2;   
int button3 = 3;   
int button4 = 4;  
int button5 = 5;
int button6 = 6;
int button7 = 7;
//encoder turns assignment
int kll = 8;
int klr = 9;   
int krl = 10;   
int krr = 14;       
int kml = 15;
int kmr = 16;
Encoder knobLeft(8, 9);
Encoder knobRight(10, 14);
Encoder knobMiddle(15, 16);
//joystick assignment    
Joystick_ leftJoystick(0x01, JOYSTICK_TYPE_JOYSTICK, 2, 0, false, false, false, false, false, false, false, false, false, false, false);
Joystick_ rightJoystick(0x02, JOYSTICK_TYPE_JOYSTICK, 2, 0, false, false, false, false, false, false, false, false, false, false, false);

const bool testAutoSendMode = true;

int LJV;  
int LJH = 19;
int RJV = 20;
int RJH = 21;
int iLJV = 0;  
int iLJH = 0;
int iRJV = 0;
int iRJH = 0;
int nLJV;
int nLJH;
int nRJV;
int nRJH;
int mLJV = A0;
int mLJH = A1;
int mRJV = A2;
int mRJH = A3;
void setup() 
  {
  leftJoystick.begin();
  rightJoystick.begin();
  Keyboard.begin();
  
  pinMode(button0, INPUT_PULLUP);                        
  pinMode(button1, INPUT_PULLUP);                        
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);
  pinMode(button5, INPUT_PULLUP);
  pinMode(button6, INPUT_PULLUP);
  pinMode(button7, INPUT_PULLUP);
  pinMode(kll, INPUT_PULLUP);
  pinMode(klr, INPUT_PULLUP);
  pinMode(krl, INPUT_PULLUP);
  pinMode(krr, INPUT_PULLUP);
  pinMode(kml, INPUT_PULLUP);
  pinMode(kmr, INPUT_PULLUP);
  pinMode(mLJV, INPUT);
  pinMode(mLJH, INPUT);
  pinMode(mRJV, INPUT);
  pinMode(mRJH, INPUT);

  long positionLeft  = -999;
  long positionRight = -999;
  long positionMiddle = -999;


  }
void loop() 
{
  iLJV = analogRead(mLJV);
  iLJH = analogRead(mLJH);
  iRJV = analogRead(mRJV);
  iRJH = analogRead(mRJH);
  
  int LJV = map(mLJV, 0, 1023, -127, 127);
  int LJH = map(mLJH, 0, 1023, -127, 127);
  int RJV = map(mRJV, 0, 1023, -127, 127);
  int RJH = map(mRJH, 0, 1023, -127, 127);

  leftJoystick.setXAxis(mLJH);
  leftJoystick.setYAxis(mLJV);
  rightJoystick.setXAxis(mRJH);
  rightJoystick.setYAxis(mRJV);

  leftJoystick.sendState();
  rightJoystick.sendState();


  delay(50);

  long nLeft, nRight, nMiddle;
  int Left = knobLeft.read();
  int Middle = knobMiddle.read();
  int Right = knobRight.read();
  nLeft = knobLeft.read();
  nMiddle = knobMiddle.read();
  nRight = knobRight.read();
  char outputLeft = ' '; 
  char outputMiddle = ' '; 
  char outputRight = ' '; 
  if (nLeft != Left || nRight != Right || nMiddle != Middle) 
  {
    if (nLeft < Left) 
    {
      outputLeft = 'k'; 
      Keyboard.press('k');
      Keyboard.releaseAll();
    } 
    else if (nLeft > Left) 
    {
      outputLeft = 'r'; 
      Keyboard.press('r');
      Keyboard.releaseAll();
    }

    if (nRight < Right) 
    {
      outputRight = 'z'; 
      Keyboard.press('z');
      Keyboard.releaseAll();
    }
    else if (nRight > Right) 
    {
      outputRight = 'x'; 
      Keyboard.press('x');
      Keyboard.releaseAll();
    }

    if (nMiddle < Middle) 
    {
      outputMiddle = 'n'; 
      Keyboard.press('n');
      Keyboard.releaseAll();
    } 
    else if (nMiddle > Middle) 
    {
      outputMiddle = 'm';
      Keyboard.press('m');
      Keyboard.releaseAll();

    }

    if(button0 != LOW)
    {
        Keyboard.press("a");
        Keyboard.releaseAll();

    }
    if(button1 != LOW)
    {
        Keyboard.press("b");
        Keyboard.releaseAll();

    }
    if(button2 != LOW)
    {
        Keyboard.press("c");
        Keyboard.releaseAll();

    }
    if(button3 != LOW)
    {
        Keyboard.press("d");
        Keyboard.releaseAll();

    }
    if(button4 != LOW)
    {
        Keyboard.press("e");
        Keyboard.releaseAll();

    }
    if(button5 != LOW)
    {
        Keyboard.press("f");
        Keyboard.releaseAll();

    }
    if(button6 != LOW)
    {
        Keyboard.press("g");
        Keyboard.releaseAll();

    }
    if(button7 != LOW)
    {
        Keyboard.press("h");
        Keyboard.releaseAll();

    }

  }

}
  


This is my code, the purpose is this: Sends different key inputs depending how the rotary encoder is turned. Send joystick inputs. Sends keypress if a button is pressed, I have compiled and check myself so logic errors. Does anyone have any logic errors they can see? Everything is sent over HID

What happened when yo tried it?

All of these:

map(mRJH, 0, 1023, -127, 127)

Should be iRJH

What is the code supposed to do??

local variables at the end of the setup are useless


what are those for

you set them but don't use the variable for the keyboard press event, it's hardcoded.


why do you read twice in duplicated variables?

that's not the way to detect change from the previously sent value..

#include <Keyboard.h>
#include <Encoder.h>
#include <Joystick.h>
//button press assignment
int button0 = 0;   
int button1 = 1;   
int button2 = 2;   
int button3 = 3;   
int button4 = 4;  
int button5 = 5;
int button6 = 6;
int button7 = 7;
//encoder turns assignment
int kll = 8;
int klr = 9;   
int krl = 10;   
int krr = 14;       
int kml = 15;
int kmr = 16;
Encoder knobLeft(8, 9);
Encoder knobRight(10, 14);
Encoder knobMiddle(15, 16);
//joystick assignment    
Joystick_ leftJoystick(0x01, JOYSTICK_TYPE_JOYSTICK, 2, 0, false, false, false, false, false, false, false, false, false, false, false);
Joystick_ rightJoystick(0x02, JOYSTICK_TYPE_JOYSTICK, 2, 0, false, false, false, false, false, false, false, false, false, false, false);

const bool testAutoSendMode = true;

int LJV;  
int LJH = 19;
int RJV = 20;
int RJH = 21;
int iLJV = 0;  
int iLJH = 0;
int iRJV = 0;
int iRJH = 0;
int nLJV;
int nLJH;
int nRJV;
int nRJH;
int mLJV = A0;
int mLJH = A1;
int mRJV = A2;
int mRJH = A3;
void setup() 
  {
  leftJoystick.begin();
  rightJoystick.begin();
  Keyboard.begin();
  
  pinMode(button0, INPUT_PULLUP);                        
  pinMode(button1, INPUT_PULLUP);                        
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);
  pinMode(button5, INPUT_PULLUP);
  pinMode(button6, INPUT_PULLUP);
  pinMode(button7, INPUT_PULLUP);
  pinMode(kll, INPUT_PULLUP);
  pinMode(klr, INPUT_PULLUP);
  pinMode(krl, INPUT_PULLUP);
  pinMode(krr, INPUT_PULLUP);
  pinMode(kml, INPUT_PULLUP);
  pinMode(kmr, INPUT_PULLUP);
  pinMode(mLJV, INPUT);
  pinMode(mLJH, INPUT);
  pinMode(mRJV, INPUT);
  pinMode(mRJH, INPUT);


  }
void loop() 
{
  iLJV = analogRead(mLJV);
  iLJH = analogRead(mLJH);
  iRJV = analogRead(mRJV);
  iRJH = analogRead(mRJH);
  
  int iLJV = map(mLJV, 0, 1023, -127, 127);
  int iLJH = map(mLJH, 0, 1023, -127, 127);
  int iRJV = map(mRJV, 0, 1023, -127, 127);
  int iRJH = map(mRJH, 0, 1023, -127, 127);

  leftJoystick.setXAxis(mLJH);
  leftJoystick.setYAxis(mLJV);
  rightJoystick.setXAxis(mRJH);
  rightJoystick.setYAxis(mRJV);

  leftJoystick.sendState();
  rightJoystick.sendState();


  delay(50);

  long oLeft = -999;
  long oRight = -999;
  long oMiddle = -999;
  int Left = knobLeft.read();
  int Middle = knobMiddle.read();
  int Right = knobRight.read();
  
  if (oLeft != Left || oRight != Right || oMiddle != Middle) 
  {
    if (oLeft < Left) 
    {
      Keyboard.press('k');
      Keyboard.releaseAll();
    } 
    else if (oLeft > Left) 
    {
      Keyboard.press('r');
      Keyboard.releaseAll();
    }

    if (oRight < Right) 
    {
      Keyboard.press('z');
      Keyboard.releaseAll();
    }
    else if (oRight > Right) 
    {
      Keyboard.press('x');
      Keyboard.releaseAll();
    }

    if (oMiddle < Middle) 
    {
      Keyboard.press('n');
      Keyboard.releaseAll();
    } 
    else if (oMiddle > Middle) 
    {
      Keyboard.press('m');
      Keyboard.releaseAll();

    }

    if(button0 != LOW)
    {
        Keyboard.press("a");
        Keyboard.releaseAll();

    }
    if(button1 != LOW)
    {
        Keyboard.press("b");
        Keyboard.releaseAll();

    }
    if(button2 != LOW)
    {
        Keyboard.press("c");
        Keyboard.releaseAll();

    }
    if(button3 != LOW)
    {
        Keyboard.press("d");
        Keyboard.releaseAll();

    }
    if(button4 != LOW)
    {
        Keyboard.press("e");
        Keyboard.releaseAll();

    }
    if(button5 != LOW)
    {
        Keyboard.press("f");
        Keyboard.releaseAll();

    }
    if(button6 != LOW)
    {
        Keyboard.press("g");
        Keyboard.releaseAll();

    }
    if(button7 != LOW)
    {
        Keyboard.press("h");
        Keyboard.releaseAll();

    }

  }

}
  


Would this fix these previous errors, especially the change detection on the rotary encoder.

It is supposed to be a controller

why did you use 0x01 and 0x02...

you had two other topics that got merged on understanding the joystick library

it was made clear not to use 1 and 2...

did I loose my time over there?


if you set oLeft to -999 and use it for comparison right afterwards, it probably won't do what you want...

You are still mapping the pin number. Not the reading from the pin....
It is fully legal to do so. But probably not what you want.

Would this work?

#include <Keyboard.h>
#include <Encoder.h>
#include <Joystick.h>
//button press assignment
const byte button0 = 0;   
const byte button1 = 1;   
const byte button2 = 2;   
const byte button3 = 3;   
const byte button4 = 4;  
const byte button5 = 5;
const byte button6 = 6;
const byte button7 = 7;
//encoder turns assignment
const byte kll = 8;
const byte klr = 9;   
const byte krl = 10;   
const byte krr = 14;       
const byte kml = 15;
const byte kmr = 16;
Encoder knobLeft(8, 9);
Encoder knobRight(10, 14);
Encoder knobMiddle(15, 16);
//joystick assignment    
Joystick_ leftJoystick(0x15, JOYSTICK_TYPE_JOYSTICK, 2, 0, false, false, false, false, false, false, false, false, false, false, false);
Joystick_ rightJoystick(0x12, JOYSTICK_TYPE_JOYSTICK, 2, 0, false, false, false, false, false, false, false, false, false, false, false);

const bool testAutoSendMode = true;

int oLeft = -999;
int oRight = -999;
int oMiddle = -999;

const byte LJV = 18;  
const byte LJH = 19;
const byte RJV = 20;
const byte RJH = 21;
int nLJV;
int nLJH;
int nRJV;
int nRJH;
int mLJV = A0;
int mLJH = A1;
int mRJV = A2;
int mRJH = A3;
void setup() 
  {
  leftJoystick.begin();
  rightJoystick.begin();
  Keyboard.begin();
  
  pinMode(button0, INPUT_PULLUP);                        
  pinMode(button1, INPUT_PULLUP);                        
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);
  pinMode(button5, INPUT_PULLUP);
  pinMode(button6, INPUT_PULLUP);
  pinMode(button7, INPUT_PULLUP);
  pinMode(kll, INPUT_PULLUP);
  pinMode(klr, INPUT_PULLUP);
  pinMode(krl, INPUT_PULLUP);
  pinMode(krr, INPUT_PULLUP);
  pinMode(kml, INPUT_PULLUP);
  pinMode(kmr, INPUT_PULLUP);
  pinMode(mLJV, INPUT);
  pinMode(mLJH, INPUT);
  pinMode(mRJV, INPUT);
  pinMode(mRJH, INPUT);


  }
void loop() 
{
  int iLJV = analogRead(mLJV);
  int iLJH = analogRead(mLJH);
  int iRJV = analogRead(mRJV);
  int iRJH = analogRead(mRJH);
  
  int nLJV = map(iLJV, 0, 1023, -127, 127);
  int nLJH = map(iLJH, 0, 1023, -127, 127);
  int nRJV = map(iRJV, 0, 1023, -127, 127);
  int nRJH = map(iRJH, 0, 1023, -127, 127);

  leftJoystick.setXAxis(mLJH);
  leftJoystick.setYAxis(mLJV);
  rightJoystick.setXAxis(mRJH);
  rightJoystick.setYAxis(mRJV);

  leftJoystick.sendState();
  rightJoystick.sendState();


  delay(50);

  int Left = knobLeft.read();
  int Middle = knobMiddle.read();
  int Right = knobRight.read();
  
  if (oLeft != Left || oRight != Right || oMiddle != Middle) 
  {
    if (oLeft < Left) 
    {
      Keyboard.press('k');
      Keyboard.releaseAll();
      oLeft = Left;
    } 
    else if (oLeft > Left) 
    {
      Keyboard.press('r');
      Keyboard.releaseAll();
      oLeft = Left;

    }

    if (oRight < Right) 
    {
      Keyboard.press('z');
      Keyboard.releaseAll();
      oRight = Right;
    }
    else if (oRight > Right) 
    {
      Keyboard.press('x');
      Keyboard.releaseAll();
      oRight = Right;
    }
  
    if (oMiddle < Middle) 
    {
      Keyboard.press('n');
      Keyboard.releaseAll();
      oMiddle = Middle;
    } 
    else if (oMiddle > Middle) 
    {
      Keyboard.press('m');
      Keyboard.releaseAll();
      oMiddle = Middle;

    }

    if(button0 != LOW)
    {
        Keyboard.press("a");
        Keyboard.releaseAll();

    }
    if(button1 != LOW)
    {
        Keyboard.press("b");
        Keyboard.releaseAll();

    }
    if(button2 != LOW)
    {
        Keyboard.press("c");
        Keyboard.releaseAll();

    }
    if(button3 != LOW)
    {
        Keyboard.press("d");
        Keyboard.releaseAll();

    }
    if(button4 != LOW)
    {
        Keyboard.press("e");
        Keyboard.releaseAll();

    }
    if(button5 != LOW)
    {
        Keyboard.press("f");
        Keyboard.releaseAll();

    }
    if(button6 != LOW)
    {
        Keyboard.press("g");
        Keyboard.releaseAll();

    }
    if(button7 != LOW)
    {
        Keyboard.press("h");
        Keyboard.releaseAll();

    }

  }

}
  


You still mixed a global and local variables with the same names:

Please read something about variable scope:
https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/scope/

@donut
Your code demonstrates that you lack the most basic knowledge. Now you're just shuffling around lines of code and waiting to see if they start working. That's not how to program.
I would recommend you start with some basic book on Arduino and C language.

1 Like

In industry a code review is not to determine if code works or not.

It is to review the style of the code, and its efficiency, or adherence to company style.

So this is not a code review. It is simply a very raw beginner with no idea how to code, pretending he does know, in stead of just asking why his code does not actually function the way he thought it would. Please modify your title to reflect this fact.

2 Likes

So from what I understand I don't have a need for local variables so if i change it to this:

#include <Keyboard.h>
#include <Encoder.h>
#include <Joystick.h>

// Button pin assignments
const byte button0 = 0;   
const byte button1 = 1;   
const byte button2 = 2;   
const byte button3 = 3;   
const byte button4 = 4;  
const byte button5 = 5;
const byte button6 = 6;
const byte button7 = 7;

// Encoder pin assignments
const byte kll = 8;
const byte klr = 9;   
const byte krl = 10;   
const byte krr = 14;       
const byte kml = 15;
const byte kmr = 16;

// Encoder objects
Encoder knobLeft(kll, klr);
Encoder knobRight(krl, krr);
Encoder knobMiddle(kml, kmr);

// Joystick objects  
Joystick_ leftJoystick(0x15, JOYSTICK_TYPE_JOYSTICK, 2, 0, false, false, false, false, false, false, false, false, false, false, false);
Joystick_ rightJoystick(0x12, JOYSTICK_TYPE_JOYSTICK, 2, 0, false, false, false, false, false, false, false, false, false, false, false);

const bool testAutoSendMode = true;

int oLeft = -999;
int oRight = -999;
int oMiddle = -999;

const byte LJV = 18;  
const byte LJH = 19;
const byte RJV = 20;
const byte RJH = 21;
int nLJV;
int nLJH;
int nRJV;
int nRJH;
int mLJV = A0;
int mLJH = A1;
int mRJV = A2;
int mRJH = A3;

void setup() {
  leftJoystick.begin();
  rightJoystick.begin();
  Keyboard.begin();
  
  pinMode(button0, INPUT_PULLUP);                        
  pinMode(button1, INPUT_PULLUP);                        
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);
  pinMode(button5, INPUT_PULLUP);
  pinMode(button6, INPUT_PULLUP);
  pinMode(button7, INPUT_PULLUP);
  pinMode(kll, INPUT_PULLUP);
  pinMode(klr, INPUT_PULLUP);
  pinMode(krl, INPUT_PULLUP);
  pinMode(krr, INPUT_PULLUP);
  pinMode(kml, INPUT_PULLUP);
  pinMode(kmr, INPUT_PULLUP);
  pinMode(mLJV, INPUT);
  pinMode(mLJH, INPUT);
  pinMode(mRJV, INPUT);
  pinMode(mRJH, INPUT);
}

void loop() {
  int iLJV = analogRead(mLJV);
  int iLJH = analogRead(mLJH);
  int iRJV = analogRead(mRJV);
  int iRJH = analogRead(mRJH);
  
  nLJV = map(iLJV, 0, 1023, -127, 127);
  nLJH = map(iLJH, 0, 1023, -127, 127);
  nRJV = map(iRJV, 0, 1023, -127, 127);
  nRJH = map(iRJH, 0, 1023, -127, 127);

  leftJoystick.setXAxis(mLJH);
  leftJoystick.setYAxis(mLJV);
  rightJoystick.setXAxis(mRJH);
  rightJoystick.setYAxis(mRJV);

  leftJoystick.sendState();
  rightJoystick.sendState();

  delay(50);

  int Left = knobLeft.read();
  int Middle = knobMiddle.read();
  int Right = knobRight.read();
  
  if (oLeft != Left || oRight != Right || oMiddle != Middle) {
    if (oLeft < Left) {
      Keyboard.press('k');
      Keyboard.releaseAll();
      oLeft = Left;
    } else if (oLeft > Left) {
      Keyboard.press('r');
      Keyboard.releaseAll();
      oLeft = Left;
    }

    if (oRight < Right) {
      Keyboard.press('z');
      Keyboard.releaseAll();
      oRight = Right;
    } else if (oRight > Right) {
      Keyboard.press('x');
      Keyboard.releaseAll();
      oRight = Right;
    }
  
    if (oMiddle < Middle) {
      Keyboard.press('n');
      Keyboard.releaseAll();
      oMiddle = Middle;
    } else if (oMiddle > Middle) {
      Keyboard.press('m');
      Keyboard.releaseAll();
      oMiddle = Middle;
    }

    // Read button states and send corresponding keystrokes
    switch0 = digitalRead(button0);
    if (switch0 == LOW) {
        Keyboard.press("a");
        Keyboard.releaseAll();
    }

    switch1 = digitalRead(button1);
    if (switch1 == LOW) {
        Keyboard.press("b");
        Keyboard.releaseAll();
    }

    switch2 = digitalRead(button2);
    if (switch2 == LOW) {
        Keyboard.press("c");
        Keyboard.releaseAll();
    }

    switch3 = digitalRead(button3);
    if (switch3 == LOW) {
        Keyboard.press("d");
        Keyboard.releaseAll();
    }

    switch4 = digitalRead(button4);
    if (switch4 == LOW) {
        Keyboard.press("e");
        Keyboard.releaseAll();
    }

    switch5 = digitalRead(button5);
    if (switch5 == LOW) {
        Keyboard.press("f");
        Keyboard.releaseAll();
    }

    switch6 = digitalRead(button6);
    if (switch6 == LOW) {
        Keyboard.press("g");
        Keyboard.releaseAll();
    }

    switch7 = digitalRead(button7);
    if (switch7 == LOW) {
        Keyboard.press("h");
        Keyboard.releaseAll();
    }
  }
}

It should be correct, the keypresses are now triggered by button state, and it should work?

hum what are iLJV, iLJH, iRJV and iRJH if not local variables... ?

(but nothing wrong with having local variables where they make sense)

You forget to declare your new switch0-switch7 variables

Thank you.

Before asking this on a forum, it would always be better trying to compile your code first.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.