Servo motor drive wire cutter Program

I am created an Arduino code for servo motor cutting wire in mm measurement with set quantity and speed. There will be a start/stop switch, setting switch, increase switch, decrease switch, Cutting confirmation switch, forward switch and backward switch. After on Show on display eeprom saved measurement, Quantity and Speed. If setting button press above 3 second then setting option will be shown. After setting measurement, quantity and speed all setting will be saved in eeprom. The increase and decrease switch will be using for the parameters setting. By pressing the Forward switch the measurement motor will be rotate to the forward position and by pressing the decrease switch the measurement motor will be rotate to the backward position. The cutting Confirmation switch will be use for cutting confirmation. If the switch will pressed then the counting will be done and the cutting motor will be start to cutting.

I have created a code but this is not working, the code is
#include <EEPROM.h>
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4); // Address 0x27, 20 columns, 4 rows

Servo measurementMotor;
Servo cuttingMotor;

const int startStopSwitch = 2;
const int settingSwitch = 3;
const int increaseSwitch = 4;
const int decreaseSwitch = 5;
const int confirmationSwitch = 6;
const int forwardSwitch = 7;
const int backwardSwitch = 8;

int measurementPosition = 0; // Current measurement position
int quantity = 0; // Cutting quantity
int speed = 0; // Cutting speed
int remainingQuantity = 0; // Remaining cutting quantity

bool isRunning = false;
bool isSettingMode = false;

unsigned long settingButtonPressTime = 0;

void setup() {
pinMode(startStopSwitch, INPUT);
pinMode(settingSwitch, INPUT);
pinMode(increaseSwitch, INPUT);
pinMode(decreaseSwitch, INPUT);
pinMode(confirmationSwitch, INPUT);
pinMode(forwardSwitch, INPUT);
pinMode(backwardSwitch, INPUT);

measurementMotor.attach(9);
cuttingMotor.attach(10);

lcd.begin(20, 4);
lcd.print("Initializing...");

loadSettings();

lcd.clear();
lcd.print("System Ready");
}

void loop() {
if (digitalRead(startStopSwitch) == HIGH) {
if (!isRunning) {
startProcess();
} else {
stopProcess();
}
}

if (digitalRead(settingSwitch) == HIGH) {
if (millis() - settingButtonPressTime > 3000) {
isSettingMode = true;
handleSettings();
}
settingButtonPressTime = millis();
}

if (!isSettingMode) {
if (digitalRead(forwardSwitch) == HIGH) {
rotateForward();
}

if (digitalRead(backwardSwitch) == HIGH) {
  rotateBackward();
}

if (digitalRead(confirmationSwitch) == HIGH) {
  confirmCutting();
}

}
}

void handleSettings() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Setting Mode");

adjustSettings();

saveSettings();

lcd.clear();
lcd.print("Exiting Settings");
delay(2000);

isSettingMode = false;
}

void adjustSettings() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Adjust Settings");

while (isSettingMode) {
if (digitalRead(increaseSwitch) == HIGH) {
increaseSetting();
}

if (digitalRead(decreaseSwitch) == HIGH) {
  decreaseSetting();
}

lcd.setCursor(0, 1);
lcd.print("Measurement: ");
lcd.print(measurementPosition);

lcd.setCursor(0, 2);
lcd.print("Quantity: ");
lcd.print(quantity);

lcd.setCursor(0, 3);
lcd.print("Speed: ");
lcd.print(speed);

delay(100);

}
}

void increaseSetting() {
delay(200); // Debounce

if (measurementPosition < 180) {
measurementPosition++;
} else if (quantity < 100) {
quantity++;
} else if (speed < 100) {
speed++;
}
}

void decreaseSetting() {
delay(200); // Debounce

if (measurementPosition > 0) {
measurementPosition--;
} else if (quantity > 0) {
quantity--;
} else if (speed > 0) {
speed--;
}
}

void startProcess() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Starting Process");

// Display set measurement, quantity, and speed
lcd.setCursor(0, 1);
lcd.print("Set Measurement: ");
lcd.print(measurementPosition);

lcd.setCursor(0, 2);
lcd.print("Set Quantity: ");
lcd.print(quantity);

lcd.setCursor(0, 3);
lcd.print("Set Speed: ");
lcd.print(speed);

delay(5000); // Display the settings for 5 seconds

isRunning = true;
}

void stopProcess() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Stopping Process");
delay(2000);

isRunning = false;

cuttingMotor.write(0); // Assuming 0 is the stop position, adjust as needed

