Automatic wire cutter with servo and stepper motor

can someone please help, what's wrong with this code when i upload this down button sends signal continuously and the loop is not working when i select wire length stepper motor should run longer to maintain wire length and servo motor also should delay in its operation but it is running after every one second.

01_wire_cutter.ino (4.8 KB)
//------------------------------- librarys ----------------------------------
#include <Wire.h>
#include <LiquidCrystal.h>
#include <Servo.h>

//------------------------------- lcd ----------------------------------
LiquidCrystal lcd(12, 11, 2, 3, 4, 5);

//------------------------------- stepper ----------------------------------
#define stepPin 7
#define dirPin 8

//------------------------------- servo ----------------------------------
Servo snippers;
#define servo 10
#define openAngle 180
#define closedAngle 0

//------------------------------- input ----------------------------------

#define leftButton 14
#define rightButton 9
#define upButton 6
#define downButton 15

//------------------------------- user settings ----------------------------------
unsigned int wireLength = 0;
unsigned int wireQuantity = 0;

//------------------------------- system settings ----------------------------------
int state = 0;
int incrementSpeed = 1;
int previousWireLength = 0;
int previousWireQuantity = 0;
float mmPerStep = 0.18096;

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

lcd.begin(16, 2); //LCD columns and rows

pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(leftButton, INPUT_PULLUP);
pinMode(rightButton, INPUT_PULLUP);

pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);

snippers.attach(servo);

snippers.write(openAngle);

delay(1000);
}

void loop() {
if (!digitalRead(rightButton)){
if(state == 5){
state = 0;
}
else{
state += 1;
}
delay(200);
lcd.clear();
}
if (!digitalRead(leftButton) && state > 0 && state < 4){
state -=1;
delay(200);
lcd.clear();
}

switch (state){
case 0:
homeScreen();
break;
case 1:
chooseWireLength();
break;
case 2:
chooseWireQuantity();
break;
case 3:
confirm();
break;
case 4:
currentlyCutting();
break;
case 5:
finishedCutting();
break;
}

}

void homeScreen(){
lcd.setCursor(0, 0);
lcd.print("WIRE CUTTER");
lcd.setCursor(11, 1);
lcd.print("NEXT>");
delay(100);
}

void chooseWireLength(){
wireLength = changeValue(wireLength);

//clear LCD if required
if(previousWireLength != wireLength){
lcd.clear();
previousWireLength = wireLength;
}

//Display information on LCD
lcd.setCursor(0, 0);
lcd.print("LENGTH:" + (String)wireLength + "mm");
displayNavigation();
}

void chooseWireQuantity(){
wireQuantity = changeValue(wireQuantity);

//clear LCD if required
if(previousWireQuantity != wireQuantity){
lcd.clear();
previousWireQuantity = wireQuantity;
}

//Display information on LCD
lcd.setCursor(0, 0);
lcd.print("QUANTITY:" + (String)wireQuantity);
displayNavigation();
}

void confirm(){
lcd.setCursor(0, 0);
lcd.print((String)wireLength + "mm x " + (String)wireQuantity + "pcs");
lcd.setCursor(0, 1);
lcd.print("<BACK");
lcd.setCursor(10, 1);
lcd.print("START>");
delay(100);
}

void currentlyCutting(){
lcd.setCursor(0, 0);
lcd.print((String)0 + "/" + (String)wireQuantity);
lcd.setCursor(0, 1);
lcd.print("???s");
int stepsToTake = (int)wireLength/mmPerStep;
for(int i = 0; i < wireQuantity; i++){
unsigned long timeForOneCycle = millis();
digitalWrite(dirPin,HIGH);
for(int x = 0; x < stepsToTake; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}

lcd.setCursor(0, 0);
lcd.print((String)(i+1) + "/" + (String)wireQuantity);

snippers.write(closedAngle);
delay(600);
snippers.write(openAngle);
delay(600);


lcd.setCursor(0, 1);

unsigned long timeRemaining = ((millis() - timeForOneCycle)*(wireQuantity - (i+1)))/1000;
lcd.print((String)timeRemaining + "s");

}
wireLength = 0;
wireQuantity = 0;
state = 5;
}

void finishedCutting(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CUTTING COMPLETE");
lcd.setCursor(11, 1);
lcd.print("NEXT>");
delay(100);
}

int changeValue(int currentValue ){
if (!digitalRead(upButton)) {
delay(100);

currentValue += incrementSpeed;
incrementSpeed++:

}
if (!digitalRead(downButton)) {
if(currentValue - incrementSpeed >= 0){
delay(100);
currentValue -= incrementSpeed;

}
else{
  currentValue = 0;
}

}
if (!digitalRead(downButton) && !digitalRead(upButton)){
incrementSpeed = 1;
}
return currentValue;
}

void displayNavigation(){
lcd.setCursor(0, 1);
lcd.print("<BACK");
lcd.setCursor(11, 1);
lcd.print("NEXT>");
delay(100);
}

Preformatted text

`

I will look at your code, but in the meantime can you post it properly, please?

Read the forum guidelines to see how to properly post code and some good information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please go back and fix your original post.

Is this your code or is it copied from somewhere?

Please post a schematic. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

2 Likes

First observation, you’re test IF the button is pressed - not WHEN the button is pressed.

Hi,
Your code gives compilation error.

" :" wrong ";" right

try this way:

oid loop() {
  if (!digitalRead(rightButton)) {

    if (state == 5) {
      state = 0;
    }
    else {
      state += 1;
    }
    while (!digitalRead(rightButton)) {}            //  <<<<<<<---------------------
    //delay(200);
    lcd.clear();
  }
......................................

Is the 'down' button pin shorted to ground?

Is your "float mmPerStep = 0.18096;" value correct? Are you entering the desired length in mm?

Are you saying the 'snippers' servo does a cut every second, even if the stepper is in the middle of feeding wire?!?

Yes servo runs every second and even stepper is not feeding properly I think it’s loop coding is wrong.
This code was copied from you tube I am new to Arduino
Thanks for your time

There is no compilation error I think the loop on not functioning properly what I need is when stepper motor is feeding in mm after desired length stepper should stop and allow servo to move for second then stepper runs as per quantity entered.

I am using the same float .18096value the problem is
This coding in not making proper loop it should allow first stepper motor to run as per length entered in mm and after desired length achieved then it should allow servo to make one revolution only then this cycle should continue until desired quantity of wires cut

Hi, @vishalymca
Welcome to the forum.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

The Youtube video: https://www.youtube.com/watch?v=d7VzVV2itAo
The code is on a Google Drive in a *.rar file.
The website of that Youtube channel: https://electricdiylab.com/
The Wire Cutter is only on Youtube, not on the website.

The website itself has a good design. The projects on the website are fantastic. The electronics are good as far as I can tell. However, the code is lacking quality in every sketch that I look at.

Please try this
https://drive.google.com/file/d/1iC4AMHDUVlfjNlICEOi6QLwa36VQQeFK/view?usp=drive_open

It’s same project
But code is lacking in quality
Please help to improve

I tried to make the project in Wokwi. The sketch is still the same. Is it working in a different way than you project ?

Can you show a photo of your project. The Arduino 5V pin is not strong enough for a servo motor or stepper motor.

1 Like

No I am using 12 volt adapter which is enough, both stepper and servo getting enough power working but the loop is not working I don’t know how to fix this cow problem.

If you do the same thing in your project as in the simulation. Where does your project do something different ?

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