NEED HELP Updating LCD with IR sensor without interrupting a RUNNING(NEMA23)

The title speaks for itself, It might be simple for most of you but I am really in a stump here.
Basically its a sorting conveyor belt, but only limited to an IR sensor placed at the end of the conveyor and an LCD that updated by the IR that counts the objects that roll through the sensor, Pretty simple concept right????
SO WHY CANT I GET IT??? I have a code that updates the LCD and runs the motor , but when i try to run them at the same time the motor runs and the ir updates the lcd after sensing 3 or more objects so its delayed, PLEASE PLEASE HELP ME
L, M and R are the options, it basically has an input from the lcd and is reduced 1 by one and places them according to how many is placed at each option, so its mostly a counter+sorter

what i am having a problem with is sensing with the IR and the same time running the motor- thats it .. :frowning:

unsigned long currentTime = micros();
if (currentTime - previousTime > timeInterval) {
previousTime = currentTime;

/// SENSOR code
IRState = digitalRead(SensorPin7);
if ( IRpast == LOW && IRState == HIGH)
lcd.print(" ");
else if(L==0 && M!=0){

lcd.print(" ");

else if(M==0 && L==0 && R!=0){

lcd.print(" ");

IRpast = IRState;

A few things, first you posted code without using code tags. The use and reasons for that are covered in the sticky threads at the top of the forum. Second, it's bad style to use single letter variable names, use meaningful names instead (gobs of blank white lines are also frowned on). Third, you didn't give a detailed problem description, just "so many things go wrong". That leaves us guessing. Fourth, you didn't post your entire sketch.

Hi, sorry about that , how do i go about editing my post ? id also like to post my whole code but i think its too long,
I was thinking the wrong thing i did was basically with the order of my code or my use of the micros function.
I don't really get how to run two things at the same time :frowning:

I'm not asking you for details of what you think is wrong. I'm asking you what actually happens when you run the sketch? What specifically does not happen that should, or vice versa.

If your code is too big to post, you can add it as an attachment. Here is some help with "two things at one time.."

  1. open your code in the Arduino-IDE
  2. press Ctrl.t for autoformatting your code
  3. do a right-click with your mouse and choose "copy for forum"
  4. press-alt-tab to change back to your browser
  5. press ctrl-v to insert the clipboard-content into the "write-post-field"
  6. add a detailed description what your code does and how it deviates from what you like the code to do

stepper-motors need a very constant timing except for acceleration / decceleration.
But even for acc./decc. the timing is well calculated.

Writing to a display needs too much time for the regular step-pulse-creation.

How the solution looks like highly depends on the stepper-library you are using.
This is just one reason why you should always post the whole sketch.

One solution is to create the step-pulses with a timer-interrupt.
This means you setup the amount of steps that should be created and then let do the timer-interrupt create the steps.

right now your display-printing is interrupting your stepper-motor.
When using a ISR for creating the step-pulses the ISR is interrupting the display-print which is not a problem.

Some stepper-libraries work internal this way. But in can be written with a quite small amount of code without using a library too

best regards Stefan