Kann man den CODE von RotaryEncoder Lib nutzen?
Und wenn ja was muss ich da tun?
// +++ Mega: Haupt TAB +++ //
#include <Arduino.h>
#include <RotaryEncoder.h>
#include <Wire.h>
int PIN_IN1 = 22; //Gelb
int PIN_IN2 = 23; //Weiß
int ROTARYSTEPS = 3;
int ROTARYMIN = -3;
int ROTARYMAX = 127;
byte midiKanal = 0;
byte controllerNummer = 0;
int controllerWert;
// Setup a RotaryEncoder with 4 steps per latch for the 2 signal input pins:
// RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::FOUR3);
// Setup a RotaryEncoder with 2 steps per latch for the 2 signal input pins:
RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);
// Last known rotary position.
int lastPos = -1;
// This interrupt routine will be called on any change of one of the input signals
void checkPosition(){
encoder.tick(); // just call tick() to check the state.
}
void setup(){
Serial.begin(115200);
Wire.begin();
Wire.setClock(400000L);
while (! Serial);
Serial.println("LimitedRotator example for the RotaryEncoder library.");
encoder.setPosition(10 / ROTARYSTEPS); // start with the value of 10.
attachInterrupt(PIN_IN1, checkPosition, CHANGE);
attachInterrupt(PIN_IN2, checkPosition, CHANGE);
}
// Read the current position of the encoder and print out when changed.
void loop(){
rotaryEncoder();
}
// +++ Mega: rotaryEncoder TAB +++ //
void rotaryEncoder(){
encoder.tick();
// get the current physical position and calc the logical position
int newPos = encoder.getPosition() * ROTARYSTEPS;
if (newPos < ROTARYMIN) {
encoder.setPosition(ROTARYMIN / ROTARYSTEPS);
newPos = ROTARYMIN;
} else if (newPos > ROTARYMAX) {
encoder.setPosition(ROTARYMAX / ROTARYSTEPS);
newPos = ROTARYMAX;
}
if (lastPos != newPos) {
Serial.print(newPos);
Serial.println();
lastPos = newPos;
controllerWert = newPos;
sendI2C(midiKanal, controllerNummer, controllerWert);
}
}
// +++ Mega: I2C TAB +++ //
byte i2cStatus = 0;
void sendI2C(byte command, byte firstByte, byte secondByte) {
Wire.beginTransmission(8);
byte i2cBuffer[3] = {command, firstByte, secondByte};
Wire.write(i2cBuffer, 3);
i2cStatus = Wire.endTransmission();
}