It apears that the problem is the following code.
If I comment it out, it works, but I loose my menu functions.
uint8_t selectedMenuItem = lcd.showMenu(menu, menuLen, 1);
if (selectedMenuItem == mkLoadLabels) {
lcd.print("Loading Labels");
loadingLabels = true;
runningLabels = false;
pedalIsPressed = false;
loadLabels();
} else if (selectedMenuItem == mkRunLabels) {
lcd.print("Run Labels");
runningLabels = true;
loadingLabels = false;
pedalIsPressed = false;
countLabels = 0;
// Serial.println(runningLabels);
} else if (selectedMenuItem == mkExit) {
lcd.print("Exit OK?");
lcd.setCursor(2, 1);
lcd.print("Click to Exit");
runningLabels = false;
loadingLabels = false;
pedalIsPressed = false;
mkRoot;
}
while (lcd.getEncoderState() == eNone)
;
Complete code:
#include <Hardware.h>
#include "AccelStepper.h"
#include <Wire.h>
#include <LiquidCrystal_I2C_Menu.h>
LiquidCrystal_I2C_Menu lcd(0x27, 16, 2);
#define pinCLK 8
#define pinDT 9
#define pinSW 7
#define enable23 13
#define STEP_PIN_23 11
#define DIR_PIN_23 10
#define enable17 12
#define STEP_PIN_17 4
#define DIR_PIN_17 5
AccelStepper stepper23(1, STEP_PIN_23, DIR_PIN_23);
AccelStepper stepper17(1, STEP_PIN_17, DIR_PIN_17);
#define pedalPin 3
int pedalPinStatus;
bool runningLabels = false;
volatile bool pedalIsPressed = false;
int countLabels = 0;
#define irPin 2
bool loadingLabels = false;
int irPinStatus;
bool loadingFlag = 0;
unsigned long timeEnd;
unsigned long timeBegin;
enum { mkBack,
mkRoot,
mkLoadLabels,
mkRunLabels,
mkExit,
};
sMenuItem menu[] = {
{ mkBack, mkRoot, "Menu Select:" },
{ mkRoot, mkLoadLabels, "Load Labels" },
{ mkRoot, mkRunLabels, "Run Labels" },
{ mkRoot, mkExit, "Exit" },
};
uint8_t menuLen = sizeof(menu) / sizeof(sMenuItem);
void pedal_ISR(void) {
if (runningLabels) {
detachInterrupt(digitalPinToInterrupt(pedalPin));
pedalIsPressed = true;
}
}
void setup() {
Serial.begin(115200);
lcd.begin();
lcd.attachEncoder(pinDT, pinCLK, pinSW);
lcd.clear();
lcd.print(" BREW BRO");
lcd.setCursor(0, 1);
lcd.print(" WELCOME");
delay(2000);
pinMode(enable23, OUTPUT);
digitalWrite(enable17, LOW); // Disable NEMA 17
pinMode(enable17, OUTPUT);
digitalWrite(enable23, HIGH); // Disable NEMA 23 so user can manually rotate shaft to Load Labels
pinMode(irPin, INPUT);
pinMode(pedalPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(pedalPin), pedal_ISR, LOW);
}
void loadLabels() {
irPinStatus = digitalRead(irPin);
Serial.println("Loading");
digitalWrite(enable23, LOW); // Enable Stepper23
if (irPinStatus == 1) {
timeBegin = micros();
do {
irPinStatus = digitalRead(irPin);
stepper23.setMaxSpeed(3000);
stepper23.setSpeed(-3000);
stepper23.runSpeed();
} while (irPinStatus == 1);
} else if (irPinStatus == 0) {
timeEnd = micros();
loadingLabels = false;
stepper23.stop();
}
unsigned long duration = timeEnd - timeBegin;
Serial.print("Time: ");
Serial.println(duration);
lcd.clear();
lcd.print("Loading Complete");
lcd.setCursor(2, 1);
lcd.print("Click to Exit");
delay(2000);
loadingLabels = false;
}
void runLabels() {
digitalWrite(enable17, HIGH); // Enable stepper17
digitalWrite(enable23, LOW); // Enable stepper23
irPinStatus = digitalRead(irPin);
if (irPinStatus == 0) { // Ensure a label is loaded
timeBegin = micros();
do {
irPinStatus = digitalRead(irPin);
stepper23.setMaxSpeed(5000);
stepper23.setSpeed(-5000);
stepper23.runSpeed();
stepper17.setMaxSpeed(3000);
stepper17.setSpeed(-2000);
stepper17.runSpeed();
} while (irPinStatus == 0);
} else if (irPinStatus == 1) {
runningLabels = false;
timeEnd = micros();
stepper23.stop();
stepper17.stop();
digitalWrite(enable17, LOW); // Disable stepper17
}
unsigned long duration = timeEnd - timeBegin;
Serial.print("Time: ");
Serial.println(duration);
countLabels++;
Serial.print("countLabels: ");
Serial.println(countLabels);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(countLabels);
attachInterrupt(digitalPinToInterrupt(pedalPin), pedal_ISR, LOW);
}
void loop() {
pedalPinStatus = digitalRead(pedalPin);
if (pedalPinStatus == 0) {
Serial.print("Pedal Fired: ");
Serial.println(pedalPinStatus);
runLabels();
}
// uint8_t selectedMenuItem = lcd.showMenu(menu, menuLen, 1);
// if (selectedMenuItem == mkLoadLabels) {
// lcd.print("Loading Labels");
// loadingLabels = true;
// runningLabels = false;
// pedalIsPressed = false;
// loadLabels();
// } else if (selectedMenuItem == mkRunLabels) {
// lcd.print("Run Labels");
// runningLabels = true;
// loadingLabels = false;
// pedalIsPressed = false;
// countLabels = 0;
// // Serial.println(runningLabels);
// } else if (selectedMenuItem == mkExit) {
// lcd.print("Exit OK?");
// lcd.setCursor(2, 1);
// lcd.print("Click to Exit");
// runningLabels = false;
// loadingLabels = false;
// pedalIsPressed = false;
// mkRoot;
// }
// while (lcd.getEncoderState() == eNone)
// ;
}
Serial Monitor:
Pedal Fired: 0
Time: 4285145468
countLabels: 1
Pedal Fired: 0
Time: 4281719528
countLabels: 2