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