I have been trying to incorporate an interrupt for the limit switch violation (taking the check out of the main loop) and trying to incorporate functions instead of one big loop. I have been trying to track down the errors all evening but can't seem to make headway. Can anyone give me some guidance please?
I tried have one loop for spinning the motor, and then another loop to display data on the LCD, but only once per loop. The limit or "stopp" function deals with the limit switch getting set and I was hoping to be able to flag it, then reset it by bumping the jog button. I would not want the motor to start right away when I cleared the table away from the limit so hoping to require a reset of some fashion, without adding another button. I know that part of the code needs work but I can't get past the numerous errors.
Thanks in advance.
#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 = 1; // sets table direction initially CW is table left
const int enablepin = A6; //enable pin out to controller
int enableout = 1; //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 = 1; //sets runn to state of runpin (low to run)
const int jogin = A3; //jog switch in
int jogout = 0; //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 = 1; //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 = 800; //set to pulse setting on controller
volatile int stopped = 0; //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 () {
int stopped = 0;
}
void setup() {
pinMode(limitpin, INPUT_PULLUP); //limitswitch low when violated
pinMode (dir_in, INPUT_PULLUP); // switch takes pin low, otherwise, pin is high due to pullup
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
// displaydata(); //goes to display data function
//spinmotor(); //spin the motor
}
void spinmotor() {
digitalWrite(limitled, !stopped); //lights indicator***************************
if (stopped == 0) //checks to see if limit flag has been reset
stopp(); //if not, go to stopp function
}
++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
}
}
limit = digitalRead(limitpin); //one to run, zero if at limit
runn = digitalRead(runpin); //zero to run, one to not run
jogout = digitalRead(jogin);
dir = digitalRead(dir_in); //reads motor direction according to switch
digitalWrite(dirpinout, dir); //sets dir pin to controller
if (limit + runn == 2) { // if run on and limit not reached, enable controller
digitalWrite(enablepin, LOW); //enables motor
}
else if (limit + jogout == 2) { //if jog on and limit not reached, enable controller
digitalWrite(enablepin, LOW); //enbables motor
}
else digitalWrite(enablepin, HIGH); //disable controller
}
void spinmotor() { // reads speed control and spins the motor
speedpulse = analogRead (potin); //reads speed pot
speedpulse = map (speedpulse, 0, 1023, 250, 16250); //converts pot to speed delays max is 16383
if (stopped = 1) { //if stopped flag is set
stopp(); // then goto stopp function, otherwise, do the following
}
digitalWrite (pulse, HIGH); //pulses motor
delayMicroseconds(100); //100 usec pulse delay
digitalWrite (pulse, LOW); //pulses motor off
delayMicroseconds(speedpulse); //delay
delayMicroseconds(speedpulse); //delay
}
void displaydat() { //function to display data is called once per loop
lcd.clear();
rpm = 60000 / (tfrpm - tsrpm); //converting numbers to minute values
lcd.print("RPM Feed in/min");
lcd.setCursor(0, 1);
lcd.print(rpm, 1);
lcd.print(" --- ");
lcd.print(rpm * 0.1);
}
void stopp() { //stopp function for limit violation
lcd.clear(); //writes situation to display
delay(500);
lcd.print("Limit reached");
lcd.setCursor(0, 1);
lcd.print("Push Jog to reset");
delay (1000);
if digitalRead(A3) = 0{ //if jog button pushed, reset stopp flag, else, don't
stopped = 1;
}