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;
}
}
}