SOLVED - Shrink Tubing Cutter programming question

Hey guys,

I am building a shrink tubing cutter for cutting multiple equal lengths of tubing with two stepper motors. It will first cycle the cutter and then cycle the feeder. The problem I am having is in the feeder cycle (feeder function at the end of the code). Once it cycles the feeder it should stop. The problem is that when I add:

if (feederStop == tubeLength) {
      feederStart = 0;
    }

it won’t cycle the stepper for the allotted time. It goes immediately to the end of the for loop. I have tried moving this piece of code to several places, but it either ignores it, or it goes right to the end of the for loop. I’m not sure what I am missing. It shouldn’t go to the end of the for loop until i is equal to the tubeLength.

Any thoughts would be greatly appreciated!

// ************************************************************************************
// LIBRARIES
// ************************************************************************************


#include <arduino.h>
#include <analogWrite.h> //allows the esp32 to use the analogWrite function
#include "BluetoothSerial.h"


// ************************************************************************************
// BLUETOOTH
// ************************************************************************************

// Check if Bluetooth configs are enabled
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

// Bluetooth Serial object
BluetoothSerial SerialBT;



// ************************************************************************************
// DEFINE PINS
// ************************************************************************************

//Onboard LED
#define LED_PIN 2

//Stepper MOTOR Right Pins
#define RIGHT_ENABLE 32
#define RIGHT_DIRECTION 33
#define RIGHT_STEP 25

//Stepper MOTOR Left Pins
#define LEFT_ENABLE 26
#define LEFT_DIRECTION 27
#define LEFT_STEP 13

// define LED strip pins
#define RED_LED_PIN 22
#define GREEN_LED_PIN 21
#define BLUE_LED_PIN 23

//sensor
#define SENSOR 34

// ************************************************************************************
// VARIABLES
// ************************************************************************************

// Handle received and sent messages
byte incomingValue = 100;

//timer stuff for steppers without delay
unsigned long previousMicros1 = 0;        // will store last time LED was updated
const long speedRight = 1500;  //stepper speed
unsigned long previousMicros2 = 0;        // will store last time LED was updated
const long speedLeft = 1500;  //stepper speed
int setByte = 0;
bool startButton = 0;
bool feederStart = 0;
int sensorRead = 0;
int sensorRead2 = 0;
bool sensorFlag = 0;
bool timerFlagClosed = 0;
bool chopperStart = 0;
int numberOfTubes = 0;
int tubeLength = 2000;
int feederStop = 0;
int k = 0; //number of tubes for loop counter
int i = 0; //length for loop counter


// ************************************************************************************
// SETUP
// ************************************************************************************

void setup() {

  // Bluetooth device name and Serial setup
  Serial.begin(115200);
  SerialBT.begin("Shrink Tube Cutter");
  Serial.println("The device started, now you can pair it with bluetooth!");


  //LED strip output mode
  pinMode(RED_LED_PIN, OUTPUT); //set LED pins as outpute
  pinMode(GREEN_LED_PIN, OUTPUT);
  pinMode(BLUE_LED_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);  //flash onboard LED

  //Stepper motor right output mode
  pinMode(RIGHT_ENABLE, OUTPUT); //Enable
  pinMode(RIGHT_STEP, OUTPUT); //Step
  pinMode(RIGHT_DIRECTION, OUTPUT); //Direction
  digitalWrite(RIGHT_ENABLE, LOW); //enable needs to be HIGH for stepper to move

  //Stepper motor left output mode
  pinMode(LEFT_ENABLE, OUTPUT); //Enable
  pinMode(LEFT_STEP, OUTPUT); //Step
  pinMode(LEFT_DIRECTION, OUTPUT); //Direction
  digitalWrite(LEFT_ENABLE, LOW); //enable needs to be HIGH for stepper to move

  //sensor
  pinMode(SENSOR, INPUT);

  //Stuff to run on start-up
  analogWrite(RED_LED_PIN, 0); //set the led strip to off on startup
  analogWrite(GREEN_LED_PIN, 0);
  analogWrite(BLUE_LED_PIN, 0);
}



// ************************************************************************************
// MAIN LOOP
// ************************************************************************************

void loop() {

  App();
  if (startButton == 1) {
    //for (k = 0; k <= numberOfTubes; k++) {
    chopper();
    feeder();
  }
  //if (k == numberOfTubes) {
  // startButton = 0;
  // }
  //}

}