// Calculate remaining quantity and display
remainingQuantity = max(0, quantity - 1); // Assuming 1 cut is done
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Remaining Quantity:");
lcd.print(remainingQuantity);
delay(5000); // Display remaining quantity for 5 seconds
}

void rotateForward() {
if (measurementPosition < 180) {
measurementPosition++;
measurementMotor.write(measurementPosition);
}
}

void rotateBackward() {
if (measurementPosition > 0) {
measurementPosition--;
measurementMotor.write(measurementPosition);
}
}

void confirmCutting() {
if (digitalRead(confirmationSwitch) == HIGH) {
delay(200); // Debounce

if (!isRunning && quantity > 0) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Cutting Confirmed");

  isRunning = true;

  cuttingMotor.write(90); // Assuming 90 is the center position, adjust as needed

  delay(2000); // Display the confirmation for 2 seconds
}

}
}

void loadSettings() {
EEPROM.get(0, measurementPosition);
EEPROM.get(sizeof(int), quantity);
EEPROM.get(sizeof(int) * 2, speed);
}

void saveSettings() {
EEPROM.put(0, measurementPosition);
EEPROM.put(sizeof(int), quantity);
EEPROM.put(sizeof(int) * 2, speed);
}

How has it changed working? What worked before?

Maybe this?

remainingQuantity = 0; // Remaining cutting quantity

This is a new code i have write for control a wire cutter.

when posting code use code tags, e.g.
select < CODE/ > and paste text where it says “type or paste code here”
it makes the code much easier to read

what Arduino are you using?
post a schematic showing the wiring and how you have powered the system
did you test the components of the system, e.g. servo, LCD, etc in seperate programs before attempting the code of post 1

Hello TANMOYSIL

Welcome to the world's best Arduino forum ever.

I assume that you have written the programme by yourself, then it is quite easy to find the error.

There's a trick to figuring out why something isn't working:

Use a logic analyzer to see what happens.
Put Serial.print statements at various places in the code as diagnostic prints to see the values of variables, especially ones that control the servos, and determine whether they meet your expectations.

Have a nice day and enjoy coding in C++.

Hi @TANMOYSIL

welcome to the Arduino-forum

You have written a somehow detailed description of the functionality of your code.
But there are three things that you should improve:

Structure your description into paragraphs.
This is hard to read: dontwriteverythingoneaftereachotheronlyinkeywords

It should look like this:
I have written a code with this functionality:

There are

  • a start/stop switch,
  • a setting switch,
  • an increase switch and an decrease switch,
  • a cutting confirmation switch,
  • a forward switch and backward switch.

After on Show on display eeprom saved measurement, Quantity and Speed.

If setting button press above 3 second then setting option will be shown.
After setting measurement, quantity and speed all setting will be saved in eeprom.

The increase and decrease switch will be using for the parameters setting.
By pressing the Forward switch the measurement motor will be rotate to the forward position and by pressing the decrease switch the measurement motor will be rotate to the backward position.

The cutting Confirmation switch will be use for cutting confirmation. If the switch will pressed then the counting will be done and the cutting motor will be start to cutting.

Much better to read in paragraphs.

Please RE-edit your posting to present your code as a code-section

click on the pencil-icon below your posting and then re-edit your posting following this short tutorial

And last but not least:
With your person everything is OK. What I write next is meant as optimising communication for faster and better help you.

I hope that you agree that communication will become much better if you use
more words to describe your wanted functionality.

Your knowledge about english seems to be limited. Now how can you use much more words to improve communication?

Use your native language, your mother-language. Write down a detailed description in your native language.

But how does this work? This is the english speaking forum?
Believe me: I have tested this in dozens of cases. Use google translate.

The grammar won't be brilliant but the bigger amount of words that you use will still improve the communication.

So at least give it a try in this way:
write down 10 sentences about your project in your native language.
Translate them to english with google translate
Then take the english translation and translate it to french
Then take the french translation and translate it to chinese
Then take the chinese translation back to your native language

You will be astonished that it will be still understandable after all these translations

best regards Stefan

This code looks like it might have been written by an AI, or perhaps someone wrote a draft version but never debugged it. It is syntactically correct, but the logic makes no sense. For example, startProcess() sets a flag isRunning, but nowhere is there code to implement the process. It appears the code will never get into settings mode, and once in settings mode will never get out. The code in increaseSetting() doesn't make any sense.

Either way, I think you need to debug it step by step. For example, I would focus on fixing the settings mode.

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