hello everyone!
I'm using this selector switch to ask if I want the stepper motor to execute the word at the first switch while it is rotating and then the switch happens immediately, the stepper motor stops before it completes its time. I would like to ask how do you do it when you change the switch immediately? The stepper motor completes the first instruction for a certain time and then follows the instruction on the next switch. I tried increasing the delay but it didn't work.
or how to make when the switch is turned back and the stepper motor is in the position where the switch was previously selected.
#define Time0 10000
#define Time1 4500
#define Time2 6000
#define Time3 10000
#define Time4 8000
int pul = 4;
int dir = 3;
int ena = 2;
int sw0 = A0;
int sw1 = A1;
int sw2 = A2;
int sw3 = A3;
int sw4 = A4;
int buttonState0;
int buttonState1;
int buttonState2;
int buttonState3;
int buttonState4;
unsigned long Milliseclast0;
unsigned long Millisec0;
unsigned long Milliseclast1;
unsigned long Millisec1;
unsigned long Milliseclast2;
unsigned long Millisec2;
unsigned long Milliseclast3;
unsigned long Millisec3;
unsigned long Milliseclast4;
unsigned long Millisec4;
enum { idle0, for0, rev0 };
int state0 = idle0;
enum { idle1, for1, rev1 };
int state1 = idle1;
enum { idle2, for2, rev2 };
int state2 = idle2;
enum { idle3, for3, rev3 };
int state3 = idle3;
enum { idle4, for4, rev4 };
int state4 = idle4;
void setup () {
pinMode (pul, OUTPUT);
pinMode (dir, OUTPUT);
pinMode (ena, OUTPUT);
pinMode (sw0, INPUT_PULLUP);
buttonState0 = digitalRead (sw0);
pinMode (sw1, INPUT_PULLUP);
buttonState1 = digitalRead (sw1);
pinMode (sw2, INPUT_PULLUP);
buttonState2 = digitalRead (sw2);
pinMode (sw3, INPUT_PULLUP);
buttonState3 = digitalRead (sw3);
pinMode (sw4, INPUT_PULLUP);
buttonState4 = digitalRead (sw4);
digitalWrite (ena,HIGH);
Serial.begin (9600);
}
void step (void)
{
digitalWrite (pul,HIGH);
delayMicroseconds(20);
digitalWrite (pul,LOW);
delayMicroseconds(20);
}
void loop () {
Millisec0 = millis ();
Millisec1 = millis ();
Millisec2 = millis ();
Millisec3 = millis ();
Millisec4 = millis ();
if (idle0 != state0) {
if ((Millisec0 - Milliseclast0) > Time0)
state0 = idle0;
else
step ();
}
//____________________________________________
if (idle1 != state1) {
if ((Millisec1 - Milliseclast1) > Time1)
state1 = idle1;
else
step ();
}
//____________________________________________
if (idle2 != state2) {
if ((Millisec2 - Milliseclast2) > Time2)
state2 = idle2;
else
step ();
}
//____________________________________________
if (idle3 != state3) {
if ((Millisec3 - Milliseclast3) > Time3)
state3 = idle3;
else
step ();
}
//____________________________________________
if (idle4 != state4) {
if ((Millisec4 - Milliseclast4) > Time4)
state4 = idle4;
else
step ();
}
//_____________________________________________________________________
int button0 = digitalRead (sw0);
if (buttonState0 != button0) {
buttonState0 = button0;
delay (10);
if (LOW == button0 && idle0 == state0) {
Milliseclast0 = Millisec0;
state0 = rev0;
digitalWrite (dir, LOW);
delay (1000);
}
}
//_____________________________________________________________________
int button1 = digitalRead (sw1);
if (buttonState1 != button1) {
buttonState1 = button1;
delay (10);
if (LOW == button1 && idle1 == state1) {
Milliseclast1 = Millisec1;
state1 = for1;
digitalWrite (dir, HIGH);
delay (1000);
}
else if (HIGH == button1 && idle1 == state1) {
Milliseclast1 = Millisec1;
state1 = rev1;
digitalWrite (dir, LOW);
}
}
//_____________________________________________________________________
int button2 = digitalRead (sw2);
if (buttonState2 != button2) {
buttonState2 = button2;
delay (10);
if (LOW == button2 && idle2 == state2) {
Milliseclast2 = Millisec2;
state2 = for2;
digitalWrite (dir, HIGH);
delay (1000);
}
else if (HIGH == button2 && idle2 == state2) {
Milliseclast2 = Millisec2;
state2 = rev2;
digitalWrite (dir, LOW);
}
}
//_____________________________________________________________________
int button3 = digitalRead (sw3);
if (buttonState3 != button3) {
buttonState3 = button3;
delay (10);
if (LOW == button3 && idle3 == state3) {
Milliseclast3 = Millisec3;
state3 = for3;
digitalWrite (dir, HIGH);
delay (1000);
}
else if (HIGH == button3 && idle3 == state3) {
Milliseclast3 = Millisec3;
state3 = rev3;
digitalWrite (dir, LOW);
}
}
//_____________________________________________________________________
int button4 = digitalRead (sw4);
if (buttonState4 != button4) {
buttonState4 = button4;
delay (10);
if (LOW == button4 && idle4 == state4) {
Milliseclast4 = Millisec4;
state4 = for4;
digitalWrite (dir, HIGH);
delay (1000);
}
else if (HIGH == button4 && idle4 == state4) {
Milliseclast4 = Millisec4;
state4 = rev4;
digitalWrite (dir, LOW);
}
}
//_____________________________________________________________________
}