@@ -46,14 +46,16 @@ RotaryEncoder::RotaryEncoder(int pin1, int pin2, LatchMode mode)
4646 _mode = mode;
4747
4848 // Setup the input pins and turn on pullup resistor
49- pinMode (pin1, INPUT_PULLUP);
50- pinMode (pin2, INPUT_PULLUP);
51-
52- // when not started in motion, the current state of the encoder should be 3
53- int sig1 = digitalRead (_pin1);
54- int sig2 = digitalRead (_pin2);
49+ if ((pin1 >= 0 ) && (pin2 >= 0 )) {
50+ pinMode (pin1, INPUT_PULLUP);
51+ pinMode (pin2, INPUT_PULLUP);
52+ // when not started in motion, the current state of the encoder should be 3
53+ int sig1 = digitalRead (_pin1);
54+ int sig2 = digitalRead (_pin2);
55+ }
56+
5557 _oldState = sig1 | (sig2 << 1 );
56-
58+
5759 // start with position 0;
5860 _position = 0 ;
5961 _positionExt = 0 ;
@@ -108,11 +110,18 @@ void RotaryEncoder::setPosition(long newPosition)
108110} // setPosition()
109111
110112
113+ // Slow, but Simple Variant by directly Read-Out of the Digital State within loop-call
111114void RotaryEncoder::tick (void )
112115{
113116 unsigned long now = millis ();
114117 int sig1 = digitalRead (_pin1);
115118 int sig2 = digitalRead (_pin2);
119+ tick (sig1, sig2);
120+ } // tick()
121+
122+ // When a faster method than digitalRead is available you can _tick with the 2 values directly.
123+ void RotaryEncoder::tick (int sig1, int sig2)
124+ {
116125 int8_t thisState = sig1 | (sig2 << 1 );
117126
118127 if (_oldState != thisState) {
0 commit comments