Controlling Stepper Motor and Running Sensor Simultaneously

Hi Everyone,

I am new to arduino and coding and am working on a project to control a stepper motor and run a laser sensor at the same time to collect data on the number of times the laser is triggered during a revolution. Currently my code is the basic starter code for the stepper motor but am confused on configuring a while loop to collect the sensor information at the same time the motor is running. This is what I have at this moment.

#include <Stepper.h>
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int inPin = 7;
int val = 0;
int count = 0;
int i = 0;

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  myStepper.setSpeed(60); // set speed to 60 rpm
  pinMode(inPin, INPUT);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);
  
  for (i = 0; i < stepsPerRevolution; i++){
    val = digitalRead(inPin);
    if (val == 1){
      count++;
    }
    if(i == stepsPerRevolution){
    // Return to original position
    Serial.println("Testing Complete. Returing to Original Position");
    myStepper.step(-stepsPerRevolution);
    break;
    }
  }
}

IS there a question hidden somewhere?

jasonlo17:
and run a laser sensor at the same time to collect data on the number of times the laser is triggered during a revolution.

What is going to trigger the laser?
What is the purpose of counting the number of times it is triggered per revolution?

The standard Stepper library blocks the Arduino until it completes all the steps.

The AccelStepper library has the non-blocking run() and runSpeed() functions.

...R

As Robin said you need a better library with a stronger kung-fu for stepper control.

But also kindly tell us if you want to read sensor data synchronised ith ... say... motor steps, or is the sensor data independent of whatever the motor is doing ?
The coding depends on that alot