MHeironimus / ArduinoJoystickLibrary

An Arduino library that adds one or more joysticks to the list of HID devices an Arduino Leonardo or Arduino Micro can support.
GNU Lesser General Public License v3.0
2.06k stars 403 forks source link

How to get encoder switches to work? #192

Closed laserbeak closed 3 years ago

laserbeak commented 3 years ago

Hi, Orignially, I used this code to get encoder switches to work:

    #define CLK_PIN  1
    #define DATA_PIN 0
    #define BUTTON  2

void setup() {
   pinMode(CLK_PIN,INPUT);
   pinMode(DATA_PIN,INPUT);
   pinMode(BUTTON, INPUT_PULLUP);

   Serial.begin(9600);
   Serial.println("Rotary Encoder KY-040");
}

void loop() {
static uint16_t state=0,counter=0;

    delayMicroseconds(100); // Simulate doing somehing else as well.

    state=(state<<1) | digitalRead(CLK_PIN) | 0xe000;

    if (state==0xf000){
       state=0x0000;
       if(digitalRead(DATA_PIN))
         counter++;
       else
         counter--;
       Serial.println(counter);
    }

    if (digitalRead(BUTTON)==0) {

      delay(10);
      if (digitalRead(BUTTON)==0) {
          Serial.println("Next Detent");
          while(digitalRead(BUTTON)==0);
      }
   }
}

How would I use your library to achieve something like this?

VkTheProgrammer17 commented 3 years ago

Encoder switches are just like normal 2-pin push button switches, so whatever code you have written for your switches, use that code for these encoder switches.

laserbeak commented 3 years ago

Alright, Figured it out. Here it is for anyone who's curious, or could show me a more elegant way of doing it. (2 Encoder Switches + 8 Buttons):

#include <Joystick.h>

#define CLK_PIN     1
#define DATA_PIN    0
#define ENC_BUTTON  2
#define CLK_PIN2    21
#define DATA_PIN2   20
#define ENC_BUTTON2 7
#define BUTTON1 3
#define BUTTON2 4
#define BUTTON3 5
#define BUTTON4 6
#define BUTTON5 19
#define BUTTON6 18
#define BUTTON7 15
#define BUTTON8 14

////////////////////////////////////////////////////
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
  14, 0,                  // Button Count, Hat Switch Count
  false, false, false,     // No X, Y, or Z Axis
  false, false, false,   // No Rx, Ry, or Rz
  false, false,          // No rudder or throttle
  false, false, false);  // No accelerator, brake, or steering
