// -----
// LimitedRotator.ino - Example for the RotaryEncoder library.
// This class is implemented for use with the Arduino environment.
//
// Copyright (c) by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a BSD 3-Clause License. See http://www.mathertel.de/License.aspx
// More information on: http://www.mathertel.de/Arduino
// -----
// 26.03.2017 created by Matthias Hertel
// 06.02.2021 conditions and settings added for ESP8266
// -----
// This example checks the state of the rotary encoder in the loop() function.
// The current position is printed on output when changed.
// In addition to the SimplePollRotator example here the range of the rotator is limited to the range 0 - 16 and only incremental steps of 2 are realized.
// To implement this limit the boundaries are checked and eventually the current position is adjusted.
// The internal (physical) position of the rotary encoder library remains by stepping with the increment 1
// so the the logical position is calculated by applying the ROTARYSTEPS factor.
// Hardware setup:
// Attach a rotary encoder with output pins to A2 and A3.
// The common contact should be attached to ground.
#include <Arduino.h>
#include <RotaryEncoder.h>
#define PIN_IN1 12
#define PIN_IN2 13
int ROTARYSTEPS = 1;
#define ROTARYMIN 0
#define ROTARYMAX 160
// 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;
// millis 1000 = 1 Sekunden
unsigned long eventTime_1 = 5000; //
unsigned long eventTime_2 = 500; //
unsigned long eventTime_3 = 1000; //
unsigned long previousTime_1 = 0; //
unsigned long previousTime_2 = 0; //
unsigned long previousTime_3 = 0; //
int ROTARY_count = 0;
int Rotary_dir_current = 0;
int a = 0;
void setup()
{
Serial.begin(115200);
while (! Serial);
Serial.println("LimitedRotator example for the RotaryEncoder library.");
//encoder.setPosition(10 / ROTARYSTEPS); // start with the value of 10.
} // setup()
// Read the current position of the encoder and print out when changed.
void loop()
{
encoder.tick();
int Rotary_dir = (int)(encoder.getDirection());
// get the current physical position and calc the logical position
int newPos = encoder.getPosition() * ROTARYSTEPS;
int RPM = encoder.getRPM();
// millis
unsigned long currentTime = millis();
////////////////
// Rotary dir change
if ( Rotary_dir == 1 || Rotary_dir == -1 ) {
previousTime_1 = currentTime;
if ( Rotary_dir == 1 && Rotary_dir_current == -1 || Rotary_dir == -1 && Rotary_dir_current == 1 ) {
ROTARY_count = 0;
}
if ( Rotary_dir == 1 ) {
Rotary_dir_current = 1;
ROTARY_count ++;
}
else {
Rotary_dir_current = -1;
ROTARY_count ++;
}
}
if (RPM > 15) { // 50
if (Rotary_dir == 1 ) { encoder.setPosition(lastPos + 10); }
if (Rotary_dir == -1 ) { encoder.setPosition(lastPos - 10); }
}
////////////////
if (newPos < ROTARYMIN) {
encoder.setPosition(ROTARYMIN / ROTARYSTEPS);
newPos = ROTARYMIN;
} else if (newPos > ROTARYMAX) {
encoder.setPosition(ROTARYMAX / ROTARYSTEPS);
newPos = ROTARYMAX;
} // if
//while ( Rotary_dir_current == 1 ) {
if (lastPos != newPos ) {
//if (( currentTime - previousTime_2 ) >= eventTime_2 ) {
Serial.print(newPos);
Serial.print(" Count: ");
Serial.print(ROTARY_count);
Serial.print(" dir: ");
Serial.print(Rotary_dir);
Serial.print(" dir2: ");
Serial.print(Rotary_dir_current);
Serial.print(" rpm:");
Serial.println(RPM);
lastPos = newPos;
previousTime_2 = currentTime;
} // if
} // loop ()
// The End