See code below. Be kind I am far from being an expert!
#include <Stepper.h>
#include <NewSoftSerial.h>
#include <SerLCD.h>
NewSoftSerial NSS(0,12); // NewSoftSerial Object on pin 12
SerLCD theLCD(NSS); // SerLCD object using that NSS
const int mtstep = 2;
const int mtdir = 3;
const int enmot = 4;
const int upsl = 5;
const int dwsl = 6;
const int upfs = 7;
const int rpos = 9;
const int uplm = 10;
const int dwlm = 8;
float pos = 0;
long tmr = 0;
int dly = 0;
int lcdon = 0;
unsigned long msecbegin;
unsigned long msecend;
unsigned long mtime;
float posref;
float posend;
float posbegin;
float posdif;
float posmm;
int msectime;
long p = 0;
float mmsec;
void setup ()
{
// Remember to start the NewSoftSerial port before doing things with the SerLCD
delay (5000);
NSS.begin(9600); // This will attempt to initialize the display to blank with the backlight on
delay (500);
theLCD.begin();
delay (50);
theLCD.setPosition(1,2);
delay (50);
theLCD.print("Linear slide");
delay (50);
theLCD.setPosition(2,2);
delay (50);
theLCD.print("Test");
delay (2000);
theLCD.clear();
pinMode (mtstep, OUTPUT); // Pin 2 motor steps
pinMode (mtdir, OUTPUT); // Pin 3 motor direction
pinMode (enmot, OUTPUT); // Pin 4 enable-disable motor
pinMode (upsl, INPUT); // Pin 5 button up slow
pinMode (dwsl, INPUT); // Pin 6 button down slow direction for driver is HIGH
pinMode (upfs, INPUT); // Pin 7 button up fast direction for driver is LOW
pinMode (rpos, INPUT); // Pin 9 button reset position on the lcd
pinMode (uplm, INPUT); // Pin 8 limit switch up
pinMode (dwlm, INPUT); // Pin 10 limit switch down
digitalWrite (upsl, HIGH); // pullup pin 5
digitalWrite (dwsl, HIGH); // pullup pin 6
digitalWrite (upfs, HIGH); // pullup pin 7
digitalWrite (rpos, HIGH);// pullup pin 9
digitalWrite (uplm, HIGH);// pullup pin 10
digitalWrite (dwlm, HIGH);// pullup pin 8
msectime = 0;
posbegin = 12;
}
// Prepare to take some steps
void loop ()
{
digitalWrite (enmot, LOW); // Turn off the motor to keep it from getting overheated or to allow manual motion
int upslread = digitalRead (upsl); // read and attribute value to upslread
int dwslread = digitalRead (dwsl); // read and attribute value to upslread
int upfsread = digitalRead (upfs); // read and attribute value to upfsread
int rposread = digitalRead (rpos); // read and attribute value to rposread
int uplmread = digitalRead (uplm); // read and attribute value to uplmread
int dwlmread = digitalRead (dwlm); // read and attribute value to dwlmread
tmr = tmr++;
lcdon = (tmr/200);
if (upslread == HIGH)
{
if (dwslread == HIGH)
{
if (upfsread == HIGH)
{
if (lcdon > 1)
{
p = 0; // reset posbegin
posend = pos; // the final position is the actual position pos
posdif = ((posend-posbegin)*1000); // the difference between now and where it was before.
posdif = (((posdif*0.025))/10);
theLCD.setBacklight(157);
theLCD.clear();
theLCD.setPosition(1,0);
posmm = (((pos*0.025))/10);
mtime = ((msecend - msecbegin));
theLCD.print(posmm);
//theLCD.print(msecbegin);
//theLCD.print(posend);
theLCD.setPosition(1,8);
theLCD.print("mm");
tmr = 0;
lcdon = 0;
theLCD.setPosition(2,0);
mmsec = (posdif/mtime);
theLCD.print((mmsec));
theLCD.setPosition(2,8);
theLCD.print("mm/s");
msectime = 0;
}
}
}
}
if ((upslread == LOW) && (uplmread == HIGH))
{
if (p == 0)
{
posbegin = pos;
p = p++;
}
for (int i = 0; i < 1; i++)
{
digitalWrite (enmot, HIGH);
digitalWrite (mtstep, HIGH);
digitalWrite (mtdir, LOW);
delayMicroseconds (2600);
pos = pos++;
digitalRead (upslread);
if (upslread == LOW)
{
msecend = millis();
int i = i--;
}
if (msectime == 0)
{
msecbegin = millis();
msectime = msectime++;
}
}
}
else
{
digitalWrite (mtstep, LOW);
}
if ((dwslread == LOW) && (dwlmread == HIGH))
{
if (p == 0)
{
posbegin = pos;
p = p++;
}
for (int i = 0; i < 1 ; i++)
{
digitalWrite (enmot, HIGH);
digitalWrite (mtstep, HIGH);
digitalWrite (mtdir, HIGH);
delayMicroseconds (2600);
posref = posref -1;
pos = pos - 1;
digitalRead (dwslread);
if (dwslread == LOW)
{
msecend = millis();
int i = i--;
}
if (msectime == 0)
{
msecbegin = millis();
msectime = msectime++;
}
}
}
else
{
digitalWrite (mtstep, LOW);
}
if ((upfsread == LOW) && (uplmread == HIGH))
{
if (p == 0)
{
posbegin = pos;
p = p++;
}
for (int i = 0; i < 1; i++)// upfs switch and LCD counting.
{
digitalWrite (enmot, HIGH);
digitalWrite (mtstep, HIGH);
digitalWrite (mtdir, LOW);
delayMicroseconds (150);
posref = posref + 1;
pos = pos + 1;
digitalRead (upfsread);
if (upfsread == LOW)
{
msecend = millis();
int i = i--;
}
if (msectime == 0)
{
msecbegin = millis();
msectime = msectime++;
}
}
}
else
{
digitalWrite (mtstep, LOW);
}
if (rposread == LOW){
theLCD.clear();
theLCD.print("POSITION RESET");
theLCD.setPosition (2,0);
delay (50);
theLCD.print (".");
theLCD.setPosition (2,1);
delay (50);
theLCD.print (".");
theLCD.setPosition (2,2);
delay (50);
theLCD.print (".");
theLCD.setPosition (2,3);
delay (50);
theLCD.print (".");
theLCD.setPosition (2,4);
delay (50);
theLCD.print (".");
theLCD.setPosition (2,5);
delay (50);
theLCD.print (".");
theLCD.setPosition (2,6);
delay (50);
theLCD.print (".");
theLCD.setPosition (2,7);
delay (50);
theLCD.print (".");
theLCD.setPosition (2,8);
delay (50);
theLCD.print (".");
theLCD.setPosition (2,9);
delay (50);
theLCD.print (".");
theLCD.setPosition (2,10);
delay (50);
theLCD.print (".");
theLCD.setPosition (2,11);
delay (50);
theLCD.print (".");
theLCD.setPosition (2,12);
delay (50);
theLCD.print (".");
theLCD.setPosition (2,13);
delay (50);
theLCD.print (".");
theLCD.setPosition (2,14);
delay (50);
theLCD.print (".");
theLCD.setPosition (2,15);
delay (50);
theLCD.print (".");
theLCD.setPosition (2,16);
delay (50);
theLCD.print (".");
delay (50);
theLCD.clear();
pos = 0;
theLCD.print(pos);
theLCD.setPosition(1,8);
theLCD.print("mm");
msecbegin = msecend; // give 0mm/s
posdif = 0;
}
else
{
digitalWrite (enmot, LOW); // shut down the motor
}
}