Tube Cutter automation with STEPPER MOTOR AND RELAY.

Hi, I want to make a automated tube cutter but I`m having difficulties to do the arduino code.

The idea is to have a stepper motor to feed the cutter machine that will be controlled by a relay connected to the arduino.

The cutter machine is controlled by a pneumatic system and is currently controlled by a switch and I want it to be automatic.

I have already built the CIRCUIT SCHEMATICS as ATTACHED (The arduino is with a prototype shield and the LED connected to the relay represents the cutter machine).

PLEASE HELP ME TO CREATE A CODE TO DO AS FOLLOW:

1: Start the loop when the button from the prototype shield is pushed.

2: Run the stepper motor for a chosen number of steps, speed and acceleration. (the acceleration is to prevent the tube from slipping).

3: Stop the stepper motor.

4: Trigger the relay so that the cutter machine can cut the tube.

5: Stay with the relay triggered until the tube is completely cut (approximately 1 second)

6: repeat this loop until the prototype button is pushed again.

OBS: on the circuit schematics that you can see attached the pins are connected as follow:

pin 2: direction
pin 3: steps
pin 10: relay
pin 11: button from prototype shield
pin 12: green LED from prototype shield (will not be used)
pin 13: red LED from prototype shield (will not be used)

Thanks in advance!

1: Start the loop when the button from the prototype shield is pushed.

Does this mean waiting in setup() until some mysterious switch is pressed, and then looping forever?

2: Run the stepper motor for a chosen number of steps, speed and acceleration. (the acceleration is to prevent the tube from slipping).

Chosen randomly?

3: Stop the stepper motor.

After the stepper has stepped the required number of times, that happens automatically.

4: Trigger the relay so that the cutter machine can cut the tube.

How is the relay wired? Normally open? Normally closed? Does HIGH trigger the relay, or LOW?

5: Stay with the relay triggered until the tube is completely cut (approximately 1 second)

How is the Arduino supposed to know when the cutting is complete?

6: repeat this loop until the prototype button is pushed again.

So, you don't want to wait in setup().

Clearly requirements are going to be ESSENTIAL. Yours are anything but.

Hi PaulS.

Just to make it clear and more detailed.

It would be something like this:

IF button = HIGH;

Run the stepper motor for a specific number of steps, speed and acceleration.

(the number of steps will define the length of the tube, I will have to test and adjust the number of steps on the code since I don't have the diameter of the pulley yet) (the acceleration is to prevent the tube from slipping)

Trigger the relay so that the cutter machine can cut the tube. (the relay is normally closed and HIGH trigger the relay)

Stay with the relay triggered with a delay command for around 1 second (this 1 sec is the time for the blade to move down and cut the plastic tube).

After the relay comes back to LOW we need 1 more second for the blade to return to its original position, so that the loop can repeat.(the arduino doesn't know when the tube is finally cut, it will be adjusted with this delay command. I`m assuming 1 sec for the blade moving down and 1 sec for the blade moving up)

ELSE (when button = LOW) Nothing happens

I think I figured out the code.
I`m just waiting for the parts to arrive to test it.

This is my first project and I`m just learning.
Could someone with more knowledge please verify?

#include <AccelStepper.h>
int ButtonA = 11;
int relay = 10;
AccelStepper(1,2,3); //two is step pin
int pos = 400;
int ButtonAState = 0;