// ************************************************************************************
// APP TASKS
// ************************************************************************************

void App() {

  // Read received messages
  if (SerialBT.available()) {

    delay(7);
    setByte = SerialBT.read();  //byte that determines which function is called
    incomingValue = SerialBT.read();
    Serial.println(setByte);

    if (setByte == 3) {  // receiving a 3 starts chopper-------------------------------------------------
      startButton = 1;
      timerFlagClosed = 1;
      numberOfTubes = 5;
      feederStart = 1;
    }

    else if (setByte == 2) { // receiving a 2 disables machine---------------------------------------------
      digitalWrite(LED_PIN, LOW);
      digitalWrite(RIGHT_ENABLE, LOW);
      digitalWrite(LEFT_ENABLE, LOW);
      startButton = 0;
    }
    else if (setByte == 0) { // receiving a 3 turns the LEDs on--------------------------------------------
      SerialBT.write('3');
      analogWrite(RED_LED_PIN, incomingValue);

    }
    delay(10);
  }
}



// ************************************************************************************
// TUBING CHOPPER CYCLE
// ************************************************************************************
void chopper() {
  unsigned long currentMicros1 = micros();
  digitalWrite(RIGHT_ENABLE, HIGH); //enable needs to be low for stepper to move
  //digitalWrite(LEFT_ENABLE, HIGH); //enable needs to be low for stepper to move
  if (timerFlagClosed == 1) {
    for (int j = 0; j < 70; j++) { //ignore the sensors for a brief period
      digitalWrite(RIGHT_DIRECTION, LOW); //sets motor counterclockwise direction for right door
      digitalWrite(RIGHT_STEP, HIGH); //right stepper motor moves counterclockwise
      delayMicroseconds(450);
      digitalWrite(RIGHT_STEP, LOW);
      delayMicroseconds(450);
    }
  }
  timerFlagClosed = 0;
  //sensors are back online again
  sensorRead = digitalRead (SENSOR); //check status of the sensors
  digitalWrite(RIGHT_DIRECTION, LOW); //sets counterclockwise motor direction for right door
  if (sensorRead == LOW) { //checks sensor state of right door open sensor
    if (currentMicros1 - previousMicros1 >= speedRight) {
      previousMicros1 = currentMicros1;
      digitalWrite(RIGHT_STEP, HIGH); //right stepper motor moves clockwise
    } else {
      digitalWrite(RIGHT_STEP, LOW);
    }
  }
}



// ************************************************************************************
// TUBING FEEDER CYCLES
// ************************************************************************************
void feeder() {
  unsigned long currentMicros2 = micros(); //set timer2 perameters
  digitalWrite(LEFT_ENABLE, HIGH); //enable needs to be low for stepper to move
  digitalWrite(LEFT_DIRECTION, LOW); //sets counterclockwise motor direction for left door
  sensorRead2 = digitalRead (SENSOR); //check status of the sensors
  if (feederStart == 1 && sensorRead2 == 1) {
    for (i = 0; i <= tubeLength; i++) {
      //feeder motor operation /////////
      feederStop = i;
      if (currentMicros2 - previousMicros2 >= speedLeft) { //check timer1 to see if it's time to step
        previousMicros2 = currentMicros2;
        digitalWrite(LEFT_STEP, HIGH); //right stepper motor moves clockwise
      } else {
        digitalWrite(LEFT_STEP, LOW);
      }
    }
    if (feederStop == tubeLength) {
      feederStart = 0;
    }
    Serial.println(feederStart);
    Serial.println(feederStop);
  }
}

Tubing Cutter.jpg

Your code is a bit confusing. In your feeder() function

    for (i = 0; i <= tubeLength; i++) {
      //feeder motor operation /////////
      feederStop = i;
      if (currentMicros2 - previousMicros2 >= speedLeft) { //check timer1 to see if it's time to step
        previousMicros2 = currentMicros2;
        digitalWrite(LEFT_STEP, HIGH); //right stepper motor moves clockwise
      } else {
        digitalWrite(LEFT_STEP, LOW);
      }
    }

This code will iterate i from 0 to tubeLength in a heartbeat and you will never get any steps. It appears you are not properly stepping your motor. The first time through this loop, the time check will be true but each pass after that will be almost instant so it will never be true again.

