Sawmill Setworks

Here is the code with the moving=true in the function.
Serial.print ("MOVEUP");
Serial.println(moveForward);
Serial.print("MOVINGSTATE");
Serial.println(moving);

#include <Encoder.h>

enum PinAssignments {
  encoderPinA = 3,   // rigth
  encoderPinB = 2,   // left
};


// assume buttons are wired one side to ground, one side th arduino pin
const int buttonPinA = 4;
const int buttonPinB = 5;

const int relayPin = 7;

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(encoderPinA, encoderPinB);

int buttonStateA;
int lastButtonStateA;
int buttonStateB;
int lastButtonStateB;

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
  pinMode( buttonPinA, INPUT_PULLUP );
  pinMode( buttonPinB, INPUT_PULLUP );
  pinMode( relayPin, OUTPUT );
  digitalWrite( relayPin, HIGH );  // turn it off
}

long oldPosition  = -999;

long startPosition;
long endPosition;
bool moveForward;   // true if incrementing encoder, false is decrementing encoder
bool moving = false; // true when we are moving

const long INCHES_TO_COUNTS = 512;  // 512 pulses per inch

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
     Serial.print ("MOVEUP");
    Serial.println(moveForward);
    Serial.print("MOVINGSTATE");
    Serial.println(moving);
   
    
  }

  if ( moving ) {
    if ( moveForward == true ) {
      if ( endPosition >= startPosition ) {
        // all done moving
        digitalWrite( relayPin, HIGH );  // motor off
        Serial.println( "Forward move complete.");
        moving = false;
      }
    } else {
      if ( endPosition <= startPosition ) {
        // all done moving
        digitalWrite( relayPin, HIGH );  // motor off
        Serial.println( "Reverse move complete.");
        moving = false;
      }
    }
  }

  // check button A
  buttonStateA = digitalRead(buttonPinA);

  if (buttonStateA != lastButtonStateA) {
    lastButtonStateA = buttonStateA;
    // if the state has changed, increment the counter
    if (buttonStateA == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("Button A on");
      startMovement(1.0);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

  // check button B
  buttonStateB = digitalRead(buttonPinB);

  if (buttonStateB != lastButtonStateB) {
    lastButtonStateB = buttonStateB;
    // if the state has changed, increment the counter
    if (buttonStateB == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("Button B on");
      startMovement(1.5);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
}

void startMovement(float inches)
{
  startPosition = myEnc.read();
  endPosition = startPosition + inches * INCHES_TO_COUNTS;
  Serial.print( "Starting at " );
  Serial.print( startPosition );
  Serial.print( " and moving to " );
  Serial.println( endPosition );
  if ( endPosition < startPosition ) {
    moveForward = false;
  }
  else {
    moveForward = true;
  }
  digitalWrite( relayPin, LOW ); // start moving
  moving = true;
}
}

And here is my serial prints:

On Startup:
Basic Encoder Test:
0
MOVEUP0
MOVINGSTATE0

After button push:
Basic Encoder Test:
0
MOVEUP0
MOVINGSTATE0
Button A on
Starting at 0 and moving to 512
Forward move complete.

After moving a little:
MOVEUP1
MOVINGSTATE0
9
MOVEUP1
MOVINGSTATE0
10
MOVEUP1
MOVINGSTATE0

Once I go to 512:
511
MOVEUP1
MOVINGSTATE0
512
MOVEUP1
MOVINGSTATE0
513
MOVEUP1
MOVINGSTATE0
514
MOVEUP1
MOVINGSTATE0

.

Here is when I add a the serial print in the button
Basic Encoder Test:
0
MOVEUP0
MOVINGSTATE0
Button A on
Starting at 0 and moving to 512
MOVINGSTATE1 <------------------
Forward move complete.
1
MOVEUP1
MOVINGSTATE0

D'oh. My code was comparing endPosition to startPosition which, of course, always immediately ends the move. We need to be testing endPosition vs. newPosition.

#include <Encoder.h>

enum PinAssignments {
  encoderPinA = 3,   // rigth
  encoderPinB = 2,   // left
};


// assume buttons are wired one side to ground, one side th arduino pin
const int buttonPinA = 4;
const int buttonPinB = 5;

const int relayPin = 7;

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(encoderPinA, encoderPinB);

int buttonStateA;
int lastButtonStateA;
int buttonStateB;
int lastButtonStateB;

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
  pinMode( buttonPinA, INPUT_PULLUP );
  pinMode( buttonPinB, INPUT_PULLUP );
  pinMode( relayPin, OUTPUT );
  digitalWrite( relayPin, HIGH );  // turn it off
}

long oldPosition  = -999;

long startPosition;
long endPosition;
bool moveForward;   // true if incrementing encoder, false is decrementing encoder
bool moving = false; // true when we are moving

const long INCHES_TO_COUNTS = 512;  // 512 pulses per inch

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
    Serial.print ("MOVEUP");
    Serial.println(moveForward);
    Serial.print("MOVINGSTATE");
    Serial.println(moving);


  }

  if ( moving ) {
    if ( moveForward == true ) {
      if ( endPosition >= newPosition ) {
        // all done moving
        digitalWrite( relayPin, HIGH );  // motor off
        Serial.println( "Forward move complete.");
        moving = false;
      }
    } else {
      if ( endPosition <= newPosition ) {
        // all done moving
        digitalWrite( relayPin, HIGH );  // motor off
        Serial.println( "Reverse move complete.");
        moving = false;
      }
    }
  }

  // check button A
  buttonStateA = digitalRead(buttonPinA);

  if (buttonStateA != lastButtonStateA) {
    lastButtonStateA = buttonStateA;
    // if the state has changed, increment the counter
    if (buttonStateA == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("Button A on");
      startMovement(1.0);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

  // check button B
  buttonStateB = digitalRead(buttonPinB);

  if (buttonStateB != lastButtonStateB) {
    lastButtonStateB = buttonStateB;
    // if the state has changed, increment the counter
    if (buttonStateB == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("Button B on");
      startMovement(1.5);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
}

void startMovement(float inches)
{
  startPosition = myEnc.read();
  endPosition = startPosition + inches * INCHES_TO_COUNTS;
  Serial.print( "Starting at " );
  Serial.print( startPosition );
  Serial.print( " and moving to " );
  Serial.println( endPosition );
  if ( endPosition < startPosition ) {
    moveForward = false;
  }
  else {
    moveForward = true;
  }
  digitalWrite( relayPin, LOW ); // start moving
  moving = true;
}

Sorry for the blunder! And no, there are not good debugging tools like you are used to.

blh64:
D'oh. My code was comparing endPosition to startPosition which, of course, always immediately ends the move. We need to be testing endPosition vs. newPosition.

#include <Encoder.h>

enum PinAssignments {
  encoderPinA = 3,  // rigth
  encoderPinB = 2,  // left
};

// assume buttons are wired one side to ground, one side th arduino pin
const int buttonPinA = 4;
const int buttonPinB = 5;

const int relayPin = 7;

// Change these two numbers to the pins connected to your encoder.
//  Best Performance: both pins have interrupt capability
//  Good Performance: only the first pin has interrupt capability
//  Low Performance:  neither pin has interrupt capability
Encoder myEnc(encoderPinA, encoderPinB);

int buttonStateA;
int lastButtonStateA;
int buttonStateB;
int lastButtonStateB;

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
  pinMode( buttonPinA, INPUT_PULLUP );
  pinMode( buttonPinB, INPUT_PULLUP );
  pinMode( relayPin, OUTPUT );
  digitalWrite( relayPin, HIGH );  // turn it off
}

long oldPosition  = -999;

long startPosition;
long endPosition;
bool moveForward;  // true if incrementing encoder, false is decrementing encoder
bool moving = false; // true when we are moving

const long INCHES_TO_COUNTS = 512;  // 512 pulses per inch

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
    Serial.print ("MOVEUP");
    Serial.println(moveForward);
    Serial.print("MOVINGSTATE");
    Serial.println(moving);

}

if ( moving ) {
    if ( moveForward == true ) {
      if ( endPosition >= newPosition ) {
        // all done moving
        digitalWrite( relayPin, HIGH );  // motor off
        Serial.println( "Forward move complete.");
        moving = false;
      }
    } else {
      if ( endPosition <= newPosition ) {
        // all done moving
        digitalWrite( relayPin, HIGH );  // motor off
        Serial.println( "Reverse move complete.");
        moving = false;
      }
    }
  }

// check button A
  buttonStateA = digitalRead(buttonPinA);

if (buttonStateA != lastButtonStateA) {
    lastButtonStateA = buttonStateA;
    // if the state has changed, increment the counter
    if (buttonStateA == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("Button A on");
      startMovement(1.0);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

// check button B
  buttonStateB = digitalRead(buttonPinB);

if (buttonStateB != lastButtonStateB) {
    lastButtonStateB = buttonStateB;
    // if the state has changed, increment the counter
    if (buttonStateB == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("Button B on");
      startMovement(1.5);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
}

void startMovement(float inches)
{
  startPosition = myEnc.read();
  endPosition = startPosition + inches * INCHES_TO_COUNTS;
  Serial.print( "Starting at " );
  Serial.print( startPosition );
  Serial.print( " and moving to " );
  Serial.println( endPosition );
  if ( endPosition < startPosition ) {
    moveForward = false;
  }
  else {
    moveForward = true;
  }
  digitalWrite( relayPin, LOW ); // start moving
  moving = true;
}




Sorry for the blunder! And no, there are not good debugging tools like you are used to.

This doesnt keep the relay on either. For some reason it turns the relay off.

On Startup
Basic Encoder Test:
0
MOVEUP0
MOVINGSTATE0

on button push

Button A on
Starting at 0 and moving to 512
MOVINGSTATE1
Forward move complete.
MOVINGSTATE0

I am not sure why the change didnt work, do we have to tell the code to do while?

SO if ( endPosition >= newPosition )
do digitalWrite( relayPin, LOW );
while ( endPosition >= newPosition )

else
digitalWrite( relayPin, HIGH);
???

I am not a lost for how to say do this wait until x is met.

The comparison was backwards. Moving forward, newPosition = 0 and endPosition = 512 so the condition for stopping the move would be newPosition >= endPosition.

You definitely do NOT want to use a do...while() loop or any other type of loop. That is what the loop() function is already doing. If you do, your buttons will not be responsive.

#include <Encoder.h>

enum PinAssignments {
  encoderPinA = 3,   // rigth
  encoderPinB = 2,   // left
};


// assume buttons are wired one side to ground, one side th arduino pin
const int buttonPinA = 4;
const int buttonPinB = 5;

const int relayPin = 7;

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(encoderPinA, encoderPinB);

int buttonStateA;
int lastButtonStateA;
int buttonStateB;
int lastButtonStateB;

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
  pinMode( buttonPinA, INPUT_PULLUP );
  pinMode( buttonPinB, INPUT_PULLUP );
  pinMode( relayPin, OUTPUT );
  digitalWrite( relayPin, HIGH );  // turn it off
}

long oldPosition  = -999;

long startPosition;
long endPosition;
bool moveForward;   // true if incrementing encoder, false is decrementing encoder
bool moving = false; // true when we are moving

const long INCHES_TO_COUNTS = 512;  // 512 pulses per inch

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
    Serial.print ("MOVEUP");
    Serial.println(moveForward);
    Serial.print("MOVINGSTATE");
    Serial.println(moving);


  }

  if ( moving ) {
    if ( moveForward == true ) {
      if ( newPosition >= endPosition ) {
        // all done moving
        digitalWrite( relayPin, HIGH );  // motor off
        Serial.println( "Forward move complete.");
        moving = false;
      }
    } else {
      if ( newPosition <= endPosition ) {
        // all done moving
        digitalWrite( relayPin, HIGH );  // motor off
        Serial.println( "Reverse move complete.");
        moving = false;
      }
    }
  }

  // check button A
  buttonStateA = digitalRead(buttonPinA);

  if (buttonStateA != lastButtonStateA) {
    lastButtonStateA = buttonStateA;
    // if the state has changed, increment the counter
    if (buttonStateA == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("Button A on");
      startMovement(1.0);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

  // check button B
  buttonStateB = digitalRead(buttonPinB);

  if (buttonStateB != lastButtonStateB) {
    lastButtonStateB = buttonStateB;
    // if the state has changed, increment the counter
    if (buttonStateB == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("Button B on");
      startMovement(1.5);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
}

void startMovement(float inches)
{
  startPosition = myEnc.read();
  endPosition = startPosition + inches * INCHES_TO_COUNTS;
  Serial.print( "Starting at " );
  Serial.print( startPosition );
  Serial.print( " and moving to " );
  Serial.println( endPosition );
  if ( endPosition < startPosition ) {
    moveForward = false;
  }
  else {
    moveForward = true;
  }
  digitalWrite( relayPin, LOW ); // start moving
  moving = true;
}

blh64:
The comparison was backwards. Moving forward, newPosition = 0 and endPosition = 512 so the condition for stopping the move would be newPosition >= endPosition.

You definitely do NOT want to use a do...while() loop or any other type of loop. That is what the loop() function is already doing. If you do, your buttons will not be responsive.

#include <Encoder.h>

enum PinAssignments {
  encoderPinA = 3,  // rigth
  encoderPinB = 2,  // left
};

// assume buttons are wired one side to ground, one side th arduino pin
const int buttonPinA = 4;
const int buttonPinB = 5;

const int relayPin = 7;

// Change these two numbers to the pins connected to your encoder.
//  Best Performance: both pins have interrupt capability
//  Good Performance: only the first pin has interrupt capability
//  Low Performance:  neither pin has interrupt capability
Encoder myEnc(encoderPinA, encoderPinB);

int buttonStateA;
int lastButtonStateA;
int buttonStateB;
int lastButtonStateB;

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
  pinMode( buttonPinA, INPUT_PULLUP );
  pinMode( buttonPinB, INPUT_PULLUP );
  pinMode( relayPin, OUTPUT );
  digitalWrite( relayPin, HIGH );  // turn it off
}

long oldPosition  = -999;

long startPosition;
long endPosition;
bool moveForward;  // true if incrementing encoder, false is decrementing encoder
bool moving = false; // true when we are moving

const long INCHES_TO_COUNTS = 512;  // 512 pulses per inch

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
    Serial.print ("MOVEUP");
    Serial.println(moveForward);
    Serial.print("MOVINGSTATE");
    Serial.println(moving);

}

if ( moving ) {
    if ( moveForward == true ) {
      if ( newPosition >= endPosition ) {
        // all done moving
        digitalWrite( relayPin, HIGH );  // motor off
        Serial.println( "Forward move complete.");
        moving = false;
      }
    } else {
      if ( newPosition <= endPosition ) {
        // all done moving
        digitalWrite( relayPin, HIGH );  // motor off
        Serial.println( "Reverse move complete.");
        moving = false;
      }
    }
  }

// check button A
  buttonStateA = digitalRead(buttonPinA);

if (buttonStateA != lastButtonStateA) {
    lastButtonStateA = buttonStateA;
    // if the state has changed, increment the counter
    if (buttonStateA == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("Button A on");
      startMovement(1.0);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

// check button B
  buttonStateB = digitalRead(buttonPinB);

if (buttonStateB != lastButtonStateB) {
    lastButtonStateB = buttonStateB;
    // if the state has changed, increment the counter
    if (buttonStateB == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("Button B on");
      startMovement(1.5);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
}

void startMovement(float inches)
{
  startPosition = myEnc.read();
  endPosition = startPosition + inches * INCHES_TO_COUNTS;
  Serial.print( "Starting at " );
  Serial.print( startPosition );
  Serial.print( " and moving to " );
  Serial.println( endPosition );
  if ( endPosition < startPosition ) {
    moveForward = false;
  }
  else {
    moveForward = true;
  }
  digitalWrite( relayPin, LOW ); // start moving
  moving = true;
}

IT WORKS!!!
I have been playing with code to get this to work for over 2 years thanks so much!!!!

What I will have to do now is play around and make sure I can get the up down to work!!!

I have create a new code line to give me an accurate 512 per cycle.

Let me know if anyone recommends a way to improve the code.

#include <EncoderStepCounter.h>
#define ENCODER_PIN1 2
#define ENCODER_INT1 digitalPinToInterrupt(ENCODER_PIN1)
#define ENCODER_PIN2 3
#define ENCODER_INT2 digitalPinToInterrupt(ENCODER_PIN2)


//enum PinAssignments {
//  Encoder_Pin1 = 3,   // rigth
//  Encoder_Pin2 = 2,   // left
//};


// assume buttons are wired one side to ground, one side th arduino pin
const int buttonPinA = 4;
const int buttonPinB = 5;
EncoderStepCounter encoder(ENCODER_PIN1, ENCODER_PIN2);
const int relayPin = 7;

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability

int buttonStateA;
int lastButtonStateA;
int buttonStateB;
int lastButtonStateB;

void setup() {
  encoder.begin();
  // Initialize interrupts
  attachInterrupt(ENCODER_INT1, interrupt, CHANGE);
  attachInterrupt(ENCODER_INT2, interrupt, CHANGE);
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
  pinMode( buttonPinA, INPUT_PULLUP );
  pinMode( buttonPinB, INPUT_PULLUP );
  pinMode( relayPin, OUTPUT );
  digitalWrite( relayPin, HIGH );  // turn it off
}
void interrupt() {
  encoder.tick();
}
signed long position = 0;
long oldPosition  = -999;

long startPosition;
long endPosition;
bool moveForward;   // true if incrementing encoder, false is decrementing encoder
bool moving = false; // true when we are moving

const long INCHES_TO_COUNTS = 512;  // 512 pulses per inch

void loop() {
 signed char pos = encoder.getPosition();
  if (pos != 0) {
    position += pos;
    encoder.reset();
  //  Serial.println(position);
  //Serial.print ("MOVEUP");
  //  Serial.println(moveForward);
  //  Serial.print("MOVINGSTATE");
 //   Serial.println(moving);
  }
   long newPosition = position;
 if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
   // Serial.print ("MOVEUP");
  //  Serial.println(moveForward);
  //  Serial.print("MOVINGSTATE");
  //  Serial.println(moving);
 }

  if ( moving ) {
    if ( moveForward == true ) {
      if ( newPosition >= endPosition ) {
        // all done moving
        digitalWrite( relayPin, HIGH );  // motor off
        Serial.println( "Forward move complete.");
        moving = false;
      }
    } else {
      if ( newPosition <= endPosition ) {
        // all done moving
        digitalWrite( relayPin, HIGH );  // motor off
        Serial.println( "Reverse move complete.");
        moving = false;
      }
    }
  }
  // check button A
  buttonStateA = digitalRead(buttonPinA);

  if (buttonStateA != lastButtonStateA) {
    lastButtonStateA = buttonStateA;
    // if the state has changed, increment the counter
    if (buttonStateA == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("Button A on");
      startMovement(1.0);
       Serial.print("MOVINGSTATE");
    Serial.println(moving);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

  // check button B
  buttonStateB = digitalRead(buttonPinB);

  if (buttonStateB != lastButtonStateB) {
    lastButtonStateB = buttonStateB;
    // if the state has changed, increment the counter
    if (buttonStateB == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("Button B on");
      startMovement(1.5);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
}

void startMovement(float inches)
{
  startPosition = position;
  endPosition = startPosition + inches * INCHES_TO_COUNTS;
  Serial.print( "Starting at " );
  Serial.print( startPosition );
  Serial.print( " and moving to " );
  Serial.println( endPosition );
  if ( endPosition < startPosition ) {
    moveForward = false;
  }
  else {
    moveForward = true;
  }
  digitalWrite( relayPin, LOW ); // start moving
  moving = true;
}

My newest issue is now the LCD. I have this issue on another project also.
What I want to do is display the button I pushed, so when I click one inch I would like to display it on the screen.

The problem is that the screen doesnt refresh after the button work has been completed.

I tried to add this to the startmovement however the screen flashes.

I tried to add this to the startmovement however the screen flashes.

I can't see anything wrong with the code ...

I have the up and down relays working perfectly with the amounts set. But I just noticed an issue.

I always want the saw head to drop from zero.

So for example if i finish my cut that will set the encoder to 0.
If I manually go up 5 inches or -2560 ticks.

Then I push the once inch button which is 512 ticks. I want the head to drop 3072.

the code is to large to apply in the post so I have to add as an attachment

12-23-2019.ino (11.8 KB)

tylerltr450:
I have the up and down relays working perfectly with the amounts set. But I just noticed an issue.

I always want the saw head to drop from zero.

So for example if i finish my cut that will set the encoder to 0.
If I manually go up 5 inches or -2560 ticks.

Then I push the once inch button which is 512 ticks. I want the head to drop 3072.

the code is to large to apply in the post so I have to add as an attachment

never-mind I figured out my own stupidity with math.

endPosition = (startPosition +(-1*startPosition)) + ticks;

what this will do is trigger the relay until the value is hit.

so if I am at -2560 and I hit 1 inchs so 512 ticks. This will hold the relay until 512 is hit.

Sometimes I confuse myself but I am sure this code will work, because everytime I move the mill forward the home button is triggered.

which means that if I drop the head 5 inchs to make my first cut, I will always either do half up or move the head up to roll back.

So the math should be fine. Also I run the half up on a different relay and I call the startmovementup which is a different set of code to calculate half up is -256.

what I am having issues with now is programming a button.
I would like to program a button if the program switch is low.

so if program switch button is low set program position.
however my code is setting program position to zero.

the goal is the ability to program a button to whatever depth I want. outside of the standard buttons.

once programmed I should be able to hit the programmed height button and it would call the programmed height.

buttonStateL = digitalRead(buttonProgramSwitch);
  if (buttonStateL != lastButtonStateL) {
    lastButtonStateL = buttonStateL;
   if (buttonStateL == LOW) {
     Serial.println("Programing");
    
    position = programPosition;
  Serial.println(programPosition);
}
 delay(50); 
  }

  // check button K Programed Inches
  buttonStateK = digitalRead(buttonProgram);

  if (buttonStateK != lastButtonStateK) {
    lastButtonStateK = buttonStateK;
    // if the state has changed, increment the counter
    if (buttonStateK == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("Programed");
      lcd.setCursor(0, 0);
      lcd.print("Programed Button");
      startMovementDown(programPosition);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

I think you have your variable assignment reversed. You want the current position 'position' assigned to the new variable 'programPosition' so you can use it later.

buttonStateL = digitalRead(buttonProgramSwitch);
  if (buttonStateL != lastButtonStateL) {
    lastButtonStateL = buttonStateL;
   if (buttonStateL == LOW) {
     Serial.println("Programing");
   
    programPosition = position;
  Serial.println(programPosition);
}
 delay(50);
  }

  // check button K Programed Inches
  buttonStateK = digitalRead(buttonProgram);

  if (buttonStateK != lastButtonStateK) {
    lastButtonStateK = buttonStateK;
    // if the state has changed, increment the counter
    if (buttonStateK == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("Programed");
      lcd.setCursor(0, 0);
      lcd.print("Programed Button");
      startMovementDown(programPosition);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

If you want this programmed value to stay around even with power cycles, then you will have to write it to the EEPROM and read it back on power-up.

As for the size of your code, you could greatly reduce it if you used arrays for all the buttons rather than duplicating each button (A,B,C,...)

You also seem to be using pin 3 for two purposes. The encoder and the LCD backlight. Probably not a good thing...

Same overlap is true with buttonProgram/buttonProgramSwitch and your relay pins. The comments also do not match the assignment. Analog pin A2 is NOT digital pin 14. Comments that lie are worse than no comments at all.

blh64:
I think you have your variable assignment reversed. You want the current position 'position' assigned to the new variable 'programPosition' so you can use it later.

buttonStateL = digitalRead(buttonProgramSwitch);

if (buttonStateL != lastButtonStateL) {
    lastButtonStateL = buttonStateL;
  if (buttonStateL == LOW) {
    Serial.println("Programing");
 
    programPosition = position;
  Serial.println(programPosition);
}
delay(50);
  }

// check button K Programed Inches
  buttonStateK = digitalRead(buttonProgram);

if (buttonStateK != lastButtonStateK) {
    lastButtonStateK = buttonStateK;
    // if the state has changed, increment the counter
    if (buttonStateK == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("Programed");
      lcd.setCursor(0, 0);
      lcd.print("Programed Button");
      startMovementDown(programPosition);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }




If you want this programmed value to stay around even with power cycles, then you will have to write it to the EEPROM and read it back on power-up.


As for the size of your code, you could greatly reduce it if you used arrays for all the buttons rather than duplicating each button (A,B,C,...)

You also seem to be using pin 3 for two purposes. The encoder and the LCD backlight. Probably not a good thing...

Same overlap is true with buttonProgram/buttonProgramSwitch and your relay pins. The comments also do not match the assignment. Analog pin A2 is NOT digital pin 14. Comments that lie are worse than no comments at all.

Ahh a simple mistake onces again. Im learning!!

made the change and it works. I can drop the head to whatever tick count and then it sets it.

as for writing to EEPROM, I am not so worried about that because normally its a custom set each time I saw, and to be honest I never used it on the current setup since it overwrites one of my 4 buttons.

the current mill setup also offers an extra 1/8 inch if the toggle is set. I might work on that first before trying to build arrays.

BLH64 thanks for your help in this undertaking, I have learned a lot while doing this thanks to your help!

I finished the toggle for adding an extra 125 clicks if selected.

I will not start working on the array to see if I can get to work the way i would like.

Ok guys im back again and I am having troubles with my code.
I am trying to add the ability to control the ticks to inch.

The trouble I am at is that I ran out of pins on the Uno.

What I am looking to do is use 2 already used buttons for a new function. It is like below.

IF program switch is LOW and Program button is LOW
then execute the programming of user height

if program switch is low and 1 inch button is pressed
then add a tick to bladetick

if program switch is low and 2 inch button is pressed
then subtract a tick from bladetick.

The problem I am having is when I have program switch low and I push the 1 inch button it triggers the 1 inch drop and doesn't adjust the tick count. I tried to do a while loop and that never worked any suggestions to lock the code in the if loop while program switch is low.

Here is the snipit of my code to help out.

 buttonStateL = digitalRead(buttonProgramSwitch);
  if (buttonStateL != lastButtonStateL) {
    lastButtonStateL = buttonStateL;
   while (buttonStateL == LOW) {
     Serial.println("Programing");
    if (buttonStateK == LOW){ 
    programPosition = (LastCutPosition - newPosition);;
  Serial.println(programPosition);
  cutHeight = (float)programPosition / bladeticks;
  lcd.setCursor(0, 0);
  lcd.print("Program Complete");
  lcd.setCursor(0, 1);
  lcd.print("Pgrm Height ");
  lcd.print (cutHeight, 2);
   lcd.print("in");
   delay(50); 
}
  if (buttonPinOne == LOW) {
    int bladeticks = ++stock;
    lcd.setCursor(0, 1);
  lcd.print("Blade Kerf: ");
  Serial.print(bladeticks);
  lcd.print(bladeticks);
  }
  if (buttonPinOneQuarter == LOW) {
    int bladeticks = --stock;
    lcd.setCursor(0, 1);
  lcd.print("Blade Kerf: ");
  lcd.print(bladeticks);
  }
  }
 delay(50); 
  }

Once again, you did NOT post your entire sketch. Please do so. How is anyone supposed to try to compile your code if you don't provide it.

One thing I do notice is that you have multiple definitions of 'bladeticks' that are each within an if() statement so they have no relationship to one another.

Here is the code.
I set

#include <EncoderStepCounter.h>
#define ENCODER_PIN1 2
#define ENCODER_INT1 digitalPinToInterrupt(ENCODER_PIN1)
#define ENCODER_PIN2 3
#define ENCODER_INT2 digitalPinToInterrupt(ENCODER_PIN2)
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR    0x27  // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
//SDA PIN A4
//SCL PIN A5



//enum PinAssignments {
//  Encoder_Pin1 = 3,   // rigth
//  Encoder_Pin2 = 2,   // left
//};


// assume buttons are wired one side to ground, one side th arduino pin
const int buttonPinOne = 5;
const int buttonPinOneQuarter = 6;
const int buttonProgram = 16;//analog pin A2
const int buttonProgramSwitch = 17;//analog pin A3

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);    // Set the LCD I2C address

EncoderStepCounter encoder(ENCODER_PIN1, ENCODER_PIN2);
const int relayPinDown = 14;//analog pin A0
const int relayPinUp = 15;//analog pin A1

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability

// One Inch
int buttonStateB;
int lastButtonStateB;
// One 1/4
int buttonStateC;
int lastButtonStateC;
//One 1/2
int buttonStateD;
int lastButtonStateD;
// Program Button
int buttonStateK;
int lastButtonStateK;
// Program Button Switch
int buttonStateL;
int lastButtonStateL;


void setup() {
  encoder.begin();
  // Initialize interrupts
  attachInterrupt(ENCODER_INT1, interrupt, CHANGE);
  attachInterrupt(ENCODER_INT2, interrupt, CHANGE);
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
  lcd.begin (20, 4);
  lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home ();                   // go home
  lcd.setCursor(0, 0);
  // Print a message to the LCD.
  lcd.print("Sawmill");
  lcd.setCursor(0, 1);
  lcd.print(" Rev 2.0");
  lcd.setCursor(0, 2);
  lcd.print("");

  delay(2000);
  lcd.clear();
  pinMode( buttonPinOne, INPUT_PULLUP );
  pinMode( buttonPinOneQuarter, INPUT_PULLUP );
  pinMode (buttonProgram, INPUT_PULLUP);
  pinMode( buttonProgramSwitch, INPUT_PULLUP);
  pinMode( relayPinDown, OUTPUT );
  digitalWrite( relayPinDown, HIGH );  // turn it off
  pinMode( relayPinUp, OUTPUT );
  digitalWrite( relayPinUp, HIGH );  // turn it off

}
void interrupt() {
  encoder.tick();
}
signed long position = 0;
long oldPosition  = -999;

long startPosition;
long endPosition;
long NewendPosition;
long LastCutPosition;
long programPosition;
long bladeTicks;
long ZeroPosition;
float CutDepth;
float bladeHeight;
float cutHeight;
bool moveForward;   // true if incrementing encoder, false is decrementing encoder
bool moving = false; // true when we are moving
static int stock = 86;

void loop() {

  signed char pos = encoder.getPosition();
  if (pos != 0) {
    position += pos;
    encoder.reset();
    //  Serial.println(position);
    //Serial.print ("MOVEUP");
    //  Serial.println(moveForward);
    //  Serial.print("MOVINGSTATE");
    //   Serial.println(moving);
  }
  long newPosition = position;
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  bladeHeight = (((float)newPosition / 86)+1);
  lcd.setCursor(0, 3);
  lcd.print("Blade Height ");
    lcd.print(bladeHeight, 2);
   lcd.print("in");
  CutDepth = (((float)LastCutPosition - (float)newPosition) / 86);
   lcd.setCursor(0, 2);
  lcd.print("Cut Depth ");
  lcd.print(CutDepth, 2);
  lcd.setCursor(14,2);
  lcd.print("  ");
 
  }

  if ( moving ) {
    if ( moveForward == true ) {
      if ( newPosition >= endPosition ) {
        // all done moving
        digitalWrite( relayPinDown, HIGH );  // motor off
        digitalWrite( relayPinUp, HIGH );  // motor off
        Serial.println( "Forward move complete.");
        moving = false;
        lcd.clear();

      }
    } else {
      if ( newPosition <= endPosition ) {
        // all done moving
        digitalWrite( relayPinDown, HIGH );  // motor off
        digitalWrite( relayPinUp, HIGH );  // motor off
        Serial.println( "Reverse move complete.");
        moving = false;
        lcd.clear();

      }
    }
  }


  // check button B 1 Inch
  buttonStateB = digitalRead(buttonPinOne);

  if (buttonStateB != lastButtonStateB) {
    lastButtonStateB = buttonStateB;
    // if the state has changed, increment the counter
    if (buttonStateB == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("One Inch");
      lcd.setCursor(0, 0);
      lcd.print("1 in               ");
      startMovementDown(92);
      lcd.setCursor(0,1);
      lcd.print("                  ");
       
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

  // check button C 1.25 Inch
  buttonStateC = digitalRead(buttonPinOneQuarter);

  if (buttonStateC != lastButtonStateC) {
    lastButtonStateC = buttonStateC;
    // if the state has changed, increment the counter
    if (buttonStateC == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("One Quarter on");
      lcd.setCursor(0, 0);
      lcd.print("1 1/4              ");
      startMovementDown(114);
      lcd.setCursor(0,1);
      lcd.print("                  ");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

 buttonStateL = digitalRead(buttonProgramSwitch);
  if (buttonStateL != lastButtonStateL) {
    lastButtonStateL = buttonStateL;
   if (buttonStateL == LOW);{{ {{
     Serial.println("Programing");
   }
    if (buttonStateK == LOW){ 
    programPosition = (LastCutPosition - newPosition);;
  Serial.println(programPosition);
  cutHeight = (float)programPosition / 86;
  lcd.setCursor(0, 0);
  lcd.print("Program Complete");
  lcd.setCursor(0, 1);
  lcd.print("Pgrm Height ");
  lcd.print (cutHeight, 2);
   lcd.print("in");
   delay(50); 
}
  }

  if (buttonPinOne == LOW); {
    int bladeticks = ++stock;
    lcd.setCursor(0, 1);
  lcd.print("Blade Kerf: ");
  Serial.print(bladeticks);
  lcd.print(bladeticks);
  }
  }
  if (buttonPinOneQuarter == LOW); {
    int bladeticks = --stock;
    lcd.setCursor(0, 1);
  lcd.print("Blade Kerf: ");
  lcd.print(bladeticks);
  }
   }
}
  // check button K Programed Inches
  buttonStateK = digitalRead(buttonProgram);

  if (buttonStateK != lastButtonStateK) {
    lastButtonStateK = buttonStateK;
    // if the state has changed, increment the counter
    if (buttonStateK == LOW) {
      // if the current state is LOW then the button went from off to on:
      Serial.println("Programed");
      lcd.setCursor(0, 0);
      lcd.print("Programed Button  ");
      startMovementDown(programPosition);
    lcd.setCursor(0,1);
      lcd.print("                  ");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

}

void startMovementDown(float ticks)
{
  startPosition = position;
  if (buttonStateM == LOW){
    lcd.setCursor(11, 0);
      lcd.print("Extra 1/8");
  endPosition = (LastCutPosition - ticks) - 11;
  }
  else {endPosition = LastCutPosition - ticks;
  }
   Serial.print( "Starting at " );
  Serial.print( startPosition );
  Serial.print( " and moving to " );
  Serial.println( endPosition );
  if ( endPosition < startPosition ) {
    moveForward = false;
  }
  else {
    moveForward = true;
  }
  digitalWrite( relayPinDown, LOW ); // start moving
  moving = true;
}

When I pull the program button low, it says programming but when I push the button to adjust the tick count it tried to move the head down

You still have the issue that blh64 pointed out.

wildbill:
You still have the issue that blh64 pointed out.

How do I call that out then because

IF Program pin is pulled low and if I push 1 inch button it should count up
if Program pin is pulled low and if I push 1.25 button it should count down.

if program button is low {

would I be better doing only this ?

and if inch if is pushed {
count up
}

else if 1.25 button is pushed {
count down
}
}

I guess i am trying to build a loop based on the program pin going low.

would be better to only put

if (buttonPinOne == LOW && buttonStateL == LOW) within the void loop?