void setup()
{
pinMode(11,INPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(10,OUTPUT);
stepper.setMaxSpeed(6000);
stepper.setAcceleration(4000);

digitalWrite(11,HIGH);
digitalWrite(10,LOW);
}

void loop()
{

if (ButtonA == LOW)
{
ButtonAState=1;
}

if (ButtonAState == 1)
{
stepper.runToNewPosition(pos);
delay(500);
digitalWrite(relay,HIGH); //trigger the relay
delay(1000); // waits for a second
digitalWrite(relay,LOW); // turn of the relay
delay(1000); // waits for a second
}
}

int ButtonA = 11;

if (ButtonA == LOW)

I can assure you that 11 is NOT 0, so ButtonAState will never be 1, so the next if statement will never be true, so the Arduino will never do anything.

   if(digitalRead(ButtonA) == LOW) // Assumes the button is sewn on properly
   {
       pos += 400; // Define a new position
       stepper.runToNewPosition(pos); // Wait for the stepper to get there
       delay(500); // Then, stuff your thumb up your ass uselessly
       digitalWrite(relay,HIGH); //trigger the relay
       delay(1000); // waits for a second
       digitalWrite(relay,LOW); // turn of the relay
       delay(1000); // waits for a second
   }

Thanks PaulS, now I think it is correct!

#include <AccelStepper.h>
#define ButtonA 11;
#define relay 10;
AccelStepper(1,2,3); //two is step pin
int pos = 400;

void setup()
{
pinMode(ButtonA,INPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(relay,OUTPUT);
stepper.setMaxSpeed(6000);
stepper.setAcceleration(4000);
}

void loop()
{
if (digitalRead(ButtonA) == LOW)
{
stepper.runToNewPosition(pos);
digitalWrite(relay,HIGH); //trigger the relay
delay(1000); // waits for a second
digitalWrite(relay,LOW); // turn of the relay
delay(1000); // waits for a second
}
}

I also made an upgrade to the code, for this code below I can control the number of tubes to be cut, for this example it will cut 10 pieces. please check this code.

#include <AccelStepper.h>
#define ButtonA 11;
#define relay 10;
AccelStepper(1,2,3); //two is step pin
int pos = 400;
int ButtonAState = 0;
int totalcount = 0;

void setup()
{
pinMode(ButtonA,INPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(relay,OUTPUT);
stepper.setMaxSpeed(6000);
stepper.setAcceleration(4000);
}

void loop()
{
if (digitalRead(ButtonA) == LOW)
{
ButtonAState=1;
}
if (ButtonAState == 1)
{
stepper.runToNewPosition(pos);
digitalWrite(relay,HIGH); //trigger the relay
delay(1000); // waits for a second
digitalWrite(relay,LOW); // turn of the relay
delay(1000); // waits for a second
totalcount++;
}
if (totalcount == 10)
{
ButtonAState=0;
}
}

int tubeCount = 10;

void loop()
{
   if(digitalRead(ButtonA) == LOW)
   {
      for(byte t=0; t<tubeCount; t++)
      {
         CutTube();
      }
   }
}

void CutTube()
{
   pos += 400; // Define a new position
   stepper.runToNewPosition(pos); // Wait for the stepper to get there
   delay(500); // Then, stuff your thumb up your ass uselessly
   digitalWrite(relay,HIGH); //trigger the relay
   delay(1000); // waits for a second
   digitalWrite(relay,LOW); // turn of the relay
   delay(1000); // waits for a second
}

It is VERY important to change the stepper position. If you don’t, the stepper will move exactly once. I do not think that is what you want. I think you want to feed, start cutting, wait, stop cutting, wait, and then repeat the whole process.

Notice, too, how I use code tags (use the </> icon then paste your code.

Notice, too, how I indent code to make it more readable.

ow, now I get the step position. did`nt know this code tags and now it is much more readable.

Now I think it is finally wright! Thanks!

#include <AccelStepper.h>
#define ButtonA 11;
#define relay 10;
AccelStepper(1,2,3); //two is step pin
int tubeCount = 10;

void setup()
{
	pinMode(ButtonA,INPUT);
	pinMode(2,OUTPUT);
	pinMode(3,OUTPUT);
	pinMode(relay,OUTPUT);
	stepper.setMaxSpeed(6000);
	stepper.setAcceleration(4000);
}

void loop() 
{
	if (digitalRead(ButtonA) == LOW)
	{
		for(byte t=0; t<tubeCount; t++)
		{
			CutTube();
		}
	}	
}

void CutTube()
{
	pos += 400; // define a new position
	stepper.runToNewPosition(pos); // wait for the stepper to get there
	digitalWrite(relay,HIGH); //trigger the relay
	delay(1000); // waits for a second
	digitalWrite(relay,LOW); // turn of the relay
	delay(1000); // waits for a second
	totalcount++;
}

Actually the "totalcount++;" on the last line shouldn't`t be there.

Just one more question.
Is this code going to work? to display for example (when cutting the 9th tube):

Tube length = 30mm
Tube count = 9

I wil change for the correct pins

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2); //Change this for your pins

void setup()
{
   lcd.begin(16, 2);
}

void loop()
{
   lcd.setCursor(0,0);
   lcd.print(“Tube length = 30mm”)

   lcd.setCursor(0,1);
   lcd.print(“Tube count = '");
   lcd.print(t, DEC);
}

Was this button going to be a momentary-on bushbutton, or was it to be a switch on/off either is good?

When the button is pushed or the switch is turned off, would you like the device to continue until it has finished its current cycle, or would you like it to stop immediately?

With respect to accelleration/decelleration, it might be easiest to work out timings in a spreadsheet and then copy/paste them into the code. You can use a quintic to manage maximum "jerk". The alternative is to have a bunch of math in the sketch, and that can get a little opaque.

PaulMurrayCbr, the button will be a momentary-on-pushbutton.

It would be good if when pushed again during the cycle it stops the cycle immediately. But I don’t know how to do it.

I will search about timing the acceleration/decelleration in a spreadsheet and using quintic to manage maximum “jerk”.

Thanks!

If I want the acceleration to happen in the first and last 20% of total steps, is this code going to work?

This would help to get a smooth acceleration and deceleration.

For example for a total of 1000 steps:

stepper.setCurrentPosition(0);
pos = 1000;
stepper.runToNewPosition(pos);
while ((stepper.currentPosition()) << 200) // 20 percent of total steps
	{
		stepper.setMaxSpeed(SetMotorSpeed);
		stepper.setAcceleration(SetMotorAcceleration);
		stepper.run();
	}
while ((stepper.currentPosition()) >= 200 <= 800) // 60 percent of total steps
	{
		stepper.setSpeed(SetMotorSpeed);
		stepper.run();
	}
while ((stepper.currentPosition()) >> 800 // 20 percent of total steps
	{
		stepper.setMaxSpeed(SetMotorSpeed);
		stepper.setAcceleration(SetMotorAcceleration);
		stepper.run();
	}