Hi to all ,my son have project to do and now he cant go forward , buttons on board and limit swithes dosen blink on led somthing is wrong with code ...
An he dosent know how to wire DM542 to arduino to working ok with code.
I have an Arduino Uno, a DM542 stepper driver and a nema 17 stepper motor.
He need the stepper motor to run in one direction until it hits a limit switch then stops. Then, when I press a start button it needs to begin running in the other direction until it hits another limit switch where it will stop. Then pressing the start button should change direction again and so on..
So the start button needs to change direction and start the motor while one of the limit switches is still being pressed.
Thanks in advance for any help.
```cpp
#include <ezButton.h>
#include <AccelStepper.h>
#define DIRECTION_CCW -1
#define DIRECTION_CW 1
#define STATE_GO 1
#define STATE_MOVING 2
#define STATE_STOP 3
#define STATE_STOPPED 4
#define home_switch 9
#define MAX_POSITION 0x7FFFFFFF
ezButton limitSwitch_1(A0);
ezButton limitSwitch_2(A1);
ezButton StartButton(A2);
AccelStepper stepper(1, 7, 6);
int stepperState = STATE_STOPPED;
int direction = DIRECTION_CW;
long targetPos = 0;
int move_finished = 1; // Used to check if move is completed
long initial_homing = -1; // Used to Home Stepper at startup
void setup() {
Serial.begin(9600);
limitSwitch_1.setDebounceTime(50);
limitSwitch_2.setDebounceTime(50);
StartButton.setDebounceTime(50);
pinMode(home_switch, INPUT_PULLUP);
delay(2); // Wait for EasyDriver wake up
// Set Max Speed and Acceleration of each Steppers at startup for homing
stepper.setMaxSpeed(2000.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(8000.0); // Set Acceleration of Stepper
// Start Homing procedure of Stepper Motor at startup
Serial.print("Stepper is Homing . . . . . . . . . . . ");
while (digitalRead(home_switch)) { // Make the Stepper move CCW until the switch is activated
stepper.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepper.run(); // Start moving the stepper
delay(2);
}
stepper.setCurrentPosition(0); // Set the current position as zero for now
stepper.setMaxSpeed(1800.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(8000.0); // Set Acceleration of Stepper
initial_homing = 1;
while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
stepper.moveTo(initial_homing);
stepper.run();
initial_homing++;
delay(2);
}
stepper.setCurrentPosition(0);
Serial.println("Homing Completed");
Serial.println("");
stepper.setMaxSpeed(1800.0); // Set Max Speed of Stepper (Faster for regular movements)
stepper.setAcceleration(8000.0); // Set Acceleration of Stepper
}
void loop() {
limitSwitch_1.loop();
limitSwitch_2.loop();
StartButton.loop();
if (StartButton.isPressed()) { // transition to the move state
stepperState = STATE_GO;
Serial.println(F("Going"));
}
if (limitSwitch_1.isPressed()) {
stepperState = STATE_STOP;
Serial.println(F("The limit switch 1: TOUCHED"));
}
if (limitSwitch_2.isPressed()) {
stepperState = STATE_STOP;
Serial.println(F("The limit switch 2: TOUCHED"));
}
switch (stepperState) {
case STATE_STOP:
Serial.println(F("Stopping."));
stepper.stop();
stepper.run();
Serial.println(F("Changing Direction."));
direction *= -1;
Serial.println(F("The direction -> "));
if (direction == DIRECTION_CW) {
Serial.println(F("CLOCKWISE"));
} else {
Serial.println(F("COUNTER-CLOCKWISE"));
}
stepperState = STATE_STOPPED;
break;
case STATE_STOPPED:
break;
case STATE_GO:
targetPos = direction * MAX_POSITION;
Serial.println(F("Moving towards:"));
Serial.println(F("Target Positon "));
stepper.setCurrentPosition(0);
stepper.moveTo(targetPos);
stepperState = STATE_MOVING;
break;
case STATE_MOVING:
stepper.moveTo(targetPos);
break;
}
stepper.run();
}
now they are both wired N/C, when they are presed are N/O
Then your code is wrong.
isPressed expects the switch to go from open to closed or wired as N/O
I would connect the limit switches to interrupt pins and not use ezbutton. You want the motor to immediatly stop when it hits the switch. Waiting a debounce time before you stop the motor may damage the switches or motor
i dont have any idea more , why the hack buttons wont work , motor spining ok , but problem is on buttons , arduino wont accept them in this code , and led alsow wont blink on arduino board
Hi i give up with previsu code !
Is it posible to easy modificate this code to work with stepper nema 17 motor , now is write for dc motor and it works super .
If any can do this for me ill be super happy and if it needs ill pay!
const int Motor_ClockWiseBtn = A0;
const int Motor_AntiClockWiseBtn = A1;
const int Up_StopBtn = A2;
const int Down_StopBtn = A3;
const int IN1 = 3;
const int IN2 = 4;
const int IN3 = 5;
const int IN4 = 6;
void setup() {
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(Motor_ClockWiseBtn, INPUT_PULLUP);
pinMode(Motor_AntiClockWiseBtn, INPUT_PULLUP);
pinMode(Up_StopBtn, INPUT_PULLUP);
pinMode(Down_StopBtn, INPUT_PULLUP);
}
void loop() {
if (digitalRead(Motor_ClockWiseBtn) == LOW) {
digitalWrite(IN1, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN4, LOW);
Serial.println("Motor rotate Clock wise");
delay(100);
}
if (digitalRead(Motor_AntiClockWiseBtn) == LOW) {
digitalWrite(IN1, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN4, HIGH);
Serial.println("Motor rotate Anti Clock wise");
delay(100);
}
if (digitalRead(Up_StopBtn) == LOW) {
digitalWrite(IN1, LOW);
digitalWrite(IN3, LOW);
Serial.println("Motor stops");
delay(100);
}
if (digitalRead(Down_StopBtn) == LOW) {
digitalWrite(IN2, LOW);
digitalWrite(IN4, LOW);
Serial.println("Motor Stop");
delay(100);
}
}
Code is working
but problem is when one of two limit switch are pressed , with presing button wont start in other direction. button needs to change direction and start the motor while one of the limit switches is still being pressed.
Jim-p
I apologize, it was my mistake.
I haven't tested before whether it also works when one of the limit switches is pressed, so you can activate it in the opposite direction
no 2 buttons need to be replaced with 2 limit switch , and when is one limitswitch press i need with presing 1 button it need to go motor in oposite site