Stepper Motor Feed system for punch press.

Hey everyone, newer user here lightly familiar with c++. Here's my situation. My work place has a punch press with a stepper drive feed system controlled by a P315 API Controls Division microstep driver. There's a cam shaft that rotates and 1/rotation triggers a relay switch that will send a signal for the motor to rotate "x" amount of steps. Currently I have everything working and able to send the step signal at variable speeds using: arduino-timer - Arduino Libraries however it either sends a constant pulse train or nothing at all. I just need it to send a pulse train for x amount of steps (to be determined by the required "feed length" of material being pushed into the press) and then await input from the cam sensor before sending another pulse train.

I have tested with AccelStepper and similar libraries with basic "if digitalread(high), do the thing" however none of them seem to get the speed i require which is what led me to using the timer library.

I have also tried adding conditions into the timer functions (ie if (camsensor = high, send pulse until counter = totalrequiredsteps) that do only send pulse signals after the cam signal is received however the rpm on the motor slows down drastically despite not changing the rate in which the signal is sent. Presumably this is related to the clock speed on the board and it not being able to handle all the I/O as quickly as just sending a constant standalone pulse train.

Below is the current working code just standard non-stop pulse train. (ignore the lcd and keypad implementation they are for making an interface afterwards)

Sorry if this is too much/not enough info and thanks in advance for any assistance.

Just to clarify everything is functioning mechanically I just need some help with making it only send a step signal when an external relay switch is triggered.

//******************************** LCD IMPLEMENTATION **************************************************************
#include <LiquidCrystal.h>

const byte LCD_RS = 9;
const byte LCD_E = 8;
const byte LCD_D4 = 5;
const byte LCD_D5 = 4;
const byte LCD_D6 = 3;
const byte LCD_D7 = 2;
//LCD R/W pin to ground
//10K potentiometer wiper to VO
LiquidCrystal lcd(LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
//************************************ KEYPAD IMPLEMENTATION ********************************************************
#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {22, 23, 24, 25};
byte colPins[COLS] = {26, 27, 28, 29};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
//********************************* TIMER IMPLEMENTATION ************************************************************
#include <timer.h>
Timer <1, micros> timer;

//*******************************************************************************************************************
int count = 0; // counter variable to determine current number of steps in cycle.
int steps = 500; // Total number of steps required per cycle. Later to be adjustable via keypad input to change feed length.
byte steppin = 12; //pin assigned to step signal logic input on microstep driver.
byte dirpin = 11; //pin assigned to direction signal logic input on microstep driver.
byte camSensor = 10; //pin assigned to external relay switch triggered by cam shaft rotation signalling when a feed cycle is required.
bool stepping = false; // boolean to check if currently stepping.


bool send_pulse(void *)// pulse generation triggered in timer
{
  digitalWrite(dirpin, HIGH);
  digitalWrite(steppin, !digitalRead(steppin));
  count++;
  return true; //repeat?
}




void setup() {
  pinMode (camSensor, INPUT); //set cam sensor pin to input mode
  pinMode(dirpin, OUTPUT); // set direction signal pin to output mode
  digitalWrite(dirpin, HIGH); // send a constant HIGH signal to driver indicating direction
  pinMode (steppin, OUTPUT); //set step signal pin to output mode
  timer.every(1000, send_pulse); //call send pulse function every 1000 micros

}


void loop() 
{
  timer.tick(); // tick the timer

}

Quite some technical information, but…. What RPMs do You wish and what do You get?
Hopefully You know more than I do about Arduinos but for ne the code   digitalWrite(steppin, !digitalRead(steppin)); looks not all certain. Maybe it is.
Which Arduino are You using?

How about:

void loop() 
{
if(count)
  {
  timer.tick(); // tick the timer
  }
else if(digitalRead(camSensor)==HIGH)
  {
  count=steps;
  }
}

Not tested. Not even compiled.
Send_pulse will have to decrement count instead of incrementing

If you just need to create a pulse every 2000 microsecs then you can easily do that with code like in the second example in this Simple Stepper Code

Note that I said 2000 because as far as I can see your existing code changes from HIGH to LOW to HIGH at 1000µsec intervals. Most stepper drivers just need a short pulse of about 10µsecs (or less) and then the interval between pulses controls the speed.

To produce a train of pulses when an input is triggered you could have code something like this pseudo code

void loop() {
  if  (input == LOW and prevInput == HIGH and stepCount == 0) { // assumes LOW when triggered
     stepCount = numberOfsteps;
  }

  if (stepCount > 0) {
     moveOneStep()
  }
}

void moveOneStep() {
   if (micros() - prevStepMicros >= stepIntervalMicros) {  
     prevStepMicros = micros();
     digitalWrite(stepPin, HIGH);
     digitalWrite(stepPin, LOW);
     stepCount --;
  }
}