I have a rotary encoder KY-040 connected to a Digispark like this:
CLK go to p0 and and DT go to p2, SW is disconnected for now (it's just a button).
I did everything like the instructions here: http://henrysbench.capnfatz.com/henrys-bench/keyes-ky-040-arduino-rotary-encoder-user-manual/
#include <DigiKeyboard.h>
int pinA = 0; // Connected to CLK on KY-040
int pinB = 2; // Connected to DT on KY-040
int encoderPosCount = 0;
int pinALast;
int aVal;
boolean bCW;
void setup() {
pinMode (pinA,INPUT);
pinMode (pinB,INPUT);
/* Read Pin A
Whatever state it's in will reflect the last position
*/
pinALast = digitalRead(pinA);
Serial.begin (9600);
}
void loop() {
aVal = digitalRead(pinA);
if (aVal != pinALast){ // Means the knob is rotating
// if the knob is rotating, we need to determine direction
// We do that by reading pin B.
if (digitalRead(pinB) != aVal) { // Means pin A Changed first - We're Rotating Clockwise
encoderPosCount ++;
bCW = true;
} else {// Otherwise B changed first and we're moving CCW
bCW = false;
encoderPosCount--;
}
Serial.println("Rotated: ");
if (bCW){
//Serial.println ("clockwise");
DigiKeyboard.sendKeyStroke(KEY_V); //or DigiKeyboard.write("1");
}else{
//Serial.println("counterclockwise");
DigiKeyboard.println("cc");
}
Serial.print("Encoder Position: ");
Serial.println(encoderPosCount);
}
pinALast = aVal;
}
I only changed the pin numbers in the code and the Serial.println (...
to any of the following:
DigiKeyboard.write("1");
DigiKeyboard.println("1");
DigiKeyboard.sendKeyStroke(KEY_V);
All of them didn't do anything when rotating the knob...
Any ideas? Maybe the 10K resistors are too much?