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