Loop Function / AccelStepper Help!

Hello,

I’m developing a code that involves an IR Break Sensor Beam and a Stepper motor. My stepper motor driver is a TB6600. From research it seems I need the AccelStepper library to get it to move. I have very little programming skills, but I’m trying. The goal is to rotate the stepper motor 60 deg (33 steps) upon tripping the sensor. Once the laser is tripped I want it to cut off until the stepper motor reaches its destination (33rd step). Then, I would like the motor to keep still until the laser is tripped again in which it’ll turn off until the stepper motor rotates another 60 deg. I’d like this to happen until the stepper has completed a full 360 deg. I’ve displayed the code below. I can’t seem to figure out how to turn the laser off while the stepper is in motion. I was wondering if anyone could help me with this. Also, the stepper seems to be going all over the place. It’ll jump back and forth. Coding issue?

#include <AccelStepper.h>

AccelStepper stepper (1, 8, 9);

#define LED 2
#define SENSORPIN 3


int sensorState = 0, lastState = 0;
int x = 0;

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(SENSORPIN, INPUT_PULLUP);

  digitalWrite(SENSORPIN, HIGH); // turn on the pullup (May need to change.)

  Serial.begin(9600);

  stepper.setMaxSpeed(100);
  stepper.setAcceleration(100);
}

void loop()
{
  stepper.runSpeedToPosition();

  sensorState = digitalRead(SENSORPIN);

  if (sensorState != lastState)
  {
    if (sensorState == LOW)
    {
      x++;
      Serial.println(x);

      if (x == 3 || x == 6)
      {
        stepper.setCurrentPosition(0);


        if (stepper.currentPosition() != 0 || stepper.currentPosition() != 34)
        {
          digitalWrite(SENSORPIN, LOW);
        }
        else
        {
          digitalWrite(SENSORPIN, HIGH);
        }

        stepper.moveTo(34);
      }
      else
      {
        stepper.setCurrentPosition(0);


        if ( stepper.currentPosition() != 0 || stepper.currentPosition() != 33)
        {
          digitalWrite(SENSORPIN, LOW);
        }
        else
        {
          digitalWrite(SENSORPIN, HIGH);
        }

        stepper.moveTo(33);
      }
      if (x >= 7)
      {
        stepper.stop();
      }
    }
  }

  delay(200);

  if (sensorState && !lastState)
  {
    Serial.println("Unbroken");
  }
  if (!sensorState && lastState)
  {
    Serial.println("Broken");
  }
  lastState = sensorState;
}

This is a good way to write a loop() function...

void loop() {
  readInputs();
  doCalculations();
  runMotor();
}

Your loop appears to do this backwards, running the motor first. There's also a strange delay(200) in the middle that will be hurting you.

Read up on state machines here: http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html, written by one of the local gurus here.

MorganS:
This is a good way to write a loop() function…

void loop() {

readInputs();
  doCalculations();
  runMotor();
}




Your loop appears to do this backwards, running the motor first. There's also a strange delay(200) in the middle that will be hurting you.

Read up on state machines here: http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html, written by one of the local gurus here.

Thank you for the response. I figured something was out of wack here. The delay(200) was to prevent debouncing of the sensor, but there’s a very good chance I did it wrong or made it too long. I will take a look at the link provided.