#include <RotaryEncoder.h>
constexpr byte PIN_IN1 = A1;
constexpr byte PIN_IN2 = A0;
constexpr byte PIN_IN3 = A3;
constexpr byte PIN_IN4 = A2;
constexpr byte PIN_IN5 = A5;
constexpr byte PIN_IN6 = A4;
constexpr byte PIN_IN7 = 4;
constexpr byte PIN_IN8 = 5;
constexpr byte PIN_IN9 = 2;
constexpr byte PIN_IN10 = 3;
RotaryEncoder *encoder1 = nullptr;
RotaryEncoder *encoder2 = nullptr;
RotaryEncoder *encoder3 = nullptr;
RotaryEncoder *encoder4 = nullptr;
RotaryEncoder *encoder5 = nullptr;
// Tastaturmatrix
int reihe[] = {6, 7, 8, 9};
int spalte[] = {13, 12, 11, 10};
int col_scan;
int last_scan = -1;
void setup()
{
Serial.begin(9600);
for (int i = 0; i <= 3; i++)
{
//Initialisierung der PINs
pinMode(reihe[i], OUTPUT);
pinMode(spalte[i], INPUT);
digitalWrite(spalte[i], HIGH);
}
encoder1 = new RotaryEncoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::FOUR3);
encoder2 = new RotaryEncoder(PIN_IN3, PIN_IN4, RotaryEncoder::LatchMode::FOUR3);
encoder3 = new RotaryEncoder(PIN_IN5, PIN_IN6, RotaryEncoder::LatchMode::FOUR3);
encoder4 = new RotaryEncoder(PIN_IN7, PIN_IN8, RotaryEncoder::LatchMode::FOUR3);
encoder5 = new RotaryEncoder(PIN_IN9, PIN_IN10, RotaryEncoder::LatchMode::FOUR3);
}
int pos1 = 0;
int pos2 = 0;
int pos3 = 0;
int pos4 = 0;
int pos5 = 0;
int Reihe;
int Spalte;
void loop()
{
if (knopfGedrueckt(Reihe, Spalte)) {
Aktion(Reihe, Spalte);
}
encoder1->tick(); // just call tick() to check the state.
encoder2->tick();
encoder3->tick();
encoder4->tick();
encoder5->tick();
int newPos1 = encoder1->getPosition();
int newPos2 = encoder2->getPosition();
int newPos3 = encoder3->getPosition();
int newPos4 = encoder4->getPosition();
int newPos5 = encoder5->getPosition();
if (true) {
if (pos1 != newPos1) {
Serial.print("pos1:");
Serial.print(newPos1);
Serial.print(" dir1:");
Serial.println((int)(encoder1->getDirection()));
pos1 = newPos1;
}
if (pos2 != newPos2) {
Serial.print("pos2:");
Serial.print(newPos2);
Serial.print(" dir2:");
Serial.println((int)(encoder2->getDirection()));
pos2 = newPos2;
}
if (pos3 != newPos3) {
Serial.print("pos3:");
Serial.print(newPos3);
Serial.print(" dir3:");
Serial.println((int)(encoder3->getDirection()));
pos3 = newPos3;
}
if (pos4 != newPos4) {
Serial.print("pos4:");
Serial.print(newPos4);
Serial.print(" dir4:");
Serial.println((int)(encoder4->getDirection()));
pos4 = newPos4;
}
if (pos5 != newPos5) {
Serial.print("pos5:");
Serial.print(newPos5);
Serial.print(" dir5:");
Serial.println((int)(encoder5->getDirection()));
pos5 = newPos5;
}
}
}
boolean knopfGedrueckt(int &Reihe, int &Spalte) {
//Suche nach gedrücktem Knopf
static boolean tasteGedrueckt = false;
static unsigned long letzterDruck = 0;
if (tasteGedrueckt && millis()-letzterDruck < 300) { // Diese 300 ms verhindern, dass
// der Tastendruck "prellt" bzw.
// schnell mehrfach hintereinander
// ausgeführt wird
return false;
}
tasteGedrueckt = false;
for (int i = 0; i <= 3; i++)
{
if (tasteGedrueckt) break;
digitalWrite(reihe[0], HIGH);
digitalWrite(reihe[1], HIGH);
digitalWrite(reihe[2], HIGH);
digitalWrite(reihe[3], HIGH);
digitalWrite(reihe[i], LOW);
for (int j = 0; j <= 3; j++)
{
col_scan = digitalRead(spalte[j]);
if (col_scan == LOW)
{
letzterDruck = millis();
tasteGedrueckt = true;
Reihe = i;
Spalte = j;
break;
}
}
}
return tasteGedrueckt;
}
void Aktion(int i, int j)
{
int tasterNr = i*4+j+1;
Serial.print("Taster Nr. ");
Serial.print(tasterNr);
Serial.print("\t");
switch (tasterNr){
case 1 : // Hier die Aktion für Taster 1
Serial.println("Aktion für Taster 1");
break;
case 9 : // Hier die Aktion für Taster 9
Serial.println("Aktion für Taster 9");
break;
case 16 : // Hier die Aktion für Taster 16
Serial.println("Aktion für Taster 16");
break;
default: // und hier für alle eventuell nicht abgefangenen Tasternummern
Serial.println("Hier ist noch keine Aktion hinterlegt ....");
break;
}
}