Hi!
So I'm building a Digital Read Out for my lathe using two 1024 p/r rotary encoders. The values are sent to 2 LCDs. There's a switch thas switch the values from inches to mm. So far so good the peices of code I've put together works well but I want more! I'm trying to get the 3 readouts per LCD to be zeroed by pressing their respective button. I've searched for a couple days now but couldn't find any hint of a way to do that. As I'm fairly new to coding I don't know exactly what everything is in this code and what everything does but it does work well so far.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd1(0x27, 20, 4); // << Address 1
LiquidCrystal_I2C lcd2(0x26, 20, 4); // << Address 2
#include <Encoder.h>
Encoder myEnc1(1, 2);
Encoder myEnc2(3, 4);
const int unitSwitch = 5;
const int zeroSwitch1A = 6;
const int zeroSwitch1B = 7;
const int zeroSwitch1C = 8;
const int zeroSwitch2A = 9;
const int zeroSwitch2B = 10;
const int zeroSwitch2C = 11;
void setup() {
pinMode( unitSwitch, INPUT_PULLUP );
pinMode( zeroSwitch1A, INPUT_PULLUP );
pinMode( zeroSwitch1B, INPUT_PULLUP );
pinMode( zeroSwitch1C, INPUT_PULLUP );
pinMode( zeroSwitch2A, INPUT_PULLUP );
pinMode( zeroSwitch2B, INPUT_PULLUP );
pinMode( zeroSwitch2C, INPUT_PULLUP );
Serial.begin( 9600 );
lcd1.init();
lcd2.init();
lcd1.backlight();
lcd2.backlight();
}
long oldPosition = -999;
void loop() {
const float Wv = (.1 * PI) / 1024; // Math formula for .37" diameter pulley
//and 1024 pulse/revolution encoder.
int unitSwitch1;
int zeroSwitch1A;
int zeroSwitch1B;
int zeroSwitch1C;
int zeroSwitch2A;
int zeroSwitch2B;
int zeroSwitch2C;
Serial.println (zeroSwitch1A);
unitSwitch1 = digitalRead( unitSwitch );
// Serial.println( unitSwitch1 );
long newPosition1 = myEnc1.read();
if (newPosition1 != oldPosition)
if (unitSwitch1== HIGH) {
lcd1.setCursor(0, 0); // (Column 7,ROW 0) (Column #0-19,Row #0-3)
lcd1.print("X AXIS");
lcd1.setCursor(9, 0);
lcd1.print("-INCH-");
lcd1.setCursor(16, 0);
lcd1.setCursor(0,1);
lcd1.print("A");
lcd1.setCursor(9, 1);
lcd1.print(Wv * newPosition1, 3);//Math formula * Encoder reading ,3 decimals
lcd1.setCursor(0, 2);
lcd1.print("B");
lcd1.setCursor(9, 2);
lcd1.print(Wv * newPosition1, 3);
lcd1.setCursor(0, 3);
lcd1.print("C");
lcd1.setCursor(9, 3);
lcd1.print(Wv * newPosition1, 3);
long newPosition2 = myEnc2.read();
if (newPosition2 != oldPosition)
lcd2.setCursor(0, 0);
lcd2.print("Y AXIS -INCH-");
lcd2.setCursor(0,1);
lcd2.print("A");
lcd2.setCursor(9, 1);
lcd2.print(Wv * newPosition2, 3);
lcd2.setCursor(0, 2);
lcd2.print("B");
lcd2.setCursor(9, 2);
lcd2.print(Wv * newPosition2, 3);
lcd2.setCursor(0, 3);
lcd2.print("C");
lcd2.setCursor(9, 3);
lcd2.print(Wv * newPosition2, 3);
}
else {
long newPosition1 = myEnc1.read();
if (newPosition1 != oldPosition)
lcd1.setCursor(0, 0); // (Column 7,ROW 0) (Column #0-19,Row #0-3)
lcd1.print("X AXIS");
lcd1.setCursor(9, 0);
lcd1.print("--MM--");
lcd1.setCursor(16, 0);
lcd1.setCursor(0,1);
lcd1.print("A");
lcd1.setCursor(9, 1);
lcd1.print(Wv * newPosition1 * 25.4, 3);//Math formula * Encoder reading ,3 decimals
lcd1.setCursor(0, 2);
lcd1.print("B");
lcd1.setCursor(9, 2);
lcd1.print(Wv * newPosition1 * 25.4, 3);
lcd1.setCursor(0, 3);
lcd1.print("C");
lcd1.setCursor(9, 3);
lcd1.print(Wv * newPosition1 * 25.4, 3);
long newPosition2 = myEnc2.read();
if (newPosition2 != oldPosition)
lcd2.setCursor(0, 0);
lcd2.print("Y AXIS --MM--");
lcd2.setCursor(0,1);
lcd2.print("A");
lcd2.setCursor(9, 1);
lcd2.print(Wv * newPosition2 * 25.4, 3);
lcd2.setCursor(0, 2);
lcd2.print("B");
lcd2.setCursor(9, 2);
lcd2.print(Wv * newPosition2 * 25.4, 3);
lcd2.setCursor(0, 3);
lcd2.print("C");
lcd2.setCursor(9, 3);
lcd2.print(Wv * newPosition2 * 25.4, 3);
}
}
