consider
#undef MyHW // changes for my hardware & simulated libraries
#ifdef MyHW
char s [80];
struct AccelStepper {
int position;
enum { DRIVER };
AccelStepper (int m, int a, int n) { };
int currentPosition (void) { return position; };
void moveTo (int pos) {
position = pos;
sprintf (s, " %s: pos %6d", __func__, position);
Serial.println (s);
}
void run (void) {
position++;
sprintf (s, " %s: pos %6d", __func__, position);
if (! (position % 50))
Serial.println (s);
delay (100);
};
void setAcceleration (int a) {
sprintf (s, " %s: %6d", __func__, a);
Serial.println (s);
}
void setCurrentPosition (int a) {
position = a;
sprintf (s, " %s: pos %6d", __func__, position);
Serial.println (s);
};
void setMaxSpeed (int a) {
sprintf (s, " %s: %6d", __func__, a);
Serial.println (s);
}
};
const int labelGap = A1;
const int go = A2;
const int onOff = A3;
const int coder = 13;
const int ram = 12;
const int potPin = A0;
#else
# include <AccelStepper.h>
# include <MultiStepper.h>
const tint labelGap = 9; // label gap sensor
const int go = 8; //jar sensor
const int onOff = 7; // on off button pin
const int coder = 12;
const int ram = 6; // ram pin
const int potPin = A1;
#endif
// -----------------------------------------------------------------------------
int coderActive = 0;
int potValue = 0;
unsigned long coderDwell = 100; // time coder is on/HIGH
int labelActive = 0; // label is currently feeding, stepper running
int inProcess = 0; // variable to record if the process is running
int ramVar = 0; // variable to record ram position
int labelMaxLength = 10000;
int labelDeadSpace = 50; // steps to ignore label gap
unsigned long ramInStart; // time stamp for ram moving in
const unsigned long ramInToLabelStartDelay = 500; // delay time from ram moving in to label starting
const unsigned long ramInDelay = 100; // delay from go signal to ram moving in
const unsigned long postLabelDelay = 300; // delay after stepper has stopped till the ram moves out
unsigned long labelFinish; // time stamp for when the stepper has stopped
const unsigned long betweenLabelDelay = 1200; // delay from the ram going out to when the next process can start
unsigned long startMillis ; // timestamp for when the process starts/ go sensor triggered
unsigned long postLabel; // timestamp for when stepper stops
unsigned long coderMillis; // timestamp for when the coder output goes HIGH
int stepsBeforeEnd = 0; // variable for recording the steps at labelgap trigger to compare for overrun
int labelOverrun = 0; // variable to adjust how much overrun with potentiometer
AccelStepper labelStepper (AccelStepper::DRIVER, 11, 10);
// -----------------------------------------------------------------------------
void setup () {
Serial.begin (9600);
pinMode (go, INPUT_PULLUP);
pinMode (labelGap, INPUT_PULLUP);
pinMode (onOff, INPUT_PULLUP);
pinMode (ram, OUTPUT);
pinMode (coder, OUTPUT);
labelStepper.setMaxSpeed (1040);
labelStepper.setAcceleration (5000);//was 12000
labelStepper.setCurrentPosition (0);
Serial.println ("ready");
}
// -----------------------------------------------------------------------------
void loop ()
{
unsigned long msec = millis ();
potValue = analogRead (potPin);
labelOverrun = map (potValue, 0, 1024, 800, 0);
int buttonState = digitalRead (go);
int labelState = digitalRead (labelGap);
int onOffState = digitalRead (onOff);
switch (inProcess) {
case 0:
default:
if (LOW == buttonState && LOW == onOffState) {
inProcess = 1;
startMillis = msec;
// labelStepper.moveTo (labelMaxLength);
Serial.println ("0: start");
}
break;
case 1:
if (0 == ramVar) {
inProcess = 2;
ramVar = 1;
ramInStart = msec;
digitalWrite (ram, HIGH);
Serial.println ("0: ram on");
}
break;
case 2:
if (1 == ramVar && (msec - ramInStart) >= ramInToLabelStartDelay) {
inProcess = 3;
labelActive = 1;
coderActive = 1;
coderMillis = msec;
digitalWrite (coder, HIGH);
Serial.println ("2: coder on, labelling active");
}
break;
case 3:
if (coderActive == 1 && (msec - coderMillis) >= coderDwell) {
coderActive = 3;
digitalWrite (coder, LOW);
Serial.println ("3: coder off");
}
if (labelState == LOW && labelStepper.currentPosition () >= labelDeadSpace) {
inProcess = 4;
stepsBeforeEnd = labelStepper.currentPosition ();
Serial.println ("3: label Gap");
}
break;
case 4:
sprintf (s, " 4: %d %d %d",
labelStepper.currentPosition (), stepsBeforeEnd, labelOverrun);
Serial.println (s);
if ( (labelStepper.currentPosition () - stepsBeforeEnd) >= labelOverrun) {
inProcess = 5;
labelActive = 0;
postLabel = msec;
labelStepper.setCurrentPosition (0);
Serial.println ("4: has Overrun enough");
}
break;
case 5:
if (ramVar == 1 && (msec - postLabel >= postLabelDelay)) {
inProcess = 6;
ramVar = 0;
labelFinish = msec;
digitalWrite (ram, LOW);
Serial.println ("5: ram OUT");
}
break;
case 6:
if (buttonState == HIGH) {
inProcess = 7;
Serial.println ("6: I have seen the empty space");
}
break;
case 7:
inProcess = 0;
coderActive = 0;
Serial.print ("currentMillis ");
Serial.print (msec);
Serial.print (", labelFinish ");
Serial.println (labelFinish);
Serial.println ("7: delay to stop double label");
break;
}
//TO STOP IT LABELLING FOREVER IF SENSOR ISSUE
if (labelStepper.currentPosition () >= labelMaxLength) {
inProcess = 5;
labelActive = 0;
coderActive = 0;
postLabel = msec;
Serial.print ("label Position ");
Serial.print (labelStepper.currentPosition ());
Serial.println (" label gone too long");
labelStepper.setCurrentPosition (0);
}
//MOVE MOTOR
if (labelActive == 1) {
labelStepper.run ();
#if 0
Serial.print ("label Position ");
Serial.println (labelStepper.currentPosition ());
#endif
}
}