Stepper Motor control with sensor interrupts

Hello,
I am fairly new to arduino and electronics, I am trying to move a stepper motor based on the serial input. Below you can find the code I wrote for testing, I built my code on the Adafruit dual sensor example. Ultimately, I am trying to achieve continous movement of 2 stepper motors and interrupt the movement when the distance the sensor reads is equal to the distance from the serial. Is this achievable with stepper motors? If not, I am open to any suggestions?


#include "Adafruit_VL53L0X.h"

// address we will assign if dual sensor is present
#define LOX1_ADDRESS 0x30
#define LOX2_ADDRESS 0x31

// set the pins to shutdown
#define SHT_LOX1 7
#define SHT_LOX2 6
#define xdirPin 3
#define xStep 2

// objects for the vl53l0x
Adafruit_VL53L0X lox1 = Adafruit_VL53L0X();
Adafruit_VL53L0X lox2 = Adafruit_VL53L0X();

// this holds the measurement
VL53L0X_RangingMeasurementData_t measure1;
VL53L0X_RangingMeasurementData_t measure2;
const int stepsPerRev = 200;




void setID() {
  // all reset
  digitalWrite(SHT_LOX1, LOW);
  digitalWrite(SHT_LOX2, LOW);
  delay(10);
  // all unreset
  digitalWrite(SHT_LOX1, HIGH);
  digitalWrite(SHT_LOX2, HIGH);
  delay(10);

  // activating LOX1 and resetting LOX2
  digitalWrite(SHT_LOX1, HIGH);
  digitalWrite(SHT_LOX2, LOW);

  // initing LOX1
  if (!lox1.begin(LOX1_ADDRESS)) {
    Serial.println(F("Failed to boot first VL53L0X"));
    while (1);
  }
  delay(10);

  // activating LOX2
  digitalWrite(SHT_LOX2, HIGH);
  delay(10);

  // initing LOX2
  if (!lox2.begin(LOX2_ADDRESS)) {
    Serial.println(F("Failed to boot second VL53L0X"));
    while (1);
  }
}

int read_x_sensor() {
  lox1.rangingTest(&measure1, false); // pass in 'true' to get debug data printout!
  // print sensor one reading
  if (measure1.RangeStatus != 4) { // if not out of range
    return measure1.RangeMilliMeter;
  } else {
    return -1; // error value
  }
}

void initApp() {
  pinMode(SHT_LOX1, OUTPUT);
  pinMode(SHT_LOX2, OUTPUT);

  Serial.println(F("Shutdown pins inited..."));

  digitalWrite(SHT_LOX1, LOW);
  digitalWrite(SHT_LOX2, LOW);

  Serial.println(F("Both in reset mode...(pins are low)"));

  Serial.println(F("Starting..."));
}

void setup() {
  Serial.begin(115200);
  // wait until serial port opens for native USB devices
  while (!Serial) { delay(1); }
  pinMode(xStep,OUTPUT); 
  pinMode(xdirPin,OUTPUT);
  initApp();
  setID();
}

void loop() {
  static int distance = 0;

  // Check if there is serial input
  if (Serial.available() > 0) {
    targetDistance = Serial.parseInt();
  }

  distance = read_x_sensor();

  if (distance != -1) { // if a valid reading is obtained
    if (distance > targetDistance) {
      digitalWrite(xdirPin,LOW); 
       for(int x = 0; x < 5; x++) {
    digitalWrite(xStep,HIGH);
    delayMicroseconds(500);
    digitalWrite(xStep,LOW);
    delayMicroseconds(500);
  }// 
    } else  if (distance < targetDistance) {
      digitalWrite(xdirPin,HIGH); 
       for(int x = 0; x < 20; x++) {
    digitalWrite(xStep,HIGH);
    delayMicroseconds(500);
    digitalWrite(xStep,LOW);
    delayMicroseconds(500);
  }// 
    }
  } else {
    Serial.println("Error reading sensor.");
  }

  delay(100);
}

Yes, continuous motion of two steppers towards a target distance is achievable, but not with delay-based coding.

Look into Serial Input Basics - updated to eliminate pauses due to Serial.parseInt().

Look into Demonstration code for several things at the same time for eliminating pauses due to delayMicroseconds(500); and for loops filled with delayMicroseconds(500);

Here's a demo of six steppers moving continuously to targets with non-blocking coding:

I will look into these thanks!

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