Hi,
my first post so go easy on me, i have managed to get this code working after days and nights of research, merging code from old projects and tutorials online.
I would really appreciate some help.
The thing i need to change or would like to change is the toggle from MM to Inch without have to hold the monentary button down. remember button state ? i have looked at the examples and others code but cannot seem to get this working, hear is the code i have so far but have to hold the toggle button.
this is running on Arduino Mega
thanks in advance
//in progress V1.0
#include <Encoder.h>// Encoder library.
#include <LiquidCrystal_I2C.h> //IC2 LCD library.
LiquidCrystal_I2C lcd(0x20, 4, 5, 6, 0, 1, 2, 3, 7, NEGATIVE); // Set the LCD I2C address
Encoder Rotary_1(2,3);// Connected to Digital pins 2 and 3.
Encoder Rotary_2(19,18);// Connected to Digital pins 19 and 18.
//Setup Buttons
#define reset_1 8
#define reset_2 6
#define inch_button 7
float tmpz = 0;
float tmpx = 0;
void setup() {
lcd.begin(16, 2);
pinMode (reset_1, INPUT_PULLUP);
pinMode (reset_2, INPUT_PULLUP);
pinMode (inch_button, INPUT_PULLUP);
}
long oldPosition_1 = -999;
long oldPosition_2 = -999;
void loop() {
const float inch = 25.4; // divide MM Reading with 25.4 to get inch reading
const float Wv_z = (0.159155 * PI) / 24; // Math formula 24 pulse/revolution encoder on z axis
const float Wv_x = (0.159155 * PI) / 24; // Math formula 24 pulse/revolution encoder on x axis
if (digitalRead(inch_button) == LOW) {
long newPosition_1 = Rotary_1.read(); //Read Rotary_1 current position
lcd.setCursor(0, 0);
lcd.print("Z");
lcd.setCursor(2,0);
tmpz = Wv_z * newPosition_1 / inch; //Math formula, Encoder Current position / 25.4, 4 decimals z axis
lcd.print(tmpz, 5); //Print Distance Travelled in inches with 5 decimals
lcd.print(" inch");
}
else {
lcd.setCursor(0, 0);
lcd.print("Z");
long newPosition_1 = Rotary_1.read(); //Read Rotary_1 current position
if (newPosition_1 != oldPosition_1) //If newposition is not equal to oldposition
lcd.setCursor(2, 0);
lcd.print(Wv_z * newPosition_1, 5); //Math formula * Encoder reading ,5 decimals z axis
lcd.print(" mm");
{
lcd.print(" ");
}
}
if (digitalRead(inch_button) == LOW) { //Check to see if inch button is pressed
long newPosition_2 = Rotary_2.read(); //Read Rotary_2 current position
lcd.setCursor(0, 1);
lcd.print("X");
lcd.setCursor(2,1);
tmpx = Wv_x * newPosition_2 / inch; //Math formula, Encoder Current position / 25.4, 4 decimals x axis
lcd.print(tmpx, 5); //Print Distance Travelled in inches with 5 decimals
lcd.print(" inch");
}
else {
lcd.setCursor(0, 1);
lcd.print("X");
long newPosition_2 = Rotary_2.read(); //Read Rotary_2 current position
if (newPosition_2 != oldPosition_2) //If newposition is not equal to oldposition
lcd.setCursor(2, 1);
lcd.print(Wv_x * newPosition_2, 5); //Math formula * Encoder reading ,5 decimals z axis
lcd.print(" mm");
{
lcd.print(" ");
}
}
//Reset Z Axis
if(digitalRead(reset_1)== LOW) //Check to see if Z is to be reset
{
Rotary_1.write(0); //Reset Rotary_1 to 0
}
//Reset X Axis
if(digitalRead(reset_2)== LOW) //Check to see if X is to be reset
{
Rotary_2.write(0); //Reset Rotary_2 to 0
}
}