hi i recently started using Arduino and have a certain project i don't know why but i don't actually have any error messages but whenever i connect the my Mega 2560 to the components the motor wont work and i recently added a toggle switch thinking it might solve my problem but it ended up much worse i would like to ask for your help about what is wrong with my code I am using a NEMA 23 stepper motor and lcd with I2C and an hx711 load amplifier for my load cell as well as a keypad for input `
`
#include <HX711_ADC.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Load Cell and Keypad settings
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'.', '0', '#', 'D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};
HX711_ADC LoadCell1(52, 53);
HX711_ADC LoadCell2(6, 7);
LiquidCrystal_I2C lcd1(0x27, 20, 4);
LiquidCrystal_I2C lcd2(0x3F, 20, 4);
Keypad keypad1 = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Keypad keypad2 = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
float initialRPM1 = 0;
float initialRPM2 = 0;
bool saved1 = false;
bool saved2 = false;
// Motor control settings
int driverPUL = 47; // PUL- pin
int driverDIR = 46; // DIR- pin
int spd = A0; // Potentiometer
int switchPin = 10; // SPDT toggle switch pin
int pd = 500; // Pulse Delay period
boolean setdir = LOW; // Set Direction
void setup() {
LoadCell1.begin();
LoadCell1.start(500);
LoadCell1.setCalFactor(1000.0);
LoadCell2.begin();
LoadCell2.start(500);
LoadCell2.setCalFactor(1000.0);
lcd1.init();
lcd1.backlight();
lcd2.init();
lcd2.backlight();
pinMode(driverPUL, OUTPUT);
pinMode(driverDIR, OUTPUT);
pinMode(switchPin, INPUT_PULLUP); // Enable internal pull-up resistor
}
void loop() {
// Read switch state
int switchState = digitalRead(switchPin);
// Set direction based on switch state
if (switchState == HIGH) {
// Set direction for forward motion
digitalWrite(driverDIR, HIGH);
} else {
// Set direction for reverse motion
digitalWrite(driverDIR, LOW);
}
handleKeypadInputAndDisplay(LoadCell1, lcd1, keypad1, initialRPM1, saved1);
handleKeypadInputAndDisplay(LoadCell2, lcd2, keypad2, initialRPM2, saved2);
// Motor control logic
pd = map(analogRead(spd), 0, 1023, 2000, 50);
digitalWrite(driverPUL, HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL, LOW);
delayMicroseconds(pd);
}
void handleKeypadInputAndDisplay(HX711_ADC &loadCell, LiquidCrystal_I2C &lcd, Keypad &keypad, float &initialRPM, bool &saved) {
loadCell.update();
float i = loadCell.getData();
float torque = i / 1000 * 0.3048 * 9.80665;
float power = i / 1000 * 0.3048 * 9.80665 * 2 * 3.1415 * (initialRPM / 60.0);
char key = keypad.getKey();
if (key != NO_KEY) {
if (isDigit(key) || key == '.') {
if (key == '.') {
initialRPM += 0.1;
} else {
initialRPM = (initialRPM * 10) + (key - '0');
}
} else if (key == 'A') {
initialRPM = initialRPM / 10;
} else if (key == 'B') {
initialRPM = 0;
} else if (key == 'D') {
saved = true;
} else if (key == 'C') {
initialRPM = 0;
saved = false;
}
lcd.clear();
lcd.setCursor(13, 0);
lcd.print("RPM:");
lcd.setCursor(13, 1);
if (saved) {
lcd.print("SAVED");
} else {
lcd.print(initialRPM);
}
}
lcd.setCursor(0, 0);
lcd.print("TORQUE(N-m):");
lcd.setCursor(0, 1);
lcd.print(torque);
lcd.setCursor(0, 2);
lcd.print("POWER(w):");
lcd.setCursor(0, 3);
lcd.print(power);
}