Hi everyone, I'm a beginner, you will notice from the type of code I wrote.
I'll explain what I have to do with this project.
I use an Arduino Mega 2560V3
I have to drive a driver for a stepper motor (DM556) that operates a nema 34 stepper with which I move the X axis of a hobby milling machine that I have at home.
For the operation I have provided a joystick with NO contacts to move to the right or left, if I wanted I could also use that of an analog arduino, but to start and make it easier as programming I chose this.
I have two limit switches that I will put on the table along the X axis to stop the operation when it reaches one of the two limits, these are NC microswitches so that if you have to cut the cable or have some problem I stop the motion and cause no damage .
I have a button for fast forward (with its own trimmer to adjust the speed) and a trimmer for the basic speed subject to the two switches on the joystick.
As outputs I have: 3 LEDs, one for status as power on and 2 to indicate the direction, the 3 pins for the stepper driver ENA, DIR, PUL.
I have set the inputs as PULLUP so I always use the reference to ground.
however I managed after many tests to find the various problems that did not make me go to the sketch.
Now I have a new problem but I think it can be solved with a Accelstepper library if I'm not mistaken, I have to read up.
Basically everything I want it to do works but if I start with a high number of impulses to the driver, the motor gets stuck, I think it should use the accel_stepper library or something like that.
if anyone has some better idea or even suggests me how to optimize the sketch I would be grateful.
#include <AccelStepper.h>
#define puls_sx 3 //pulsante controller SX
#define puls_dx 4 //pulsante controller DX
#define ena 5 //ENA stepper Driver
#define dir 6 //DIR stepper Driver
#define pul 7 //PUL stepper Driver
#define rapid 8 //Pulsante rapido
#define potS A0 //Potenziometro velocità normale
#define potR A1 //Potenziometro velocità Rapida
#define fcsx 10 //Fine corsa SX NC
#define fcdx 11 //Fine corsa DX NC
#define ledSx 22 //Led direzione SX
#define ledDx 23 //Led direzione DX
#define buzzer 9 // Buzzer segnalazioni acustiche
#define power_on 2 //led stato acceso
int val_p_sx =0;
int val_p_dx =0;
int val_fcsx =0;
int val_fcdx =0;
int val_potS =0;
int val_potR =0;
int val_rapid =0;
long delay_MicrosS = 0;
long delay_MicrosR = 0;
void setup() {
Serial.begin(9600);
pinMode(puls_sx, INPUT_PULLUP);
pinMode(puls_dx, INPUT_PULLUP);
pinMode(ena, OUTPUT);
pinMode(dir, OUTPUT);
pinMode(pul, OUTPUT);
pinMode(potS, INPUT);
pinMode(potR, INPUT);
pinMode(fcsx, INPUT_PULLUP);
pinMode(fcdx, INPUT_PULLUP);
pinMode(ledSx, OUTPUT);
pinMode(ledDx, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(power_on, OUTPUT);
pinMode(rapid, INPUT_PULLUP);
digitalWrite(power_on, LOW);
digitalWrite(ledSx,LOW);
digitalWrite(ledDx,LOW);
delay(200);
digitalWrite(ledSx, HIGH);
digitalWrite(ledDx, HIGH);
Serial.println ("Ready");
delay(1000);
digitalWrite(ledSx, LOW);
digitalWrite(ledDx, LOW);
tone(1, 1000, 600);
delay(1000);
}
void tone_err() {
tone(2, 1200, 100);
delay(100);
tone(2, 1200, 100);
delay(100);
tone(2, 1200, 100);
delay(100);
tone(2, 1200, 100);
delay(100);
tone(2, 1200, 100);
delay(100);
tone(2, 1200, 100);
delay(100);
tone(2, 1200, 100);
delay(100);
tone(2, 1200, 100);
delay(100);
tone(2, 1200, 100);
delay(100);
tone(2, 1200, 100);
delay(100);
Serial.println("Tono Errore");
}
void start_normal() {
// if (digitalRead(val_rapid)==1){ //se puls rapid non premuto
delay_MicrosS = map(analogRead(potS), 0, 1023, 1000, 20000); //leggo velocita potS
{
delayMicroseconds (delay_MicrosS);
digitalWrite(pul, HIGH);
delayMicroseconds(5);
digitalWrite(pul, LOW);
}
}
void start_rapid() {
// if(digitalRead(val_rapid)==0) { //se premo rapid
delay_MicrosR = map(analogRead(potR), 0, 1023, 100, 3000); //leggo potR
{
delayMicroseconds (delay_MicrosR);
digitalWrite(pul, HIGH);
delayMicroseconds(5);
digitalWrite(pul, LOW);
}
}
//}
void loop() {
val_p_sx = digitalRead(puls_sx);
val_p_dx = digitalRead(puls_dx);
val_fcsx = digitalRead(fcsx);
val_fcdx = digitalRead(fcdx);
val_rapid = digitalRead(rapid);
if ((val_p_sx == 0)&&(val_p_dx==1)) { //funziona a SX
if (val_fcsx == 0) {
digitalWrite(ledSx, HIGH);
digitalWrite(dir, HIGH);
digitalWrite(ena, HIGH);
if (val_rapid==0){
start_rapid();
}
else {
start_normal();
}
}
else { if (val_fcsx == 1) { //finecorsa sx aperto blocco movimento
digitalWrite(ledSx, LOW); {
digitalWrite(ena, LOW);
Serial.println("finecorsa sinistra");
tone_err();
}
}
}
}
if ((val_p_dx == 0)&&(val_p_sx==1)){
if (val_fcdx == 0){ //funziona a dx
digitalWrite(ledDx, HIGH);
digitalWrite(dir, LOW);
digitalWrite(ena, HIGH);
if (val_rapid==0){
start_rapid();
}
else {
start_normal();
}
}
else { if(val_fcdx == 1); { //finecorsa dx aperto blocco movimento
digitalWrite(ledDx, LOW); {
digitalWrite(ena, LOW);
Serial.println("finecorsa destra");
tone_err();
}
}
}
}
if (((val_p_dx==0) && (val_p_sx==0))||((val_p_dx==1) && (val_p_sx==1))){
digitalWrite(ena, LOW);
Serial.println("Stop");
}
/*if ((val_p_dx==1) && (val_p_sx==1)){
digitalWrite(ena, LOW);
// Serial.println("Stop 2");
} */
}