looping 1 time too many


I am trying to figure out my bug in this program, i am sure it is very obvious but for the life of me i cant see it.

I am looking to read a slotted optical sensor every 10 steps of stepper motor, if it sees change from the last state the motor keeps turning, if not the motors stops. I have the serial output writing the step count and i am using a pushbutton to manually simulate state change of the optical sensor.

What i have almost works but it sometimes wants to loop through an extra 10 count before stopping. i think there is a small logic in program.


//make the comb "home" using the optical sensor and the stepper motor. If the optical sensor hasn't changed state in 10 (FULLSTEP) steps of the stepper motor
//then stop the motor will reverse to find edge of the comb.

const int dir = 4;//index motor
const int Step = 5;//index motor
const int slp = 6;//index motor
const int  optoPin = A4;    // the pin that the opto is attached to changed from Pin 7 for testing purposes. Push button used in testing
int cycleButtonState;//homing button state
const int cycleButt = A3;//initate homing cycle
const int dt = 100;//index motor step delay
unsigned int stepCount;//step counter
int laststepcount;//previous step count
const int stepinterval = 10;//frequency of opto read
int motorState = 1;//flag for exiting loops
int optoState ;         // current state of the opto
int newoptoState ;     // new state of the opto

void setup() {
  // initialize the opto pin as a input:
  pinMode(optoPin, INPUT_PULLUP);
  // initialize the LED as an output:

  pinMode (dir, OUTPUT);
  pinMode(Step, OUTPUT);
  pinMode(slp, OUTPUT);
  pinMode(cycleButt, INPUT_PULLUP);
  digitalWrite(slp, HIGH);

  // initialize serial communication:

  optoState = digitalRead(optoPin);
  cycleButtonState = digitalRead(cycleButt);
  Serial.print ("Cycle Button State: ");
  Serial.println (cycleButtonState);
  Serial.print ("Optical State: ");
  Serial.println (optoState);
  Serial.print ("NEW Optical State: ");
  Serial.println (newoptoState);


void loop() {  // read the opto input pin:
  optoState = digitalRead(optoPin);
  cycleButtonState = digitalRead(cycleButt);

  //-----------------------------------------------------------------------------------------Comb Homing
  if (cycleButtonState == LOW) //push button to start the cycle
  { optoState = digitalRead(optoPin);//read slotted optical sensor/push button
    while (motorState == 1) {

      IndexMotor();//start motor and increase stepcount++
      Serial.print ("Step Count: ");
      Serial.println (stepCount);

      while (stepCount == 10) {
        newoptoState = digitalRead(optoPin);
        if (optoState != newoptoState) {
          stepCount = 0;
          optoState = newoptoState;
        } else {
          Serial.println ("HOME");
          motorState = 0;


void IndexMotor()

  digitalWrite(Step, HIGH);
  digitalWrite(Step, LOW);



The classic way to find the bug in a program is to use Serial.print() to display the values used in various parts of your program, so you can trace the logic and see where the bug is. And then fix it.