Hi all,
I'm working on a button box project using encoders, switches, and buttons. I have a teensy++ 2.0 and have been trying to get my encoders to work as button input, with a clockwise rotation being one button, counter clockwise being another button, and the encoder's click being a third button. I will have four of these encoders, which as near as I can tell are http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/keyes-ky-040-arduino-rotary-encoder-user-manual/ one.
I have got it mostly working with a bit of test code and one encoder. The problem is that it seems somewhat unreliable, and I can't spin the encoder very fast or else it won't register any movement. I'll include the code below, but in essence it simply waits for input from the encoder, and then depending on the rotation it turns on a joystick button for a certain amount of time before turning it off. This is achieved with the delay command. If I make the delay too short, the button input won't be read. But if I make the delay too long then the maximum speed that you can turn the dial at is limited. Is there a better way to approach this? Thanks in advance.
I'm working on a button box project using encoders, switches, and buttons. I have a teensy++ 2.0 and have been trying to get my encoders to work as button input, with a clockwise rotation being one button, counter clockwise being another button, and the encoder's click being a third button. I will have four of these encoders, which as near as I can tell are http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/keyes-ky-040-arduino-rotary-encoder-user-manual/ one.
I have got it mostly working with a bit of test code and one encoder. The problem is that it seems somewhat unreliable, and I can't spin the encoder very fast or else it won't register any movement. I'll include the code below, but in essence it simply waits for input from the encoder, and then depending on the rotation it turns on a joystick button for a certain amount of time before turning it off. This is achieved with the delay command. If I make the delay too short, the button input won't be read. But if I make the delay too long then the maximum speed that you can turn the dial at is limited. Is there a better way to approach this? Thanks in advance.
Code:
/* Encoder Library - NoInterrupts Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
// If you define ENCODER_DO_NOT_USE_INTERRUPTS *before* including
// Encoder, the library will never use interrupts. This is mainly
// useful to reduce the size of the library when you are using it
// with pins that do not support interrupts. Without interrupts,
// your program must call the read() function rapidly, or risk
// missing changes in position.
#define ENCODER_DO_NOT_USE_INTERRUPTS
#include <Encoder.h>
#include <Bounce.h>
// Beware of Serial.print() speed. Without interrupts, if you
// transmit too much data with Serial.print() it can slow your
// reading from Encoder. Arduino 1.0 has improved transmit code.
// Using the fastest baud rate also helps. Teensy has USB packet
// buffering. But all boards can experience problems if you print
// too much and fill up buffers.
// Change these two numbers to the pins connected to your encoder.
// With ENCODER_DO_NOT_USE_INTERRUPTS, no interrupts are ever
// used, even if the pin has interrupt capability
Encoder myEnc(2, 3);
// avoid using pins with LEDs attached
Bounce button1 = Bounce(2, 10);
Bounce button2 = Bounce(3, 10);
Bounce button8 = Bounce(8, 10);
void setup() {
Serial.begin(9600);
Serial.println("Basic NoInterrupts Test:");
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
}
long oldPosition = -999;
void loop() {
button1.update();
button2.update();
button8.update();
if (button8.fallingEdge()) {
Joystick.button(3, 1);
}
if (button8.risingEdge()) {
Joystick.button(3, 0);
}
long newPosition = myEnc.read()/4;
if (newPosition != oldPosition) {
if (newPosition > oldPosition) {
Joystick.button(1,1); //turn on button 1 on the joystick
delay(12);
Joystick.button(1,0); //turn off button 1 on the joystick
}
else if (newPosition < oldPosition) {
Joystick.button(2,1); //turn on button 1 on the joystick
delay(12);
Joystick.button(2,0); //turn off button 1 on the joystick
}
oldPosition = newPosition;
Serial.println(newPosition);
}
}