Dear all,
I'm a newbie just starts my DIY project with Arduino and some steppers motor.
Currently I face some issues which I tried to troubleshoot but cannot
Firstly, let me explain overall idea of my project:
-
My project is bending wire machine (2D), to bend shape of C1, C2, C3, C4, C5, LA and LB as attachment.
-
I used 03 step motors: 01 for feeding wire, 01 for bending wire and 01 for cutting wire as attachment.
-
Hardware:
- Feeding: Nema 34 (8Nm) + Driver MA860H (Open Loop)
- Bending: Nema 23 57HSM24 + Driver H2-506 (Closed Loop)
- Cutting: Nema 23 57HSM24 + Driver H2-506 (Closed Loop)
- Arduino Mega 2560
- Nextion LCD for monitoring and sending command to Arduino
- Library:
- Accelstepper
- Nextion
Secondly, here below are some issues that I faced currently:
- As I read a lot of time that all recommend we should not use “delay” in our code, that’s why I follow and use stepper.run() instead of using stepper.runToPosition(). And the code need to check a lot of IF conditions to be sure all 03 motors rotate as my desire.
Then I checked and realized that the interval of sending run() command is not often enough to step the motor (around 20 times/ sec) --> Cannot reach speed as plan.
- When I change method and using runToPosition to block and no need to use a lot of IF conditions, it seems can run smoothly, but I cannot stop motor in case it’s over the limit switch?
Could you guys please help me to have a look of my code, anything is unnecessary, need to add more or I do need to change to more powerful Arduino?
I know it will take long time for reading my code, and I’m not too good in English so if you need to get any further information, please let me know.
Really appreciated and thank you for all your help.
Please check code as in attachment (here is over 9000)
//-----------------------------LIBRARY INCLUDED-----------------------------------------------------------
#include <AccelStepper.h>
#include "Nextion.h"
//--------------------------------------------------------------------------------------------------------------
//-------------------------Define PIN and STEPPER----------------------------------------------------------
int FeedingPUL = 4; // Feeding PUL - pin
int FeedingDIR = 5; // Feeding DIR - pin
int BendingPUL = 6; // Bending PUL - pin
int BendingDIR = 7; // Bending DIR - pin
int CuttingPUL = 8; // Cutting PUL - pin
int CuttingDIR = 9; // Cutting DIR - pin
int limitswitch = 3; // Normal: HIGH Press: LOW
int homeswitch = 2; // Normal: HIGH Press: LOW
AccelStepper Feeding(1, FeedingPUL, FeedingDIR);
AccelStepper Bending(1, BendingPUL, BendingDIR);
AccelStepper Cutting(1, CuttingPUL, CuttingDIR);
//---------------------------------------------------------------------------------------------------------------
//---------------------------Define INTERVAL-----------------------------------------------------------------
//-----------Cx_timer [C1_Feeding_interval, C1_Bending_1st, C1_Bending_2nd, C1_Bending_3rd, C1_Bending_4th, C1_Cutting_1st]
//-----------Cx [Cx_start, Cx_Feeding_interval_done, Cx_Bending_1st_done, Cx_Bending_2nd_done, Cx_Bending_3rd_done, Cx_Bending_4th_done, Cx_Cutting_1st_done, Cx_Cutting_2nd_done, Cx_done]
int C1_timer[] = {18257, 765, 8365, 9894, 17494, 18257}; // Example for timer, actually its different
int C2_timer[] = {18257, 765, 8365, 9894, 17494, 18257};
int C3_timer[] = {18257, 765, 8365, 9894, 17494, 18257};
int C4_timer[] = {18257, 765, 8365, 9894, 17494, 18257};
int C5_timer[] = {18257, 765, 8365, 9894, 17494, 18257};
int LA_timer[] = {18257, 765, 8365, 9894, 17494, 18257};
int LB_timer[] = {18257, 765, 8365, 9894, 17494, 18257};
int C1[] = {0,0,0,0,0,0,0,0,0};
int C2[] = {0,0,0,0,0,0,0,0,0};
int C3[] = {0,0,0,0,0,0,0,0,0};
int C4[] = {0,0,0,0,0,0,0,0,0};
int C5[] = {0,0,0,0,0,0,0,0,0};
int LA[] = {0,0,0,0,0,0,0,0,0};
int LB[] = {0,0,0,0,0,0,0,0,0};
int whole_set_flag; // Default dont call WHOLE_SET (WHOLE_SET = 0)
int partly_flag; // Default dont call PARTLY (PARTLY = 0)
int home_flag;
int cut_flag;
unsigned long current_ms;
unsigned long start_time;
uint32_t wholequan;
uint32_t partquan;
int i;
//-----------------------------------SETUP VOID---------------------------------------------------------------
void setup() {
Serial.begin(9600);
Feeding.setCurrentPosition(0);
Feeding.setMaxSpeed(2000); // SPEED = Steps/second
Feeding.setAcceleration(1000); // ACCELERATION = Steps/(second)^2
Feeding.setSpeed(1000); // SPEED = Steps/second
//Feeding.disableOutputs(); //Disable motor to ensure no current
Bending.setCurrentPosition(0);
Bending.setMaxSpeed(4000); // SPEED = Steps/second
Bending.setAcceleration(4000); // ACCELERATION = Steps/(second)^2
//Bending.disableOutputs(); //Disable motor to ensure no current
Cutting.setCurrentPosition(0);
Cutting.setMaxSpeed(3000); // SPEED = Steps/second
Cutting.setAcceleration(3000); // ACCELERATION = Steps/(second)^2
//Cutting.disableOutputs(); //Disable motor to ensure no current
pinMode(limitswitch,INPUT_PULLUP);
pinMode(homeswitch, INPUT_PULLUP);
nexInit();
}
//------------------------------------------LOOP VOID---------------------------------------------------------
void loop() {
nexLoop(nex_listen_list); // Check for any touch event
whole_set(whole_set_flag, wholequan);
partly(partly_flag, type_buffer, partquan);
//Feeding.run();
Bending.run();
Cutting.run();
Serial.println("SENT RUN COMMAND");
}
}
//------------------------------------C1 CONFIGURATION-----------------------------------------------------
void C1_RUN() {
if ((C1[8] == 0) && (digitalRead(homeswitch)) && (digitalRead(limitswitch))) {
if (C1[0] == 0) {
start_time = millis();
C1[0] = 1;
}
current_ms = millis();
//Serial.println(start_time);
//Serial.println(current_ms);
if((current_ms - start_time) <= C1_timer[0]) {
Feeding.runSpeed();
}
if(((current_ms - start_time) >= C1_timer[1]) && (C1[2] == 0)) {
Bending.move(100);
C1[2] = 1;
}
if(((current_ms - start_time) >= C1_timer[2]) && (C1[3] == 0) && (C1[2] == 1)) {
Bending.move(-100);
C1[3] = 1;
}
if(((current_ms - start_time) >= C1_timer[3]) && (C1[4] == 0) && (C1[2] == 1) && (C1[3] == 1)) {
Bending.move(100);
C1[4] = 1;
}
if(((current_ms - start_time) >= C1_timer[4]) && (C1[5] == 0) && (C1[2] == 1) && (C1[3] == 1) && (C1[4] == 1)) {
Bending.move(-100);
C1[5] = 1;
}
if(((current_ms - start_time) >= C1_timer[5]) && (C1[6] == 0)) {
Cutting.move(1000);
C1[6] = 1;
}
if((C1[7] == 0) && (C1[6] == 1) && Cutting.distanceToGo() == 0) {
Cutting.move(-1000);
C1[7] = 1;
}
if((C1[7] == 1) && (C1[6] == 1) && Cutting.distanceToGo() == 0) {
C1[8] = 1;
}
}
}
ma860h.pdf (311 KB)
H2-506.pdf (1.15 MB)
FINAL_TESTING.ino (33.3 KB)