Take a look at the stepper library examples.

It didn’t work because of the for loop and also because there is no delay. The for loops first check the condition statement then executes the body of the loop. So in you case the condition statement isi <= tubeLength
Here in this loop, when ‘i’ is equal to tubelength, it iterates once more and now the ‘i’ is not equal to tubelength (i.e i= tubelength + 1) so the condition statement is false and for loop stops, at this stage the ‘i’ is 1 more than the tubelength so when you check it in the if statement it gives false and doesn’t execute the statement.so the solution is to keep the whole if part inside the for loop or modify the if statement as

if (feederStop == tubeLength+1) {
      feederStart = 0;
    }

Thanks,Akash

Thanks for the replies! You guys have been very helpful! I just went with delays and it is working great now. Since nothing else has to happen during that time, the delay function will suffice.

void feeder() {
  digitalWrite(LEFT_ENABLE, HIGH); //enable needs to be low for stepper to move
  digitalWrite(LEFT_DIRECTION, LOW); //sets counterclockwise motor direction for left door
  sensorRead2 = digitalRead (SENSOR); //check status of the sensors
  if (feederStart == 1 && sensorRead2 == 1) {
    for (i = 0; i <= tubeLength; i++) {
      //feeder motor operation /////////
      feederStop = i;
      digitalWrite(LEFT_STEP, HIGH); //right stepper motor moves counterclockwise
      delayMicroseconds(450);
      digitalWrite(LEFT_STEP, LOW);
      delayMicroseconds(450);
    }
    if (feederStop == tubeLength) {
      feederStart = 0;
    }
    Serial.println(feederStart);
    Serial.println(feederStop);
  }
}

m_k_akash:
It didn’t work because of the for loop and also because there is no delay. The for loops first check the condition statement then executes the body of the loop. So in you case the condition statement isi <= tubeLength
Here in this loop, when ‘i’ is equal to tubelength, it iterates once more and now the ‘i’ is not equal to tubelength (i.e i= tubelength + 1) so the condition statement is false and for loop stops, at this stage the ‘i’ is 1 more than the tubelength so when you check it in the if statement it gives false and doesn’t execute the statement.so the solution is to keep the whole if part inside the for loop or modify the if statement as

if (feederStop == tubeLength+1) {

feederStart = 0;
    }




Thanks,Akash

That is not true. Inside the for() loop, feederStop is assigned to i so the fact that i is one more than tubelength is irrelevant. The comparison does not involve the variable ‘i’

Since your program runs in a very linear fashion, you have a lot of extra variables and things cluttering up the code. Basically, you just need to run cutter() and then feeder() until you have the correct number of tubes

// ************************************************************************************
// LIBRARIES
// ************************************************************************************


#include <arduino.h>
#include <analogWrite.h> //allows the esp32 to use the analogWrite function
#include "BluetoothSerial.h"


// ************************************************************************************
// BLUETOOTH
// ************************************************************************************

// Check if Bluetooth configs are enabled
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

// Bluetooth Serial object
BluetoothSerial SerialBT;

// ************************************************************************************
// DEFINE PINS
// ************************************************************************************

//Onboard LED
#define LED_PIN 2

//Stepper MOTOR Right Pins
#define RIGHT_ENABLE 32
#define RIGHT_DIRECTION 33
#define RIGHT_STEP 25

//Stepper MOTOR Left Pins
#define LEFT_ENABLE 26
#define LEFT_DIRECTION 27
#define LEFT_STEP 13

// define LED strip pins
#define RED_LED_PIN 22
#define GREEN_LED_PIN 21
#define BLUE_LED_PIN 23

//sensor
#define SENSOR 34

// ************************************************************************************
// VARIABLES
// ************************************************************************************

// Handle received and sent messages
byte incomingValue = 100;

int setByte = 0;
bool startButton = false;
int numberOfTubes = 0;
int tubeCount;

const int tubeLength = 2000;

// ************************************************************************************
// SETUP
// ************************************************************************************

