Here is a test program I wrote a while back to adjust date & time of a DS3231 RTC using the same rotary encoder you have. You should be able to cut out the bit(s) you need.
The main part is readEncoder that returns -1,0,1 depending of encoder direction. The other interesting bit is checkButton that returns button press state.
#include <Wire.h>
#include <RealTimeClockDS3231.h>
//Enumerations
const int eHours = 0;
const int eMinutes = 1;
const int eDay = 2;
const int eMonth = 3;
const int eYear = 4;
const int eDOW = 5;
//Constants
const int encButton = 5; //Rotary Encoder button pin (11)
const int encoder0PinA = 6; //Rotary Encoder A pin (12)
const int encoder0PinB = 7; //Rotary Encoder B pin (13)
const long debounceDelay = 90; //Button debounce time
const long adjustDelay = 10000; //10 Second Adjust timeout
//Globals
char formatted[] = "0000-00-00 00:00:00x";
void setup() {
// Wire.begin();
pinMode (encoder0PinA,INPUT);
digitalWrite(encoder0PinA,HIGH); //Enable pullup resistor
pinMode (encoder0PinB,INPUT);
digitalWrite(encoder0PinB,HIGH); //Enable pullup resistor
pinMode (encButton,INPUT); //Encoder button is set to input
Serial.begin(9600);
}
void loop() {
long loopTime = millis(); //Read timer
int mainDelay = 1000; // Default main loop delay
while ((millis() - loopTime) < mainDelay) {
if (checkButton(encButton)){
while (checkButton(encButton)){ //Wait for button to be released
};
delay(100);
digitalWrite(13, HIGH); // set the LED on
//Serial.println("Button On");
eAdjust();
//Serial.println("Button Off");
digitalWrite(13, LOW); // set the LED on
}
}
RTC.readClock(); //Read the current date and time
RTC.getFormatted2k(formatted);
Serial.println(formatted);
}
// Called from main loop when encoder button pressed
void eAdjust(){
RTC.readClock(); //Read the current date and time
int value = readEncoder(); //Init encoder readback value and prime encoder sub (result discarded)
int eMode = eHours; //Set mode to eHours
doDisplay(eMode); //Display Hours
long eTime = millis(); //Get current CPU time for adjust loop timeout
while ((millis() - eTime) < adjustDelay) {
if (checkButton(encButton)){ //Is encoder button pressed?
// Mode button pressed
//Serial.println("Button Pressed");
while (checkButton(encButton)){ //Wait for button to be released
};
delay(100); //Slight delay to allow for button debounce
//Serial.println("Button Released");
eMode++; //Increment mode
if (eMode > eDOW){ //End of modes?
// Mode cycled past end
RTC.setSeconds(0); //Zero seconds
RTC.setClock(); //Set the clock
//Serial.println("Mode Exit");
return; //Return
}
RTC.getFormatted2k(formatted);
Serial.println(formatted);
doDisplay(eMode); //Display the new mode
eTime = millis(); //Reset adjust loop timout
}
if (millis() % 10 == 0){ //Only read every 10mS
int result = readEncoder(); //Read encoder. Returns -1,0,1
//Serial.print("Encoder: ");
if (!result == 0){ //Encoder <> 0
//Encoder wheel was turned
//Serial.print("Encoder Turned");
switch (eMode){
case eHours: //Mode eHours
value = RTC.getHours() + result; //Read hours and add encoder direction
RTC.setHours(value % 24); //Store new hours after legalizing
break; //No further processing
case eMinutes:
value = RTC.getMinutes() + result;
RTC.setMinutes(value % 60);
break;
case eDay:
value = RTC.getDate() + result;
RTC.setDate(value % 31);
break;
case eMonth:
value = RTC.getMonth() + result;
RTC.setMonth(value % 12);
break;
case eYear:
value = RTC.getYear() + result;
RTC.setYear(value % 99);
break;
case eDOW:
value = RTC.getDayOfWeek() + result;
RTC.setDayOfWeek(value % 7);
break;
default: //What to do if mode unknown
break; //Nothing
}
doDisplay(eMode); //Display new mode
eTime = millis(); //Reset adjust loop timout
}
}
}
//Timed out
//RTC.setClock();
Serial.println("Timeout");
}
void doDisplay(int Mode){ //Display relevent data for given mode
Serial.print("Mode ");
Serial.print(Mode);
Serial.print(": ");
switch (Mode){
case eHours:
Serial.println(RTC.getHours());
break;
case eMinutes:
Serial.println(RTC.getMinutes());
break;
case eDay:
Serial.println(RTC.getDate());
break;
case eMonth:
Serial.println(RTC.getMonth());
break;
case eYear:
Serial.println(RTC.getYear());
break;
case eDOW:
Serial.println(RTC.getDayOfWeek());
break;
default:
break;
}
}
// Button = pin number to read
int checkButton(int Button) {
int buttonState = digitalRead(Button); // Read button
if (buttonState == HIGH) { // If button pressed then wait a bit to allow for debounce
long Time = millis();
while ((millis() - Time) < debounceDelay) {
}
buttonState = digitalRead(Button); // Read button again
return buttonState; // Return button state
}
else { //Button not pressed so no need to wait for bebounce
return LOW;
}
}
int readEncoder() {
static int encoder0PinALast = LOW;
int eDir = 0;
int n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
eDir = -1;
//Serial.print("-1");
}
else {
eDir = 1;
//Serial.print("1");
}
}
encoder0PinALast = n;
//Serial.println(eDir);
return eDir;
}