I am currently using a 3 pin rotary encoder to set a time in a clock format (MM:SS). The rotary encoder works well both up and down, but when I go to convert the counter to a time it messes up when crossing a minute. When the counter gets 60 the clock should be 01:00, but I get 00:60, then going up one more I get 01:00 (should be 1:01), then it corrects itself and correctly reads 1:02 when you go up another. Also when going back down from 2:00 to 1:59 it will read 2:-1 instead of 1:59. I have this all connected to a screen right now displaying the actual count and the clock.
I think the error is in my timing code but I am honestly not sure. I am still new to Arduino and coding in general so I may be missing something obvious.
Code I have is below.
//Install "Adafruit_LiquidCrystal" Library
// To install go to tools in the ribbon > Manage Libraries > Search for "LiquidCrystal_PCF8574" in the box. Select the latest version and then install
// include the library code:
#include <LiquidCrystal_PCF8574.h>
#include <Wire.h>
LiquidCrystal_PCF8574 lcd(0x27);
// Connect the backpack on back of the screen to the Arduino
// * 5V to Arduino 5V pin
// * GND to Arduino GND pin
// * SCL to Analog #5 (Or SCL)
// * SDA to Analog #4 (Or SDA)
// Connect via i2c, default address #0 (A0-A2 not jumpered)
//Setup for rotary encoder
// Rotary Encoder Inputs
#define inputCLK 4
#define inputDT 5
int currentStateCLK;
int previousStateCLK;
// Steup the timer
float counter = 0;
int dispcount;
int min1 = 0;
int sec1 = 0;
char buffer[6];
void setup() {
//Setup Rotary Encoder
pinMode (inputCLK,INPUT);
pinMode (inputDT,INPUT);
digitalWrite(inputCLK, HIGH);
digitalWrite(inputDT, HIGH);
// Read the initial state of inputCLK
// Assign to previousStateCLK variable
previousStateCLK = digitalRead(inputCLK);
// Setup Screen
// set up the LCD's number of columns and rows. Big Screen is (20,4), little screen is (16,2).
lcd.begin(20, 4); // Put the columns and rows of the screen here
// Print a message to the LCD.
lcd.setCursor(0,0); // First row is always 0
lcd.print("Clock Count: ");
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0,2);
lcd.print("Actual Count: ");
}
void loop() {
lcd.setBacklight(255); // Set backlight for screen
// Read the current state of inputCLK for encoder
currentStateCLK = digitalRead(inputCLK); // Reads Encoder value
// Code for encoder
// If the previous and the current state of the inputCLK are different then a pulse has occured
if (currentStateCLK != previousStateCLK){
if (digitalRead(inputDT) != currentStateCLK) { // If the inputDT state is different than the inputCLK state then the encoder is rotating counterclockwise
counter = counter - 1; // Increment by half to account for slop
}
else {// Encoder is rotating clockwise
counter = counter + 1; // Increment by half to account for slop
}
dispcount = abs(counter);
// Begin Code for timing
if (sec1 == 60 && min1 >= 0) { // Resets seconds to 0 and increases minute when hitting 60s
min1 = min1 +1;
sec1 = 0; }
else if (sec1 < 0 && min1 >= 1){ // Resets seconds to 59 and decreses minute when hitting 0s
min1 = min1 -1;
sec1 = 59; }
else if (dispcount < 60){
sec1 = dispcount;
min1 = 0;}
else if (dispcount > 59){
sec1 = dispcount - (60*min1);}
else if (dispcount == 60){
sec1 = dispcount - (60*min1);}
else if (min1 > 10){
sec1 = 0;
min1 = 0;
dispcount = 0;
counter = 0; }
// End Code for Timing
}
previousStateCLK = currentStateCLK; //Update previousStateCLK with the current state
sprintf(buffer, "%02d:%02d", min1, sec1);
lcd.setCursor(0, 1); // Set Cursor to correct place
lcd.print(buffer); // Print Cycle ON time to screen
lcd.setCursor(0, 3); // Set Cursor to correct place
lcd.print(dispcount); // Print Cycle OFF time to screen
}