Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //VERSION 5
- //See: https://forum.arduino.cc/index.php?topic=621432.0
- //No effort has been made to minimize sketch size
- #include <SwitchManager.h>
- //object instantiations
- SwitchManager insideSwitch;
- SwitchManager outsideSwitch;
- /*
- The SwitchManager.h file should be placed in your libraries folder, i.e.
- C:\Users\YouName\Documents\Arduino\libraries\SwitchManager\SwitchManager.h
- You can download the library at:
- http://gammon.com.au/Arduino/SwitchManager.zip Thank you Nick!
- */
- //*****************************
- //Switch stuff
- #define PUSHED LOW
- #define RELEASED HIGH
- //*****************************
- //Power relay
- #define powerOFF LOW
- #define powerON HIGH
- //Open/Close relay
- #define openDoor LOW
- #define closeDoor HIGH
- //*****************************
- //some constants
- #define timingDisabled 0
- #define timingEnabled 1
- #define timingFinished 2
- //*****************************
- //I/O pin variables
- const byte inSwitch = 2; //a switch that opens/closes the door
- const byte speaker = 3; //connected to the passive beeper
- const byte outSwitch = 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
- //*****************************
- //RAM variables
- byte lastStateInsideSwitch;
- byte lastStateOutsideSwitch;
- byte inSwitchFlag = 0;
- byte outSwitchFlag = 0;
- //*****************************
- //FSM stuff
- const byte STOPPED = 0;
- const byte OPENING = 1;
- const byte WAIT = 2;
- const byte CLOSING = 3;
- const byte KEEPOPENED = 4;
- byte mState = STOPPED; //power up in the stopped state
- //*****************************
- //Timing variables
- const unsigned long doorTravelTime = 6 * 1000UL; //6 seconds to move door
- const unsigned long waitTime = 6 * 1000UL; //5 seconds before closing door
- const unsigned long shortPress = 100UL; //100ms short press lower limit
- const unsigned long longPress = 2 * 1000UL; //2 seconds long press upper limit
- const unsigned long verylongPress = 4 * 1000UL; //2 seconds long press upper limit
- const unsigned long beepTime = 3000UL; //300ms the duration of the beep
- unsigned long doorOpenMillis;
- unsigned long doorClosedMillis;
- unsigned long heartBeatMillis;
- unsigned long doorWaitMillis;
- unsigned long inTimingMillis;
- unsigned long outTimingMillis;
- unsigned long beepMillis;
- // s e t u p ( )
- //****************************************************************************
- void setup()
- {
- //Serial.begin(9600);
- insideSwitch.begin (inSwitch, checkSwitches);
- outsideSwitch.begin (outSwitch, checkSwitches);
- pinMode(speaker, OUTPUT); //beeps if switches held for >= longPress time
- 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()
- // l o o p ( )
- //****************************************************************************
- 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 duration of a push?
- if (millis() - beepMillis > 50)
- {
- //restart the timer
- beepMillis = millis();
- checkForLongPush();
- }
- //***************************
- //check to see what's happening with the switches
- //"Do not use delay()s" in your sketch as it will make switch changes unresponsive
- //Use BlinkWithoutDelay (BWD) techniques instead.
- insideSwitch.check ();
- outsideSwitch.check ();
- //*****************************
- //check the FSM
- checkMachine();
- //*****************************
- //other non-blocking code goes here
- //*****************************
- } //END of loop()
- // c h e c k S w i t c h e s ( . . . )
- //****************************************************************************
- void checkSwitches(const byte newState, const unsigned long interval, const byte whichPin)
- {
- switch (whichPin)
- {
- //***************************** I N S I D E S W I T C H
- case inSwitch:
- {
- //if current state is STOPPED was the switch just RELEASED?
- if (mState == STOPPED && newState == RELEASED)
- {
- //***************************** s h o r t p u s h
- if (interval >= shortPress && interval < longPress)
- {
- //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 (interval <= shortPress)
- //***************************** l o n g p u s h
- else if (interval >= longPress && interval < verylongPress)
- {
- //FSM to the KEEPOPENED state
- mState = KEEPOPENED;
- //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 else if (interval >= longPress && interval < verylongPress)
- //***************************** v e r y l o n g p u s h
- else (interval >= verylongPress)
- {
- //do nothing
- break;
- } //END of else (interval >= verylongPress)
- } //END of if (mState == STOPPED && newState == !PUSHED)
- } //END of case inSwitch:
- //***************************** O U T S I D E S W I T C H
- case outSwitch:
- {
- //if current state is STOPPED was the switch just RELEASED?
- if (mState == STOPPED && newState == RELEASED)
- {
- //***************************** s h o r t p u s h
- if (interval >= shortPress && interval < longPress)
- {
- //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 (interval <= shortPress)
- //***************************** l o n g p u s h
- else if (interval >= longPress && interval < verylongPress)
- {
- //FSM to the KEEPOPENED state
- mState = KEEPOPENED;
- //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 else if (interval >= longPress && interval < verylongPress)
- //***************************** v e r y l o n g p u s h
- else (interval >= verylongPress)
- {
- //do nothing
- break;
- } //END of else (interval >= verylongPress)
- } //END of if (mState == STOPPED && newState == !PUSHED)
- } //END of case outSwitch:
- } //END of switch (whichPin)
- //*****************************
- //Future switch area
- //*****************************
- } //END of checkSwitches()
- // c h e c k M a c h i n e ( )
- //****************************************************************************
- 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);
- 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;
- //*************
- case KEEPOPENED:
- if (millis() - doorOpenMillis >= doorTravelTime)
- {
- //FSM to the STOPPED state
- mState = STOPPED;
- //power to door OFF
- digitalWrite(powerRelay, powerOFF);
- }
- break;
- } //END of switch/case
- } //END of checkMachine()
- // c h e c k F o r L o n g P u s h ( )
- //****************************************************************************
- //this function will make the speaker beep if a switch has been pressed for >= longPress seconds
- void checkForLongPush()
- {
- byte currentState;
- //if the door is moving, do not check the switches
- if (mState != STOPPED)
- {
- return;
- }
- //******************************************* i n S w i t c h
- currentState = digitalRead(inSwitch);
- //*****************************
- if (currentState == RELEASED)
- {
- inSwitchFlag = timingDisabled;
- }
- //*****************************
- //the switch state is PUSHED
- else
- {
- switch (inSwitchFlag)
- {
- //***************
- case timingDisabled:
- //initialize the timer
- inTimingMillis = millis();
- inSwitchFlag = timingEnabled;
- break;
- //***************
- case timingEnabled:
- if (millis() - inTimingMillis >= longPress && interval < verylongPress)
- {
- //make a 1kHz sound
- tone(speaker, 1000, beepTime);
- inSwitchFlag = timingFinished;
- }
- else if (millis() - inTimingMillis >= verylongPress)
- {
- tone(speaker, 1000, beepTime);
- delay(1);
- tone(speaker, 1000, beepTime);
- delay(1);
- tone(speaker, 1000, beepTime);
- delay(1);
- tone(speaker, 1000, beepTime);
- delay(1);
- tone(speaker, 1000, beepTime);
- delay(1);
- tone(speaker, 1000, beepTime);
- inSwitchFlag = timingFinished;
- }
- break;
- //***************
- case timingFinished:
- //do nothing
- break;
- } //END of switch/case
- } //END of else
- //******************************************* o u t S w i t c h
- currentState = digitalRead(outSwitch);
- //*****************************
- if (currentState == RELEASED)
- {
- outSwitchFlag = timingDisabled;
- }
- //*****************************
- //the switch state is PUSHED
- else
- {
- switch (outSwitchFlag)
- {
- //***************
- case timingDisabled:
- //initialize the timer
- outTimingMillis = millis();
- outSwitchFlag = timingEnabled;
- break;
- //***************
- case timingEnabled:
- if (millis() - outTimingMillis >= longPress)
- {
- //make a 1kHz sound
- tone(speaker, 1000, beepTime);
- outSwitchFlag = timingFinished;
- }
- break;
- //***************
- case timingFinished:
- //do nothing
- break;
- } //END of switch/case
- } //END of else
- } //END of checkForLongPush()
- //****************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement