am so happy but and so disappoint. i found this code and i modify it, for 2 encoder.
my big big problem is how i will adopt this to my main sketch, so it will send midi notes and not just.. count... my main sketch has the following i assume for readding the encoder and send the midi notes
//// read encoder
void readEncoder () {
int newPosition = myEnc.read();
int encoderVal = map(newPosition, -1024, 1024, -256, 256);
int encoderValue;
if (encoderVal != oldPosition) {
if ((encoderVal - oldPosition) > 0) {
encoderValue = 127;
}
else {
encoderValue = 1;
}
MIDI.sendControlChange(14, encoderValue, 1);
oldPosition = encoderVal;
}
}
THIS IS MY ENCODER AND IS WORKING SUPER FINE FOR WHAT I NEED IT FOR
Direction: CCW- ENCODER A | Counter: 0
Direction: CCW- ENCODER A | Counter: -1
Direction: CCW- ENCODER A | Counter: -2
Direction: CW- ENCODER A | Counter: -1
Direction: CW- ENCODER A | Counter: 0
Direction: CW- ENCODER A | Counter: 1
Direction: CCW -- ENCODER B | Counter: 0
Direction: CCW -- ENCODER B | Counter: -1
Direction: CCW -- ENCODER B | Counter: -2
Direction: CW -- ENCODER B | Counter: -1
Direction: CW -- ENCODER B | Counter: 0
Direction: CW -- ENCODER B | Counter: 1
I NEED TO ADOPT THIS SOME HOW TO MY MAIN SKETCH BUT NOT LIKE A COUNTER BUT LIKE MIDI NOTE SENDER..
// Rotary Encoder Inputs
#define CLK A2 // ENCODER PIN A
#define DT A3 // ENCODER PIN B
#define CLK1 A4 // ENCODER(B) PIN A
#define DT1 A5 // ENCODER (B) PIN B
#define SW 8 //ENCODER SWITCH
#define SW1 9 // ENCODER (B) SWITCH
int counter = 0;
int counter1 = 0;
int currentStateCLK;
int currentStateCLK1;
int lastStateCLK;
int lastStateCLK1;
String currentDir ="";
unsigned long lastButtonPress = 0;
unsigned long lastButtonPress1 = 0;
void setup() {
// Set encoder pins as inputs
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW, INPUT_PULLUP);
pinMode(CLK1,INPUT);
pinMode(DT1,INPUT);
pinMode(SW1, INPUT_PULLUP);
// Setup Serial Monitor
Serial.begin(9600);
// Read the initial state of CLK
lastStateCLK = digitalRead(CLK);
lastStateCLK1 = digitalRead(CLK1);
}
// ------------------------------------------------------------------------------------
void loop() {
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
// If last and current state of CLK are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK) {
counter --;
currentDir ="CCW- ENCODER A";
} else {
// Encoder is rotating CW so increment
counter ++;
currentDir ="CW- ENCODER A";
}
Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
}
// Remember last CLK state
lastStateCLK = currentStateCLK;
// ------------------------------------------------------------------------------
// Read the current state of CLK
currentStateCLK1 = digitalRead(CLK1);
// If last and current state of CLK are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateCLK1 != lastStateCLK1 && currentStateCLK1 == 1){
// If the DT1 state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK1) {
counter --;
currentDir ="CCW-ENCODER B";
} else {
// Encoder is rotating CW so increment
counter ++;
currentDir ="CW-ENCODER B";
}
Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
}
// Remember last CLK state
lastStateCLK1 = currentStateCLK1;
// ------------------------------------------------------------------------------------
// Read the button state
int btnState = digitalRead(SW);
int btnState1 = digitalRead(SW1);
//If we detect LOW signal, button is pressed
if (btnState == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
Serial.println("Button pressed!");
}
// Remember last button press event
lastButtonPress = millis();
}
//If we detect LOW signal, button is pressed
if (btnState1 == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
Serial.println("Button pressed!");
}
// Remember last button press event
lastButtonPress = millis();
}
// Put in a slight delay to help debounce the reading
delay(1);
}