0

im building my second arduino project. Im very new to this all so I could certainly use some help fixing everything.
Im trying to make a controller for a pc port of an arcade game. It uses two rotary encoders, something I never heard about before going into this project

I had found some code on github that works perfectly in my case, but the code has the rotary encoders move the mouse. something that dosent work for the games controls unfortunately (i tried)

so i attempted to change them and now they do spit out text like i need them but when i rotate them instead of getting the needed out put of

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

i get something more along the lines of

babababababababababababababababbabb

like if im turning the knob both ways at the same time when im just turning it one direction.


heres the code im working on

/*
* By Daniel Tam (daniel@danieltam.net)
============ NOTES ============
This will create a SDVX Controller using an Arduino Leonardo. 
You will hook up one wire of the button to the Arduino pin and the other to Arduino's ground. When pushed, the button should go to ground.
You will need to download the encoder library and use it in your project for this to work (ww.circuitsathome.com/mcu/reading-rotary-encoder-on-arduino)
===============================
*/

// buttons
#include <Mouse.h>
#include <Keyboard.h>
#define BT_A 2
#define BT_B 3
#define BT_C 4
#define BT_D 5
#define FX_L 6
#define FX_R 7
#define BT_ST 8

// encoders
#include <Encoder.h>
Encoder enc1(15, 14);
Encoder enc2(16, 10);
float knob1 = 0;
float knob2 = 0;
float old_knob1 = 0;
float old_knob2 = 0;

void setup()
{ 
  // set pins to read, and output high
  // when the pins read low, we know that the buttons have went to ground (aka, pushed down)
  pinMode(BT_A, INPUT);
  pinMode(BT_B, INPUT);
  pinMode(BT_C, INPUT);
  pinMode(BT_D, INPUT);
  pinMode(FX_L, INPUT);
  pinMode(FX_R, INPUT);
  pinMode(BT_ST, INPUT);

  digitalWrite(BT_A, HIGH);
  digitalWrite(BT_B, HIGH);
  digitalWrite(BT_C, HIGH);
  digitalWrite(BT_D, HIGH);
  digitalWrite(FX_L, HIGH);
  digitalWrite(FX_R, HIGH);
  digitalWrite(BT_ST, HIGH);

  Mouse.begin();
  Keyboard.begin();
}

void loop()
{
  // read encoders
  knob1 =  (float)(enc1.read());
  knob2 = (float)enc2.read();

  if(knob1 != old_knob1)
  {
    // if there's a difference in encoder movement from last pass, move the mouse
    if(knob1 < old_knob1)
    {
     // Mouse.move(0, -5);
      Keyboard.press('a');
      delay(100);
      Keyboard.release('a');
    }
    else
    {
    //  Mouse.move(0, 5);
      Keyboard.press('b');
      delay(100);
      Keyboard.release('b');
    }

  // we count the difference in the encoders, but we must not go over arduino's int limit
    if(knob1 < -255)
    {
      enc1.write(0);
      old_knob1 = 0;
    }
    else if (knob1 > 255)
    {
      enc1.write(0);
      old_knob1 = 0;
    }
    else
    {
      old_knob1 = knob1;
    }
  }

  if(knob2 != old_knob2)
  {
    if(knob2 > old_knob2)
    {
     // Mouse.move(-5, 0);
      Keyboard.press('c');
    }
    else
    {
     // Mouse.move(5, 0);
      Keyboard.press('d');
    }

    if(knob2 < -255)
    {
      enc2.write(0);
      old_knob2 = 0;
    }
    else if(knob2 > 255)
    {
      enc2.write(0);
      old_knob2 = 0;
    }
    else
    {
      old_knob2 = knob2;
    }
  }

  // read the buttons for low, if it's low, output a keyboard press  
  if(digitalRead(BT_A) == LOW)
  {
    Keyboard.press('f');
  }
  else
  {
    Keyboard.release('f');
  }

  if(digitalRead(BT_B) == LOW)
  {
    Keyboard.press('b');
  }
  else
  {
    Keyboard.release('b');
  }

  if(digitalRead(BT_C) == LOW)
  {
    Keyboard.press('c');
  }
  else
  {
    Keyboard.release('c');
  }

  if(digitalRead(BT_D) == LOW)
  {
    Keyboard.press('d');
  }
  else
  {
    Keyboard.release('d');
  }

  if(digitalRead(FX_L) == LOW)
  {
    Keyboard.press('z');
  }
  else
  {
    Keyboard.release('z');
  }

  if(digitalRead(FX_R) == LOW)
  {
    Keyboard.press('x');
  }
  else
  {
    Keyboard.release('x');
  }

  if(digitalRead(BT_ST) == LOW)
  {
    Keyboard.press('n');
  }
  else
  {
    Keyboard.release('n');
  }
}
0

Your aren't using the recommended pins to attach the encoders. Because of this the library isn't using interrupts, but depends on you calling encX.read() often enough, to not miss any changes. This, combined with the 100ms delay in your code, will almost guarantee that you will miss some changes, making the library think you are rotating the other way around.

Change the pins to 0, 1, 2, 3 (or at least one of those for each encoder).

Otherwise lower the delay to for example 1ms. I couldn't find whether this delay is even necessary, or how long it needs to be, so you'd have to experiment a bit, and see what works.

Lastly; why are you using float to store the position? Try using long instead, as that's the type enc.read() returns.

  • was there a first part that got cut off? – MintCollie Sep 25 '18 at 18:00
  • Sorry, I had to change the order around. Nothing missing. – Gerben Sep 25 '18 at 18:08
  • 1
    changing pins to 0,1 seems to do exactly what i need! I had the delay in as a test as im still learning and as for the floats that's part of the original guys code so im not too sure. – MintCollie Sep 25 '18 at 18:09
  • actually i seem to intermittently get the old result, seems to work sometimes. – MintCollie Sep 26 '18 at 0:54
  • nvm seems most of my issues stem from a wonky breadboard thats connecting the pins or something. – MintCollie Sep 26 '18 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.