Salve, sono ragazzo che ha finito un ITIS sez elettrotecnica, ed ad Arduino mi sono appena avvicinato (pochi giorni), attirato da un amico.
Di programmazione, ne so poco.
Buongiorno.
Premetto che sto utilizzando un Arduino UNO.
Col mio programma, avrei in mente di controllare uno stepper (NEMA 24), che funziona con pin DIR e PUL a 24V.
Con "controllare", intendo avviare il motore, raggiungere un velocità wmax, e poterlo fermare, sia aprendo START (pulsante) che chiudendo STOP (pulsante d' emergenza, cioè che una volta messo, rimane finché non si apre).
Vorrei sapere come posso fare ripartire il motore da fermo, e non dalla velocità "di crociera", quando ri-chiudo START & è aperto STOP.
Sotto, il programma (scusate se non so come mettere solamente delle stringhe di codice):
Prima del setup:
#include <AccelStepper.h>
// parametri della libreria AccelStepper;
#define DRIVER 1
// queste costanti sono ON = 0 se il pulsante è NC, ON = 1;
const int ON = LOW;
const int OFF = HIGH;
// enf del programma;
const int pRX = 0;
const int PTX = 1;
const int pPUL = 2;
const int pDIR = 3;
const int pENA = 4;
const int pPED = 5;
const int pALM = 6;
const int pCPUL = 7;
const int pCDIR = 8;
const int pSTOP = 9;
const int pSTART = 10;
const int pledBLUE = 11;
const int pledGREEN = 12;
const int pledRED = 13;
// parametri interni del programma;
float a = 200.0; // accelerazione prevista;
float w = 200.0; // velocità prevista;
float wmax = 1600.0; // velocità massima;
long s = 8000; // n° passi di spostamento;
const int t = 0.1; // tempo minimo di attesa [s*10^-3];
const unsigned int tmin = 20; // tempo minimo di attesa [s*10^-6];
// variabili interni del programma;
//unsigned long startTime0; // [s*10^-3];
//unsigned long startTime1; // [s*10^-3];
//unsigned long startTime2; // [s*10^-3];
//unsigned long startTime3; // [s*10^-3];
//unsigned long startTime4; // [s*10^-3];
//unsigned long startTime5; // [s*10^-3];
//unsigned long currentTime; // [s*10^-3];
// valore delle enf del programma;
int RX;
int TX;
int PUL;
int DIR;
int ENA;
int PED;
int ALM;
int CDIR;
int CPUL;
int STOP;
int START;
// stato degli interruttori;
int sCDIR = LOW;
int sSTOP = ON;
int sSTART = OFF;
AccelStepper SDX(DRIVER,pPUL,pDIR);
Setup
void setup() {
// motore;
pinMode(pPUL, OUTPUT); // impulso;
pinMode(pDIR, OUTPUT); // direzione;
pinMode(pENA, OUTPUT); // abilitazione;
// pulsanti;
pinMode(pCDIR, INPUT); // pulsante DIR;
pinMode(pCPUL, INPUT); // pulsante PUL;
pinMode(pSTOP, INPUT); // pulsante STOP;
pinMode(pSTART, INPUT); // pulsante START;
// luci;
pinMode(pledBLUE, OUTPUT); // led blu;
pinMode(pledGREEN, OUTPUT); // led verde;
pinMode(pledRED, OUTPUT); // led rosso;
// startTime0 = millis();
// startTime1 = millis();
// startTime2 = millis();
// startTime3 = millis();
// startTime4 = millis();
// startTime5 = millis();
digitalWrite(pPUL, LOW);
digitalWrite(pDIR, HIGH);
digitalWrite(pENA, LOW);
SDX.setMinPulseWidth(tmin);
SDX.setMaxSpeed(wmax);
SDX.setSpeed(w);
SDX.setAcceleration(a);
SDX.setCurrentPosition(0);
SDX.moveTo(s);
Serial.begin(9600);
}
Loop: pulsanti...
void loop(){
// I vari pulsanti.....................................................................................
int lCDIR = digitalRead(pCDIR);
if(lCDIR!=CDIR and lCDIR==ON){
sCDIR = !sCDIR;
}
int rpDIR = digitalRead(pDIR);
int lSTOP = digitalRead(pSTOP);
sSTOP = lSTOP;
/*if(lSTOP!=STOP and lSTOP==ON){ // !!!
sSTOP = !sSTOP;
}
STOP = lSTOP;*/
int rpSTOP = digitalRead(pSTOP);
int lSTART = digitalRead(pSTART);
if(lSTART!=START and lSTART==ON){
sSTART = !sSTART;
}
Loop: comandi...
// ed i vari comandi...................................................................................
if(sCDIR==OFF){
digitalWrite(pDIR,HIGH);
SDX.setPinsInverted(0,0);
} else {
digitalWrite(pDIR,LOW);
SDX.setPinsInverted(0,1);
}
if(sSTOP==ON){
digitalWrite(pDIR,HIGH);
sSTART = OFF;
// digitalWrite(pledRED,HIGH);
}
if(sSTART==OFF){
SDX.setSpeed(w);
SDX.stop();
}
else if (sSTOP==OFF){
// digitalWrite(pledRED,LOW);
if(sSTART==ON){
SDX.run();
}
}
// Velocità costante all' infinito;
if(SDX.distanceToGo()<=s*0.7){
SDX.move(s);}
Loop: luci ed altro.
// e le varie luci.....................................................................................
if(rpDIR==ON){
digitalWrite(pledBLUE,HIGH);
} else{
digitalWrite(pledBLUE,LOW);
}
if(sSTOP==ON){
digitalWrite(pledRED,HIGH);
} else{
digitalWrite(pledRED,LOW);
}
if(sSTART==ON){
digitalWrite(pledGREEN,HIGH);
} else{
digitalWrite(pledGREEN,LOW);
}
// ed i vari altri.....................................................................................
// Non serve il delay con libreria AccelStepper:
// serve comunque nel setup, come set.MinPulseWidth!;
// Non si può utilizzare il Serial.print con libreria
// AccelStepper: rallenta troppo!;
CDIR = lCDIR;
STOP = lSTOP;
START = lSTART;
}