////////////////////////////////////////////////////
void setup() {
   pinMode(CLK_PIN,INPUT);
   pinMode(DATA_PIN,INPUT);
   pinMode(ENC_BUTTON, INPUT_PULLUP);
   pinMode(CLK_PIN2,INPUT);
   pinMode(DATA_PIN2,INPUT);
   pinMode(ENC_BUTTON2, 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(BUTTON8, INPUT_PULLUP);

   Serial.begin(9600);
   Serial.println("Rotary Encoder KY-040");
   Joystick.begin();
}

////////////////////////////////////////////////////
void loop() {
static uint16_t state=0,counter=0;
static uint16_t state1=0,counter1=0;

    delayMicroseconds(100); // Simulate doing somehing else as well.
    //Encoder 1
    state=(state<<1) | digitalRead(CLK_PIN) | 0xe000;

    if (state==0xf000){
       state=0x0000;
       if(digitalRead(DATA_PIN)){
         counter++;
         Joystick.pressButton(0);
         delay(100);
         Joystick.releaseButton(0);}
       else {
         counter--;
         Joystick.pressButton(1);
         delay(100);
         Joystick.releaseButton(1);}
       Serial.println(counter);
    }

    //Encoder 2
    state1=(state1<<1) | digitalRead(CLK_PIN2) | 0xe000;

    if (state1==0xf000){
       state1=0x0000;
       if(digitalRead(DATA_PIN2)){
         counter1++;
         Joystick.pressButton(2);
         delay(100);
         Joystick.releaseButton(2);}
       else{
         counter1--;
         Joystick.pressButton(3);
         delay(100);
         Joystick.releaseButton(3);}
       Serial.println(counter1);
    }

    //Encoder button 1
    if (digitalRead(ENC_BUTTON)==0) {

      delay(10);
      if (digitalRead(ENC_BUTTON)==0) {
          Serial.println("ENC_BUTTON");
          while(digitalRead(ENC_BUTTON)==0){
            Joystick.pressButton(4);
          }
          Joystick.releaseButton(4);
      }
   }

    //Encoder button 2
    if (digitalRead(ENC_BUTTON2)==0) {

      delay(10);
      if (digitalRead(ENC_BUTTON2)==0) {
          Serial.println("ENC_BUTTON2");
          while(digitalRead(ENC_BUTTON2)==0){
            Joystick.pressButton(5);
          }
          Joystick.releaseButton(5);
      }
   }
   //Button 1
   if (digitalRead(BUTTON1)==0) {

      delay(10);
      if (digitalRead(BUTTON1)==0) {
          Serial.println("BUTTON1");
          while(digitalRead(BUTTON1)==0){
            Joystick.pressButton(6);
          }
          Joystick.releaseButton(6);
      }
   }
   //Button 2
   if (digitalRead(BUTTON2)==0) {

      delay(10);
      if (digitalRead(BUTTON2)==0) {
          Serial.println("BUTTON2");
          while(digitalRead(BUTTON2)==0){
            Joystick.pressButton(7);
          }
          Joystick.releaseButton(7);
      }
   }
   //Button 3
   if (digitalRead(BUTTON3)==0) {

      delay(10);
      if (digitalRead(BUTTON3)==0) {
          Serial.println("BUTTON3");
          while(digitalRead(BUTTON3)==0){
            Joystick.pressButton(8);
          }
          Joystick.releaseButton(8);
      }
   }
   //Button 4
   if (digitalRead(BUTTON4)==0) {

      delay(10);
      if (digitalRead(BUTTON4)==0) {
          Serial.println("BUTTON4");
          while(digitalRead(BUTTON4)==0){
            Joystick.pressButton(9);
          }
          Joystick.releaseButton(9);
      }
   }
   //Button 5
   if (digitalRead(BUTTON5)==0) {

      delay(10);
      if (digitalRead(BUTTON5)==0) {
          Serial.println("BUTTON5");
          while(digitalRead(BUTTON5)==0){
            Joystick.pressButton(10);
          }
          Joystick.releaseButton(10);
      }
   }
   //Button 6
   if (digitalRead(BUTTON6)==0) {

      delay(10);
      if (digitalRead(BUTTON6)==0) {
          Serial.println("BUTTON6");
          while(digitalRead(BUTTON6)==0){
            Joystick.pressButton(11);
          }
          Joystick.releaseButton(11);
      }
   }
   //Button 7
   if (digitalRead(BUTTON7)==0) {

      delay(10);
      if (digitalRead(BUTTON7)==0) {
          Serial.println("BUTTON7");
          while(digitalRead(BUTTON7)==0){
            Joystick.pressButton(12);
          }
          Joystick.releaseButton(12);
      }
   }
   //Button 8
   if (digitalRead(BUTTON8)==0) {

      delay(10);
      if (digitalRead(BUTTON8)==0) {
          Serial.println("BUTTON8");
          while(digitalRead(BUTTON8)==0){
            Joystick.pressButton(13);
          }
          Joystick.releaseButton(13);
      }
   }
}
VkTheProgrammer17 commented 3 years ago

you are welcome

On Tue, Feb 16, 2021 at 7:10 AM M Martin notifications@github.com wrote:

Closed #192 https://github.com/MHeironimus/ArduinoJoystickLibrary/issues/192.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/MHeironimus/ArduinoJoystickLibrary/issues/192#event-4333964133, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARVJPNOWLP5M2H3ZGMXUPOTS7HEKVANCNFSM4XF3X42Q .