#define MOTOR_A 7
#define MOTOR_B 8
#define MOTOR_C 9
const byte pinMotor[] = { MOTOR_A, MOTOR_B, MOTOR_C };
const byte pinMotorLen = sizeof pinMotor / sizeof pinMotor[0];
#define PIN_SMIN 2
#define PIN_SMAX 3
void setup(void) {
Serial.begin(115200);
for (byte i = 0; i < pinMotorLen; i++) {
pinMode(pinMotor[i], OUTPUT);
}
pinMode(PIN_SMIN, INPUT_PULLUP);
pinMode(PIN_SMAX, INPUT_PULLUP);
Serial.println(pinMotorLen);
}
#define CMD_ON true
#define CMD_OFF false
byte command = CMD_OFF;
byte fsmState = 10;
bool completatoOn = false;
bool completatoOff = false;
uint32_t timer0;
void loop(void) {
if (digitalRead(PIN_SMIN) && digitalRead(PIN_SMAX)) {
command = CMD_ON;
} else if (!digitalRead(PIN_SMAX)) {
command = CMD_OFF;
}
switch (fsmState) {
case 10:
if (command == CMD_ON) {
if (completatoOn == false) {
//complatatoOff = true;
fsmState = 0;
}
} else {
if (completatoOff == false) {
//completatoOn = true;
fsmState = 3;
}
}
break;
case 0:
digitalWrite(MOTOR_A, HIGH);
timer0 = millis();
fsmState++;
break;
case 1:
if (millis() - timer0 > 1000) {
digitalWrite(MOTOR_B, HIGH);
fsmState++;
timer0 = millis();
}
break;
case 2:
if (millis() - timer0 > 1000) {
digitalWrite(MOTOR_C, HIGH);
fsmState = 10;
completatoOn = true;
completatoOff = false;
}
break;
case 3:
digitalWrite(MOTOR_C, LOW);
timer0 = millis();
fsmState++;
break;
case 4:
if (millis() - timer0 > 1000) {
digitalWrite(MOTOR_B, LOW);
fsmState++;
timer0 = millis();
}
break;
case 5:
if (millis() - timer0 > 1000) {
digitalWrite(MOTOR_A, LOW);
fsmState = 10;
completatoOff = true;
completatoOn = false;
}
break;
}
}Full