1

I built a button box, everything is wired up and all of the push buttons work using the code below, but for some reason my rotary encoders are not working, well 3 out of the 4 are not working. Is there something in my code that I messed up? Should rotary encoders not be linked to analog pins? Any guidance would be appreciated.

EDIT - Correct code replacing incorrect code.

#include <Keyboard.h>
#include <SimpleRotary.h>

#define NUMROWS 6
#define NUMCOLS 4

const int ModPin = A0;
int ModState = 0; 

//define the symbols on the buttons of the keypads
char buttons[NUMROWS][NUMCOLS] = {
  {'7','5','3','1'},
  {'6','4','2','0'},
  {'r','e','w','q'},
  {'i','u','y','t'},
  {'f','d','s','a'},
  {'p','j','h','g'},
};

char buttonsMod[NUMROWS][NUMCOLS] = {
  {'7','5','3','1'},
  {'6','4','2','0'},
  {0xC9,0xC7,0xC5,0xC3},
  {0xC8,0xC6,0xC4,0xC2},
  {'f','d','s','a'},
  {'p','j','h','g'},
};



byte rowPins[NUMROWS] = {13,12,11,10,9,8}; //connect to the row pinouts of the keypad
byte colPins[NUMCOLS] = {7,6,5,4}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS); 
Keypad buttbxMod = Keypad( makeKeymap(buttonsMod), rowPins, colPins, NUMROWS, NUMCOLS); 

SimpleRotary RotaryA(1,2,3);
SimpleRotary RotaryB(0,A5,3);
SimpleRotary RotaryC(A4,A3,3);
SimpleRotary RotaryD(A2,A1,3);

void setup() {
  Keyboard.begin();
  pinMode(ModPin, INPUT_PULLUP);
}



void loop() { 
ModState = digitalRead(ModPin);
  if (ModState == HIGH) {
    CheckAllButtons();
  } else {
  CheckAllButtonsMod();
  }
  

byte i;

  // 0 = not turning, 1 = CW, 2 = CCW
  
  i = RotaryA.rotate();

  if ( i == 1 ) {
    Keyboard.press(KEY_LEFT_ARROW);
    delay(150);
    Keyboard.releaseAll();
  }
  if ( i == 2 ) {
    Keyboard.press(KEY_RIGHT_ARROW);
    delay(150);
    Keyboard.releaseAll();
  }

  i = RotaryB.rotate();
  
  if ( i == 1 ) {
    Keyboard.press(KEY_LEFT_ARROW);
    delay(150);
    Keyboard.releaseAll();
  }
  if ( i == 2 ) {
    Keyboard.press(KEY_RIGHT_ARROW);
    delay(150);
    Keyboard.releaseAll();
  }
  
  i = RotaryC.rotate();
  
  if ( i == 1 ) {
    Keyboard.press(KEY_LEFT_ARROW);
    delay(150);
    Keyboard.releaseAll();
  }
  if ( i == 2 ) {
    Keyboard.press(KEY_RIGHT_ARROW);
    delay(150);
    Keyboard.releaseAll();
  }
  
  i = RotaryD.rotate();
  
  if ( i == 1 ) {
    Keyboard.press(KEY_LEFT_ARROW);
    delay(150);
    Keyboard.releaseAll();
  }
  if ( i == 2 ) {
    Keyboard.press(KEY_RIGHT_ARROW);
    delay(150);
    Keyboard.releaseAll();
  }
}


void CheckAllButtons(void) {
  char key = buttbx.getKey();
  if (key != NO_KEY)  {
    Keyboard.press(key);
    delay(150);
    Keyboard.releaseAll();
  }//Keyboard.write(key);
}

void CheckAllButtonsMod(void) {
  char key = buttbxMod.getKey();
  if (key != NO_KEY)  {
    Keyboard.press(key);
    delay(150);
    Keyboard.releaseAll();
  }//Keyboard.write(key);
}
4
  • 1
    In your loop you only ever check A and B. You never do anything with C and D. Were you copy-pasting and forgot to change the B's to C's and D's? It looks like you read B three times. Are C & D the ones not working? If this is for gaming then you're going to find that with all the delay(150) in there it won't be very responsive. Hope it's a slow game.
    – Delta_G
    Sep 7, 2020 at 15:19
  • One more hint for you, avoid always naming things with single letters. Single lower case letters just suck to read. Single upper case letters are often used as macros by the core. You can end up with some strange errors that are hard to debug when those two clash. Name them encoderA and encoderB or something. But just A, B, C, and D is something to avoid.
    – Delta_G
    Sep 7, 2020 at 15:20
  • some of the code lines inside loop() are incorrectly indented ... that makes it difficult to see the end of the loop() block
    – jsotola
    Sep 7, 2020 at 16:36
  • It seems that copy paste is a bad idea, After correcting that error things seem to be working now, except for the encoder wired to 0/A5, which workes sometimes and other times it triggers the "p" code, I suspect a wiring fault, will have to go hunting.
    – Nc Bean
    Sep 7, 2020 at 18:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.