larryd:
Version 2:
//VERSION 2
//No effort has been made to minimize sketch size
//*****************************
//Switch stuff
#define PUSHED LOW
//*****************************
//Power relay
#define powerOFF LOW
#define powerON HIGH
//Open/Close relay
#define openDoor LOW
#define closeDoor HIGH
//*****************************
//I/O pin variables
const byte insideSwitch = 2; //a switch that opens/closes the door
const byte outsideSwitch = 4; //a switch that opens/closes the door
const byte openCloseRelay = 7; //drives the OPEN/Close relay
const byte powerRelay = 8; //drives the power ON/OFF relay
const byte heartBeatLED = 13; //a LED will toggle if the code is non blocking
//*****************************
//last read switch states
byte lastStateInsideSwitch;
byte lastStateOutsideSwitch;
//*****************************
//FSM stuff
const byte STOPPED = 0;
const byte OPENING = 1;
const byte WAIT = 2;
const byte CLOSING = 3;
byte mState = STOPPED; //power up in the stopped state
//*****************************
//Timing variables
const unsigned long doorTravelTime = 5 * 1000; //5 seconds to move door
const unsigned long waitTime = 5 * 1000; //5 seconds before closing door
unsigned long doorOpenMillis;
unsigned long doorClosedMillis;
unsigned long heartBeatMillis;
unsigned long switchMillis;
unsigned long doorWaitMillis;
//****************************************************************************
void setup()
{
pinMode(insideSwitch, INPUT_PULLUP);
lastStateInsideSwitch = digitalRead(insideSwitch);
pinMode(outsideSwitch, INPUT_PULLUP);
lastStateOutsideSwitch = digitalRead(outsideSwitch);
digitalWrite(powerRelay, powerOFF); //at power up, door power is OFF
pinMode(powerRelay, OUTPUT);
digitalWrite(openCloseRelay, openDoor); //at power up door defaults to open
pinMode(openCloseRelay, OUTPUT);
pinMode(heartBeatLED, OUTPUT); //heart beat LED
} //END of setup()
//****************************************************************************
void loop()
{
//*****************************
//is it time to toggle heart beat LED?
if (millis() - heartBeatMillis >= 500)
{
//restart timer
heartBeatMillis = millis();
//toggle LED
digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
}
//*****************************
//is it time to check the switches?
if (millis() - switchMillis >= 50)
{
//restart timer
switchMillis = millis();
//check the switches
checkSwitches();
}
//*****************************
//check the FSM
checkMachine();
//*****************************
//other non-blocking code goes here
//*****************************
} //END of loop()
//****************************************************************************
void checkSwitches()
{
byte currentState;
//***************************** I N S I D E S W I T C H
currentState = digitalRead(insideSwitch);
//if we are in the STOPPED state, has the inside switch changed state?
if (mState == STOPPED && lastStateInsideSwitch != currentState)
{
//update to the new state
lastStateInsideSwitch = currentState;
//is the switch currently pushed?
if (currentState == PUSHED)
{
//FSM to the OPENING state
mState = OPENING;
//the time the door started moving open
doorOpenMillis = millis();
//set up the direction relay
digitalWrite(openCloseRelay, openDoor);
//wait 10ms for relay to transfer
delay(10);
//turn on the power to the door
digitalWrite(powerRelay, powerON);
} //END of if (currentState == PUSHED)
} //END of if (mState == STOPPED && lastStateInsideSwitch != currentState)
//***************************** O U T S I D E S W I T C H
currentState = digitalRead(outsideSwitch);
//if we are in the STOPPED state, has the outside switch changed state?
if (mState == STOPPED && lastStateOutsideSwitch != currentState)
{
//update to the new state
lastStateOutsideSwitch = currentState;
//is the switch currently pushed?
if (currentState == PUSHED)
{
//FSM to the OPENING state
mState = OPENING;
//the time the door started moving open
doorOpenMillis = millis();
//set up the direction relay
digitalWrite(openCloseRelay, openDoor);
//wait 10ms for relay to transfer
delay(10);
//turn on the power to the door
digitalWrite(powerRelay, powerON);
} //END of if (currentState == PUSHED)
} //END of if (mState == STOPPED && lastStateOutsideSwitch != currentState)
//*****************************
//Future switch area
//*****************************
} //END of checkSwitches()
//****************************************************************************
void checkMachine()
{
//*****************************
//FSM
switch (mState)
{
//*************
case STOPPED:
//do nothing
break;
//*************
case OPENING:
if (millis() - doorOpenMillis >= doorTravelTime)
{
//FSM to the WAIT state
mState = WAIT;
//the time we entered the WAIT state
doorWaitMillis = millis();
//power to door OFF
digitalWrite(powerRelay, powerOFF);
}
break;
//*************
case WAIT:
if (millis() - doorWaitMillis >= waitTime)
{
//FSM to the CLOSING state
mState = CLOSING;
//the time the door started moving closed
doorClosedMillis = millis();
//set up the direction relay
digitalWrite(openCloseRelay, closeDoor);
//wait 10ms for relay to transfer
delay(10);
//power to door ON
digitalWrite(powerRelay, powerON);
}
break;
//*************
case CLOSING:
if (millis() - doorClosedMillis >= doorTravelTime)
{
//FSM to the STOPPED state
mState = STOPPED;
//power to door OFF
digitalWrite(powerRelay, powerOFF);
//wait 10ms for relay to transfer
delay(10);
//set up the direction relay
digitalWrite(openCloseRelay, openDoor);
}
break;
} //END of switch/case
} //END of checkMachine()
//****************************************************************************
In a similar project, I used large gauge springs:

Yeah Larry you did it! Pressing either button it spin in one direction and after X second in the other.
That massive code must took you much time .. i'm really gratefull. The least i can do is try to understand it as much as i can before completing the work.
Do you think we can add that last segment where i want to leave the door open until i press again the button?