#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
//#define USE_PARALLEL_2004_LCD // Is default
//#define USE_PARALLEL_1602_LCD
#define USE_SERIAL_2004_LCD
//#define USE_SERIAL_1602_LCD
#include "LCDBigNumbers.hpp" // Include sources for LCD big number generation
#if defined(USE_PARALLEL_LCD)
LiquidCrystal myLCD(2, 3, 4, 5, 6, 7); // Depends on your actual connections
//LiquidCrystal myLCD(4, 5, 6, 7, 8, 9); // Sample connections starting at pin 4
#else
#define LCD_I2C_ADDRESS 0x27 // Default LCD address is 0x27 for a 20 chars and 4 line / 2004 display
LiquidCrystal_I2C LCD(LCD_I2C_ADDRESS, LCD_COLUMNS, LCD_ROWS); // LCD_COLUMNS and LCD_ROWS are set by LCDBigNumbers.hpp depending on the defined display
#endif
/*
* Available big number fonts are: BIG_NUMBERS_FONT_1_COLUMN_2_ROWS_VARIANT_1, BIG_NUMBERS_FONT_2_COLUMN_2_ROWS_VARIANT_1,
* BIG_NUMBERS_FONT_3_COLUMN_2_ROWS_VARIANT_1, BIG_NUMBERS_FONT_3_COLUMN_2_ROWS_VARIANT_2, BIG_NUMBERS_FONT_3_COLUMN_2_ROWS_VARIANT_3,
* BIG_NUMBERS_FONT_2_COLUMN_3_ROWS_VARIANT_1, BIG_NUMBERS_FONT_2_COLUMN_3_ROWS_VARIANT_2, ,
* BIG_NUMBERS_FONT_3_COLUMN_4_ROWS_VARIANT_1, BIG_NUMBERS_FONT_3_COLUMN_4_ROWS_VARIANT_2
*/
LCDBigNumbers myLCD(&LCD, BIG_NUMBERS_FONT_2_COLUMN_3_ROWS_VARIANT_2); // Use 3x3 numbers
const int encoderPinA = 2;
const int encoderPinB = 3;
volatile int encoderValue = 0;
float encoderPPM = 1000.0;
float screwPitch = 0.5;
const int eepromAddress = 0;
int lastSavedValue = 0;
void setup() {
LCD.init();
LCD.clear();
LCD.backlight();
Serial.begin(115200);
pinMode(encoderPinA, INPUT);
pinMode(encoderPinB, INPUT);
attachInterrupt(digitalPinToInterrupt(encoderPinA), updateEncoder, CHANGE);
//myLCD.begin();
// Load the last saved encoder value from EEPROM
encoderValue = EEPROM.read(eepromAddress);
}
void loop() {
long position = 987650; // Set directly for testing
Serial.print("Raw position value: ");
Serial.println(position); // Should output 987650
char tString[7]; // Correct size
snprintf_P(tString, sizeof(tString), PSTR("%ld"), position); // Use %ld for long
Serial.print("Formatted tString: ");
Serial.println(tString); // Check the formatted string
myLCD.setBigNumberCursor(0);
myLCD.print(tString); // Print to the LCD
delay(1000); // Longer delay for easier observation
}
void updateEncoder() {
int stateA = digitalRead(encoderPinA);
int stateB = digitalRead(encoderPinB);
if (stateA == stateB) {
encoderValue++;
} else {
encoderValue--;
}
}
void displayFormattedNumber(float number) {
int integerPart = (int)number;
int decimalPart = (int)((number - integerPart) * 10);
//myLCD.print(integerPart);
//Serial.println((int)number);
myLCD.setBigNumberCursor(0);
if (integerPart /1000 < 1000)
myLCD.print('0');
else{
myLCD.print(integerPart / 1000);}
Serial.println((integerPart / 1000));
myLCD.setBigNumberCursor(5);
if (integerPart /100 < 100)
myLCD.print('0');
else{
myLCD.print(integerPart / 100);}
Serial.println((integerPart / 100) % 10);
myLCD.setBigNumberCursor(9);
if (integerPart /10 < 10)
myLCD.print('0');
else{
myLCD.print(integerPart / 10);}
Serial.println((integerPart / 10) % 10);
myLCD.setBigNumberCursor(13);
if (integerPart %10 < 10)
myLCD.print('0');
else{
myLCD.print(integerPart %10);}
Serial.println(integerPart % 10);
printColon(13, 1);
myLCD.setBigNumberCursor(17);
myLCD.print(decimalPart);
Serial.println(decimalPart);
delay(10);
}
void printColon(byte leftAdjust, byte topAdjust) {
for (byte row = 0; row < (4 - topAdjust); row++) {
myLCD.setBigNumberCursor(15, 4);
if (topAdjust == 1) {
if (row == 0 || row == 1) myLCD.print(F(" "));
else myLCD.print(F(" "));
} else {
if (row == 1 || row == 2) myLCD.print(F("."));
else myLCD.print(F(" "));
}
}
}