void setup() {

  // Bluetooth device name and Serial setup
  Serial.begin(115200);
  SerialBT.begin("Shrink Tube Cutter");
  Serial.println("The device started, now you can pair it with bluetooth!");


  //LED strip output mode
  pinMode(RED_LED_PIN, OUTPUT); //set LED pins as outpute
  pinMode(GREEN_LED_PIN, OUTPUT);
  pinMode(BLUE_LED_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);  //flash onboard LED

  //Stepper motor right output mode
  pinMode(RIGHT_ENABLE, OUTPUT); //Enable
  pinMode(RIGHT_STEP, OUTPUT); //Step
  pinMode(RIGHT_DIRECTION, OUTPUT); //Direction
  digitalWrite(RIGHT_ENABLE, LOW); //enable needs to be HIGH for stepper to move

  //Stepper motor left output mode
  pinMode(LEFT_ENABLE, OUTPUT); //Enable
  pinMode(LEFT_STEP, OUTPUT); //Step
  pinMode(LEFT_DIRECTION, OUTPUT); //Direction
  digitalWrite(LEFT_ENABLE, LOW); //enable needs to be HIGH for stepper to move

  //sensor
  pinMode(SENSOR, INPUT);

  //Stuff to run on start-up
  analogWrite(RED_LED_PIN, 0); //set the led strip to off on startup
  analogWrite(GREEN_LED_PIN, 0);
  analogWrite(BLUE_LED_PIN, 0);
}


// ************************************************************************************
// MAIN LOOP
// ************************************************************************************

void loop() {

  App();
  if (startButton == 1) {
    Serial.print("Run " );
    Serial.print( tubeCount+1);
    Serial.print( " of " );
    Serial.println( numberofTubes );
    chopper();
    feeder();
    tubeCount++;
    if (tubeCount >= numberOfTubes) {
      Serial.pprintln("complete.");
      startButton = 0;
      digitalWrite(LED_PIN, LOW);
      digitalWrite(RIGHT_ENABLE, LOW);
      digitalWrite(LEFT_ENABLE, LOW);
    }
  }
}


// ************************************************************************************
// APP TASKS
// ************************************************************************************

void App() {

  // Read received messages
  if (SerialBT.available()) {

    delay(7);
    setByte = SerialBT.read();  //byte that determines which function is called
    incomingValue = SerialBT.read();
    Serial.println(setByte);

    if (setByte == 3) {  // receiving a 3 starts chopper-------------------------------------------------
      startButton = 1;
      numberOfTubes = 5;
      tubeCount = 0;
      Serial.println("Running...");
    }

    else if (setByte == 2) { // receiving a 2 disables machine---------------------------------------------
      digitalWrite(LED_PIN, LOW);
      digitalWrite(RIGHT_ENABLE, LOW);
      digitalWrite(LEFT_ENABLE, LOW);
      startButton = 0;
    }
    else if (setByte == 0) { // receiving a 3 turns the LEDs on--------------------------------------------
      SerialBT.write('3');
      analogWrite(RED_LED_PIN, incomingValue);
    }
    delay(10);
  }
}


// ************************************************************************************
// TUBING CHOPPER CYCLE
// ************************************************************************************
void chopper() {
  digitalWrite(RIGHT_ENABLE, HIGH); //enable needs to be low for stepper to move
  digitalWrite(RIGHT_DIRECTION, LOW); //sets counterclockwise motor direction for right door
  for (int i = 0; i < 70; i++) { //ignore the sensors for a brief period
    digitalWrite(RIGHT_STEP, HIGH); //right stepper motor moves counterclockwise
    delayMicroseconds(450);
    digitalWrite(RIGHT_STEP, LOW);
    delayMicroseconds(450);
  }
  //sensors are back online again
  // loop until sensor trips
  while ( digitalRead(SENSOR) == LOW ) {
    delayMicroseconds(450);
    digitalWrite(RIGHT_STEP, LOW);
    delayMicroseconds(450);
  }
}


// ************************************************************************************
// TUBING FEEDER CYCLES
// ************************************************************************************
void feeder() {
  digitalWrite(LEFT_ENABLE, HIGH); //enable needs to be low for stepper to move
  digitalWrite(LEFT_DIRECTION, LOW); //sets counterclockwise motor direction for left door
  if (digitalRead(SENSOR) == HIGH) {
    for (int i = 0; i <= tubeLength; i++) {
      //feeder motor operation /////////
      digitalWrite(LEFT_STEP, HIGH); //right stepper motor moves counterclockwise
      delayMicroseconds(450);
      digitalWrite(LEFT_STEP, LOW);
      delayMicroseconds(450);
    }
  }
}