diff --git a/examples/pushRotate/pushRotate.ino b/examples/pushRotate/pushRotate.ino new file mode 100644 index 0000000..5fb94f0 --- /dev/null +++ b/examples/pushRotate/pushRotate.ino @@ -0,0 +1,95 @@ +#include +//#include "Keyboard.h" +//#include "Mouse.h" + +const int CLK = 4; // Definition der Pins. CLK an D6, DT an D5. +const int DT = 3; +const int SW = 2; // Der Switch wird mit Pin D2 Verbunden. ACHTUNG : Verwenden Sie einen interrupt-Pin! +long Position = 0; // Definition der "alten" Position (Diese fiktive alte Position wird benötigt, damit die aktuelle Position später im seriellen Monitor nur dann angezeigt wird, wenn wir den Rotary Head bewegen) +long PositionP = 0; // Definition der "alten" Position (Diese fiktive alte Position wird benötigt, damit die aktuelle Position später im seriellen Monitor nur dann angezeigt wird, wenn wir den Rotary Head bewegen) + +unsigned long _currentTime; +unsigned long _endrotateTime; + +// LED Outputs +#define ledCW 5 +#define ledCCW 6 +#define ledBUT 7 + +SimpleRotary rotary(4,3,2); // An dieser Stelle wird ein neues Encoder Projekt erstellt. Dabei wird die Verbindung über die zuvor definierten Varibalen (DT und CLK) hergestellt. + + +void setup() // Beginn des Setups + +{ + Serial.begin(9600); +} + +void loop() { + + _currentTime = millis(); + + byte i; + byte j; + + // 0 = not turning, 1 = CW, 2 = CCW + i = rotary.rotate(); + // 0 = not pushed, 1 = pushed, 2 = long pushed + j = rotary.pushType(10000); // number of milliseconds button has to be pushed for it to be considered a long push. + + if ( i == 1 && j != 4) { + Position = Position + 1; + Serial.print("CW "); + Serial.println(Position); + digitalWrite(ledCW, HIGH); + digitalWrite(ledCCW, LOW); + digitalWrite(ledBUT, LOW); + } + + if ( i == 2 && j != 4) { + Position = Position -1; + Serial.print("CCW "); + Serial.println(Position); + digitalWrite(ledCW, LOW); + digitalWrite(ledCCW, HIGH); + digitalWrite(ledBUT, LOW); + } + + + if ( i == 1 && j == 4) { + PositionP = PositionP + 1; + _endrotateTime = _currentTime; + rotary.resetPush(); + Serial.print("BCW "); + Serial.println(PositionP); + digitalWrite(ledCW, HIGH); + digitalWrite(ledCCW, HIGH); + digitalWrite(ledBUT, LOW); + } + + if ( i == 2 && j == 4) { + PositionP = PositionP -1; + _endrotateTime = _currentTime; + rotary.resetPush(); + Serial.print("BCCW "); + Serial.println(PositionP); + digitalWrite(ledCW, LOW); + digitalWrite(ledCCW, HIGH); + digitalWrite(ledBUT, HIGH); + } + + if ( j == 1 && _currentTime >= _endrotateTime + 1000) { + Serial.println("Pushed"); + digitalWrite(ledCW, LOW); + digitalWrite(ledCCW, LOW); + digitalWrite(ledBUT, HIGH); + + } + + if ( j == 2 && _currentTime >= _endrotateTime + 1000) { + Serial.println("Long Pushed"); + digitalWrite(ledCW, HIGH); + digitalWrite(ledCCW, HIGH); + digitalWrite(ledBUT, HIGH); + } +} diff --git a/src/SimpleRotary.cpp b/src/SimpleRotary.cpp index 960d508..f5cfae9 100644 --- a/src/SimpleRotary.cpp +++ b/src/SimpleRotary.cpp @@ -116,7 +116,7 @@ byte SimpleRotary::rotate() _statusB = ( digitalRead(_pinB) == _trigger ? true : false); if( !_statusA && _statusA_prev ){ - + if ( _statusB != _statusA ) { _dir = 0x01; } else { @@ -265,13 +265,17 @@ byte SimpleRotary::pushType(int i = 1000){ _updateTime(); _statusS = ( digitalRead(_pinS) == _trigger ) ? true : false; byte val = 0x00; + if ( !_statusS ) { + val = 0x04; + } if ( _currentTime >= _debounceSTime + _debounceSDelay ){ - + // Button has been pressed if ( !_statusS && _statusS_prev ) { _btnPressed = true; _pushTime = _currentTime; + val = 0x03; } // Button has been released @@ -286,6 +290,7 @@ byte SimpleRotary::pushType(int i = 1000){ val = 0x02; } } + _statusS_prev = _statusS; _debounceSTime = _currentTime;