#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define ENCODER_CLK 2
#define ENCODER_DT 3
#define ENCODER_SW 4
#define RESET_BTN 5 // Assuming you're using a new button on pin 5
#define LED_PIN 13 // Assuming you're using the built-in LED on most Arduino boards
#define FEED_CLK 6 // Assuming you're using a new encoder for feedCounter with CLK on pin 6
#define FEED_DT 7 // Assuming you're using a new encoder for feedCounter with DT on pin 7
//#define FEED_SW 8 // Assuming you're using a new encoder for feedCounter with SW on pin 8
float counter = 0.0; // Use float for one decimal place precision
float feedCounter = 0.0; // Use float for one decimal place precision
bool rotationEnabled = false;
void setup() {
// put your setup code here, to run once:
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize encoder pins
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_SW, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING);
// Initialize reset button
pinMode(RESET_BTN, INPUT_PULLUP);
// Initialize LED
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // Initial state: LED off
// Initialize feedCounter encoder pins
pinMode(FEED_CLK, INPUT);
pinMode(FEED_DT, INPUT);
//pinMode(FEED_SW, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(FEED_CLK), readFeedEncoder, FALLING);
}
void readEncoder() {
if (rotationEnabled) {
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH && counter < 999.9) {
counter += 0.1; // Clockwise
}
if (dtValue == LOW && counter > 0.0) {
counter -= 0.1; // Counterclockwise
}
}
}
void readFeedEncoder() {
static int lastFeedCLKState = HIGH;
static int lastFeedDTState = HIGH;
int feedCLKState = digitalRead(FEED_CLK);
int feedDTState = digitalRead(FEED_DT);
if (feedCLKState != lastFeedCLKState) {
if (feedDTState != feedCLKState) {
// Clockwise rotation
if (feedCounter < 999.9) {
feedCounter += 0.1;
}
} else {
// Counterclockwise rotation
if (feedCounter > 0.0) {
feedCounter -= 0.1;
}
}
}
lastFeedCLKState = feedCLKState;
}
// Get the counter value, disabling interrupts.
// This makes sure readEncoder() or readFeedEncoder() doesn't change the value
// while we're reading it.
float getCounter() {
float result;
noInterrupts();
result = counter;
interrupts();
return result;
}
float getFeedCounter() {
float result;
noInterrupts();
result = feedCounter;
interrupts();
return result;
}
void toggleRotation() {
rotationEnabled = !rotationEnabled;
// Update LED state based on rotationEnabled (only for counter)
digitalWrite(LED_PIN, (rotationEnabled && !rotationEnabledForFeed()) ? HIGH : LOW);
}
bool rotationEnabledForFeed() {
// You can add additional conditions if needed
return false;
}
void resetCounters() {
counter = 0.0;
feedCounter = 0.0;
}
void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0, 0);
lcd.print("Amount:");
lcd.setCursor(8, 0);
lcd.print(constrain(getCounter(), 0.0, 999.9), 1); // Constrain the counter value to 0.0-999.9 with 1 decimal place
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Feed:");
lcd.setCursor(6, 1);
lcd.print(constrain(getFeedCounter(), 0.0, 999.9), 1); // Constrain the feedCounter value to 0.0-999.9 with 1 decimal place
lcd.print(" ");
// Display counters on Serial Monitor
Serial.println("Counter: " + String(getCounter(), 1) + " Feed Counter: " + String(getFeedCounter(), 1));
if (digitalRead(ENCODER_SW) == LOW) {
delay(50); // Debounce
toggleRotation();
while (digitalRead(ENCODER_SW) == LOW) {
delay(10); // Wait for button release
}
}
// Check the reset button
if (digitalRead(RESET_BTN) == LOW) {
delay(50); // Debounce
resetCounters();
while (digitalRead(RESET_BTN) == LOW) {
delay(10); // Wait for button release
}
}
}