This can work, but maybe not the ideal method. Two things that stick out to me that might not be a problem right now as your program is small and simple, but could become an issue later as it grows. As I mentioned, not using interrupts you are waiting for your program to decide to ..........
Not sure if that makes sense, your code can work the way you have it today, but learning and structuring your code to use functions and interrupts will make it easier on you later if you want to add features or make changes.
I had it working with the previous program, then took your advice and incorporated an interrupt for the limit switch, and only write to the LCD once per loop . I also took the display and stop functions out of the main loop. Good learning along the way, lots of time invested but lots of that learning time, but all good. Need more projects for the Arduino now.
Thanks for the input.
Below is the latest code if anyone is interested
/*This program is intended for an Arduino UNO to run a power feed on a milling machine x axis. It drives a TB6600 Stepper motor controller
* running a NEMA23 stepper motor. It display feed rate, based on 0.100"/revolution of the lead screw.
* It accomodates a limit switch and displays the status of a limit violation.
* April 12 2022
* Enable signal to motor controller is LOW to enable motor
* variable "stopp" = 0 to enable motor
*
*/
#include <LiquidCrystal.h>
/*
LCD RS pin to digital pin 12
LCD Enable pin to digital pin 11
LCD D4 pin to digital pin 4
LCD D5 pin to digital pin 5
LCD D6 pin to digital pin 6
LCD D7 pin to digital pin 7
LCD R/W pin to ground
10K resistor:
ends to +5V and ground
wiper to LCD VO pin (pin 3 on display!)
6600 motor controller set to 400
*/
const int dirpinout = A5; //direction pin out to controller
const int dir_in = A2; //direction control pin in from switch
int dir; // sets table direction initially CW is table left
const int enablepin = 8; //enable pin out to controller
int enableout; //sets motor off to start
const int pulse = A4; // pulse out pin to controller
const int potin = A0; //speed pot connected to pin A0
int speedpulse; // pulse delay for speed management
const int runpin = A1; //run button enables or disables motor
int runn; //runn state(low to run)
const int jogin = A3; //jog switch in
int jogout; //jog out
const int limitpin = 2; //limit switch input
const int limitled = 13; // limit indicator connected to pin 13** may not be connected
int limit; //limit switch state
unsigned long tsrpm = 0; //tsrpm is start of timer for rpm
unsigned long tfrpm = 0; //tfrpm is stoptime for rpm count
int loops = 0; //loops is counter for loop cycles to use for rpm
int rpm = 0; //rpm
const int setpulse = 1600; //set to pulse setting on controller
volatile int stopped = 1; //stop is used to stop motor till a manual reset by activating jog
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void limitisr () {
stopped = 1; //stopped flag 1 should stop motor
//Serial.println("isr"); //used to troubleshoot
}
void setup() {
Serial.begin(9600); // send and receive at 9600 baud
pinMode(limitpin, INPUT_PULLUP); //limitswitch low when violated
pinMode (dir_in, INPUT_PULLUP); // switch takes pin low, otherwise, pin is high due to pullup, setting direction
pinMode(runpin, INPUT_PULLUP); //Run button enables motor to run
pinMode(jogin, INPUT_PULLUP); //Jog button in
pinMode (limitled, LED_BUILTIN); //just an indicator
pinMode (enablepin, OUTPUT); //high to controller to enable
pinMode (pulse, OUTPUT); //pulse to controller
pinMode(dirpinout, OUTPUT); //dir to controller for clockwise ****check this
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
loops = 0; //sets completed loops to 0
attachInterrupt(digitalPinToInterrupt(limitpin), limitisr, LOW);
}
void loop() { //main loop
limit = digitalRead(limitpin);
runn = digitalRead(runpin); //zero to run, one to not run
jogout = digitalRead(jogin); //jogs motor if jog button pushed
//reset after limit violation
if ((limit == 1) && (jogout == LOW)) { //if limit ok and jog button pushed, reset stopp flag
stopped = 0;
}
if (stopped == 1) { //stopp == 1 disables motor
stopp(); //go to stopp function
}
else {
++loops; //increments counter for loops
}
if (loops == 1) { //gets time of first loop since a reset
tsrpm = millis(); //sets the time of the start tsrpm
}
else if (loops == setpulse) { //checks to see if the number of loops equals the number of steps on controller
tfrpm = millis(); //gets final time for rpm calculation
loops = 0; //resets loops counter
displaydata(); //goes to display data function
}
dir = digitalRead(dir_in); //reads motor direction according to switch
digitalWrite(dirpinout, dir); //sets dir pin to controller
if ((stopped == 0) && (runn == 0)) { // if run on and limit not reached, enable controller
digitalWrite(enablepin, LOW); //enables motor
}
else if ((stopped == 0) && (digitalRead(jogin) == 0)) { //if jog on and limit not reached, enable controller
digitalWrite(enablepin, LOW); //enbables motor
}
else {
digitalWrite(enablepin, HIGH); //disable controller see if this allows manual movement of table
}
speedpulse = analogRead (potin); //reads speed pot
speedpulse = map (speedpulse, 0, 1023, 10, 16000); //converts pot to speed delays max is 16383
digitalWrite (pulse, HIGH); //pulses motor
delayMicroseconds(100); //100 usec pulse delay
digitalWrite (pulse, LOW); //pulses motor off
delayMicroseconds(speedpulse); //delay
}
void displaydata() { //function to display data is called once per loop
rpm = 60000 / (tfrpm - tsrpm); //converting numbers to minute values
lcd.clear();
lcd.print("RPM Feed in/min");
lcd.setCursor(0, 1);
lcd.print(rpm);
lcd.print(" --- ");
lcd.print(rpm * 0.1);
}
void stopp() { //stopp function for limit violation
lcd.clear();
lcd.print("Clear limit then");
lcd.setCursor(0, 1);
lcd.print("Jog to reset");
delay (100);
}