I´m trying to control an actuator with static enum.
I´m using this code:
int endStopClose = 8;
int endStopCloseState = LOW;
int lastEndStopCloseState = 0;
int endStopOpen = 9;
int endStopOpenState = LOW;
int lastEndStopOpenState = 0;
int controlClose = 10;
int controlCloseState = LOW;
int lastcontrolCloseState = 0;
int controlOpen = 11;
int controlOpenState = LOW;
int lastcontrolOpenState = 0;
int openWindow = 12; //Manual switch - Open window
int openWindowState = LOW;
int lastOpenWindowState = 0;
int closeWindow = 5; //Manual switch - Close Window
int closeWindowState = LOW;
int lastCloseWindowState = 0;
int audioSensor = 6;
int audioSensorState = LOW;
int sensorPin = 2;
int sensorState = LOW;
int lastSensorState = 0;
int sensorPinValue = 0; // variable to store the value coming from the sensor
int val = LOW;
byte byteRead;
void setup() {
pinMode(openWindow, INPUT_PULLUP);
pinMode(closeWindow, INPUT_PULLUP);
pinMode(audioSensor, INPUT);
pinMode(sensorPin, INPUT);
pinMode(endStopClose, INPUT_PULLUP);
pinMode(endStopOpen, INPUT_PULLUP);
pinMode(controlClose, OUTPUT);
pinMode(controlOpen, OUTPUT);
Serial.begin(57600); while (!Serial); // UART serial debug
void loop() {
static enum {MOVING_controlClose, MOVING_controlOpen, STOPPED_controlClose, STOPPED_controlOpen, STOPPED2_controlOpen} state;
static unsigned long time_stopped;
unsigned long now = millis();
switch (state) {
case MOVING_controlClose:
if (digitalRead(endStopClose) == LOW) {
digitalWrite(controlClose, LOW);
state = STOPPED_controlClose;
time_stopped = now;
}
break;
case MOVING_controlOpen:
if (digitalRead(endStopOpen) == LOW) {
digitalWrite(controlOpen, LOW);
state = STOPPED_controlOpen;
time_stopped = now;
}
break;
case STOPPED_controlClose:
if (digitalRead(openWindow) == LOW) {
digitalWrite(controlOpen, HIGH);
state = MOVING_controlOpen;
delay(50);
Serial.println("Window is opened by manual switch"); //Serial monitor information.
}
break;
case STOPPED_controlOpen:
if (digitalRead(sensorPin) == HIGH || digitalRead(closeWindow) == LOW || digitalRead(audioSensor) == HIGH) {
// if (digitalRead(sensorPin) == HIGH || digitalRead(closeWindow) == LOW) {
digitalWrite(controlClose, HIGH);
state = MOVING_controlClose;
delay(50);
Serial.println("Movment sensor activated!! Window is closing..."); //Serial monitor information.
//send_flag = true;
}
break;
case STOPPED2_controlOpen:
if (digitalRead(sensorPin) == HIGH || digitalRead(endStopOpen) == LOW) {
digitalWrite(controlClose, HIGH);
state = MOVING_controlClose;
delay(50);
Serial.println("TEST: This is instead of sending an email"); //Serial monitor information.
// send_flag = true; //sending an email with ESP8266
}
break;
}
In the void loop, I have added STOPPED2_controlOpen to the static enum
and this case:
case STOPPED2_controlOpen:
if (digitalRead(sensorPin) == HIGH || digitalRead(endStopOpen) == LOW) {
digitalWrite(controlClose, HIGH);
state = MOVING_controlClose;
delay(50);
Serial.println("TEST: This is instead a sending an email"); //Serial monitor information.
// send_flag = true; //sending an email with ESP8266
}
break;
just to describe for you, what I have done.
As you probably have guessed, it do not work and I´m lost!...
Is it posible to add more than 4 cases in the above code and how do I do it? If not - any suggestions, please?
I also have another problem:
If power is switched off when the actuator is running and the actuator is stopping between the endStopOpen and endStopClose, the program do not work, when it is powered up again.
How can I make an reset, so when the power is switched ON again, the actuator will go back to endStopClose?
Thanks in advance!!