Making stepper run till next object is found

I have this code and it is working good except 1 thing. Once an object is by the IR sensor it doesn't start the stepper again. I need to start the stepper again and stop when the next object is detected. I have tried to set a variable. but am having no luck.

#include <AccelStepper.h>
#include <Bounce2.h>

#define NUM_BUTTONS 2

const int dirPin = 4;
const int stepPin = 5;
const int motorInterfaceType = 1;

bool start = false;

const int buttonPin[NUM_BUTTONS] = {2, 3};

AccelStepper myStepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

Bounce2::Button button[NUM_BUTTONS] = {Bounce2::Button()};

int irPin = 6;
int sensorOut = HIGH;

int RelayControl = 7;

int steprun = 0;

void setup()
{
  Serial.begin(9600);

  for (byte i = 0; i < NUM_BUTTONS; i++) {
    button[i].attach(buttonPin[i], INPUT_PULLUP);
    button[i].setPressedState(LOW);
    button[i].interval(5);
  }
  myStepper.setMaxSpeed(1000);

  pinMode(irPin, INPUT);

  pinMode(RelayControl, OUTPUT);
}

void loop()
{
  sensorOut = digitalRead(irPin);

  if (start == true) {
    myStepper.setSpeed(1000);
    myStepper.runSpeed();
  } else {
    myStepper.stop();
  }

  if (sensorOut == LOW && steprun == 1) {
    start = false;
    digitalWrite(RelayControl,HIGH); 
    delay(2000);
    digitalWrite(RelayControl,LOW); 
    delay(2000);
    start = true;
  }

  for (byte i = 0; i < NUM_BUTTONS; i++) {
    button[i].update();

    if (button[0].pressed() ) {
      start = true;
      steprun = 1;
    } else if (button[1].pressed() ) {
      start = false;
      steprun = 0;
    }
  }
}

Specify "no luck".

Actually I just figured it out. Learning all this is tough. With a little help can get things to work. Paul when you do not know something and are learning luck plays a huge role in things. After trying multiple ways and reading a lot i found by using boolean to set a true or false and putting them state in the right location I was able to achieve what i wanted.

#include <AccelStepper.h>
#include <Bounce2.h>

#define NUM_BUTTONS 2

const int dirPin = 4;
const int stepPin = 5;
const int motorInterfaceType = 1;

bool start = false;

const int buttonPin[NUM_BUTTONS] = {2, 3};

AccelStepper myStepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

Bounce2::Button button[NUM_BUTTONS] = {Bounce2::Button()};

int irPin = 6;
int sensorOut = HIGH;

int RelayControl = 7;

int steprun = 0;
boolean state = true; 

void setup()
{
  Serial.begin(9600);

  for (byte i = 0; i < NUM_BUTTONS; i++) {
    button[i].attach(buttonPin[i], INPUT_PULLUP);
    button[i].setPressedState(LOW);
    button[i].interval(5);
  }
  myStepper.setMaxSpeed(1200);

  pinMode(irPin, INPUT);

  pinMode(RelayControl, OUTPUT);
}

void loop()
{
  sensorOut = digitalRead(irPin);

  if (start == true) {
    myStepper.setSpeed(1200);
    myStepper.runSpeed();
  } else {
    myStepper.stop();
  }

  if (sensorOut == LOW && steprun == 1 && state) {
    state = false;  
    delay(100); 
    start = false;
    digitalWrite(RelayControl,HIGH); 
    delay(2000);
    digitalWrite(RelayControl,LOW); 
    delay(2000);
    start = true;
  } else if (sensorOut) {
    state = true; 
  }

  for (byte i = 0; i < NUM_BUTTONS; i++) {
    button[i].update();

    if (button[0].pressed() ) {
      //Serial.println("start");
      start = true;
      steprun = 1;
    } else if (button[1].pressed() ) {
      //Serial.println("stop");
      start = false;
      steprun = 0;
    }
  }
}

For shure.

Use a logic analyzer to see what happens.

Insert Serial.println()´s at points of interrest.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.