Mm to cm or inches

This IS code (credit DIY Arduino based Automatic wire cutting Machine - YouTube and Arduino automated wire cutter - YouTube).

Please anybody can explain to me how to change wire length mm to cm or inches.

(already try to replace "mm" to "cm" but not working, stepper motor take steps in mm)

//------------------------------- librarys ----------------------------------

#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 15
#define downButton 6

//------------------------------- 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;

}
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);
}

Hi, welcome. Please read How to use the forum. Especially the part about code tags and what YOU have tried so far.

But as an answer:
cm = mm / 10

septillion:
Hi, welcome. Please read How to use the forum. Especially the part about code tags and what YOU have tried so far.

But as an answer:
cm = mm / 10

Thanks for reply

I want to convert mm to cm in code.

1 Like

So, what makes that difficult in code?

cm = mm / 10;[/code
is completely valid code.

So start by changing you post to have code tags. Next, tell us what you tried an why you think that did not work.

I want to convert mm to cm in code.

Given a measurement in mm how would you convert it to cm using pencil and paper, or even a calculator ?

Are you wanting to have the length in whole cm / inches, or do you want to allow fractional amounts?

If only whole numbers, just change all the display text from "mm" to the appropriate "cm" or "in",
then in the wire cutter section, change the line that calculates the stepper motor steps:

//original code
 int stepsToTake = (int)wireLength / mmPerStep;

//code for wireLength in cm
 int stepsToTake = (int)((wireLength * 10) / mmPerStep;)

//code for wireLength in inches
 int stepsToTake = (int)((wireLength * 25.4) / mmPerStep;)

The US has been "converting to the metric system within 10 years" for 50 years now and we have to explain how to convert mm to cm... But everyone is expected to know how many feet are in a mile?

The official definition of an inch is 25.4 mm

...R

The official definition of an inch is 25.4 mm

But its really closer to:
1 / 0.03937 = 25.400050800102 :grin:

JCA79B:
But its really closer to:
1 / 0.03937 = 25.400050800102 :grin:

Not according to my Physics text book

1 inch = 2.54 centimetres (exactly)

...R

david_2018:
Are you wanting to have the length in whole cm / inches, or do you want to allow fractional amounts?

If only whole numbers, just change all the display text from "mm" to the appropriate "cm" or "in",
then in the wire cutter section, change the line that calculates the stepper motor steps:

//original code

int stepsToTake = (int)wireLength / mmPerStep;

//code for wireLength in cm
int stepsToTake = (int)((wireLength * 10) / mmPerStep;)

//code for wireLength in inches
int stepsToTake = (int)((wireLength * 25.4) / mmPerStep;)






Thank you for quick understanding, and thanks for your help it perfectly works.


just one thing is here, an input value is 2inch, output wire size is 1.9inch (as an attachment image)

Hi,

Your wireLength variable is an int,
Try this, it may fix the problem.

//code for wireLength in cm
 int stepsToTake = (int)(((float)wireLength * 10.0) / mmPerStep;)

//code for wireLength in inches
 int stepsToTake = (int)(((float)wireLength * 25.4) / mmPerStep;)

Tom... :slight_smile:

TomGeorge:
Hi,

Your wireLength variable is an int,
Try this, it may fix the problem.

//code for wireLength in cm

int stepsToTake = (int)(((float)wireLength * 10.0) / mmPerStep;)

//code for wireLength in inches
int stepsToTake = (int)(((float)wireLength * 25.4) / mmPerStep;)




Tom... :)

If that does not fix the problem, did you try the original program to verify the length using mm? Its possible your value for mmPerStep may need to be adjusted. The picture showed two slightly different lengths of wire, if you are getting inconsistent lengths the wire may be slipping in the feeder.

FFS
This is grade 4 maths in any conversation.