Advertisement
sugar0

Untitled

Jul 3rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.11 KB | None | 0 0
  1. //VERSION 5
  2. //See: https://forum.arduino.cc/index.php?topic=621432.0
  3.  
  4. //No effort has been made to minimize sketch size
  5.  
  6. #include <SwitchManager.h>
  7. //object instantiations
  8. SwitchManager insideSwitch;
  9. SwitchManager outsideSwitch;
  10.  
  11. /*
  12. The SwitchManager.h file should be placed in your libraries folder, i.e.
  13. C:\Users\YouName\Documents\Arduino\libraries\SwitchManager\SwitchManager.h
  14. You can download the library at:
  15. http://gammon.com.au/Arduino/SwitchManager.zip Thank you Nick!
  16. */
  17.  
  18. //*****************************
  19. //Switch stuff
  20. #define PUSHED LOW
  21. #define RELEASED HIGH
  22.  
  23. //*****************************
  24. //Power relay
  25. #define powerOFF LOW
  26. #define powerON HIGH
  27.  
  28. //Open/Close relay
  29. #define openDoor LOW
  30. #define closeDoor HIGH
  31.  
  32. //*****************************
  33. //some constants
  34. #define timingDisabled 0
  35. #define timingEnabled 1
  36. #define timingFinished 2
  37.  
  38. //*****************************
  39. //I/O pin variables
  40. const byte inSwitch = 2; //a switch that opens/closes the door
  41. const byte speaker = 3; //connected to the passive beeper
  42. const byte outSwitch = 4; //a switch that opens/closes the door
  43. const byte openCloseRelay = 7; //drives the OPEN/Close relay
  44. const byte powerRelay = 8; //drives the power ON/OFF relay
  45. const byte heartBeatLED = 13; //a LED will toggle if the code is non blocking
  46.  
  47. //*****************************
  48. //RAM variables
  49. byte lastStateInsideSwitch;
  50. byte lastStateOutsideSwitch;
  51.  
  52. byte inSwitchFlag = 0;
  53. byte outSwitchFlag = 0;
  54.  
  55. //*****************************
  56. //FSM stuff
  57. const byte STOPPED = 0;
  58. const byte OPENING = 1;
  59. const byte WAIT = 2;
  60. const byte CLOSING = 3;
  61. const byte KEEPOPENED = 4;
  62.  
  63. byte mState = STOPPED; //power up in the stopped state
  64.  
  65. //*****************************
  66. //Timing variables
  67. const unsigned long doorTravelTime = 6 * 1000UL; //6 seconds to move door
  68. const unsigned long waitTime = 6 * 1000UL; //5 seconds before closing door
  69. const unsigned long shortPress = 100UL; //100ms short press lower limit
  70. const unsigned long longPress = 2 * 1000UL; //2 seconds long press upper limit
  71. const unsigned long verylongPress = 4 * 1000UL; //2 seconds long press upper limit
  72. const unsigned long beepTime = 3000UL; //300ms the duration of the beep
  73.  
  74. unsigned long doorOpenMillis;
  75. unsigned long doorClosedMillis;
  76. unsigned long heartBeatMillis;
  77. unsigned long doorWaitMillis;
  78. unsigned long inTimingMillis;
  79. unsigned long outTimingMillis;
  80. unsigned long beepMillis;
  81.  
  82.  
  83. // s e t u p ( )
  84. //****************************************************************************
  85. void setup()
  86. {
  87. //Serial.begin(9600);
  88.  
  89. insideSwitch.begin (inSwitch, checkSwitches);
  90. outsideSwitch.begin (outSwitch, checkSwitches);
  91.  
  92. pinMode(speaker, OUTPUT); //beeps if switches held for >= longPress time
  93.  
  94. digitalWrite(powerRelay, powerOFF); //at power up, door power is OFF
  95. pinMode(powerRelay, OUTPUT);
  96.  
  97. digitalWrite(openCloseRelay, openDoor); //at power up door defaults to open
  98. pinMode(openCloseRelay, OUTPUT);
  99.  
  100. pinMode(heartBeatLED, OUTPUT); //heart beat LED
  101.  
  102. } //END of setup()
  103.  
  104.  
  105. // l o o p ( )
  106. //****************************************************************************
  107. void loop()
  108. {
  109. //*****************************
  110. //is it time to toggle heart beat LED?
  111. if (millis() - heartBeatMillis >= 500)
  112. {
  113. //restart timer
  114. heartBeatMillis = millis();
  115.  
  116. //toggle LED
  117. digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  118. }
  119.  
  120. //***************************
  121. //is it time to check the duration of a push?
  122. if (millis() - beepMillis > 50)
  123. {
  124. //restart the timer
  125. beepMillis = millis();
  126.  
  127. checkForLongPush();
  128. }
  129.  
  130. //***************************
  131. //check to see what's happening with the switches
  132. //"Do not use delay()s" in your sketch as it will make switch changes unresponsive
  133. //Use BlinkWithoutDelay (BWD) techniques instead.
  134. insideSwitch.check ();
  135. outsideSwitch.check ();
  136.  
  137. //*****************************
  138. //check the FSM
  139. checkMachine();
  140.  
  141. //*****************************
  142. //other non-blocking code goes here
  143. //*****************************
  144.  
  145. } //END of loop()
  146.  
  147.  
  148. // c h e c k S w i t c h e s ( . . . )
  149. //****************************************************************************
  150. void checkSwitches(const byte newState, const unsigned long interval, const byte whichPin)
  151. {
  152. switch (whichPin)
  153. {
  154. //***************************** I N S I D E S W I T C H
  155. case inSwitch:
  156. {
  157. //if current state is STOPPED was the switch just RELEASED?
  158. if (mState == STOPPED && newState == RELEASED)
  159. {
  160. //***************************** s h o r t p u s h
  161. if (interval >= shortPress && interval < longPress)
  162. {
  163. //FSM to the OPENING state
  164. mState = OPENING;
  165.  
  166. //the time the door started moving open
  167. doorOpenMillis = millis();
  168.  
  169. //set up the direction relay
  170. digitalWrite(openCloseRelay, openDoor);
  171.  
  172. //wait 10ms for relay to transfer
  173. delay(10);
  174.  
  175. //turn on the power to the door
  176. digitalWrite(powerRelay, powerON);
  177.  
  178. } //END of if (interval <= shortPress)
  179.  
  180. //***************************** l o n g p u s h
  181. else if (interval >= longPress && interval < verylongPress)
  182. {
  183. //FSM to the KEEPOPENED state
  184. mState = KEEPOPENED;
  185.  
  186. //the time the door started moving open
  187. doorOpenMillis = millis();
  188.  
  189. //set up the direction relay
  190. digitalWrite(openCloseRelay, openDoor);
  191.  
  192. //wait 10ms for relay to transfer
  193. delay(10);
  194.  
  195. //turn on the power to the door
  196. digitalWrite(powerRelay, powerON);
  197.  
  198. } //END of else if (interval >= longPress && interval < verylongPress)
  199.  
  200. //***************************** v e r y l o n g p u s h
  201. else (interval >= verylongPress)
  202. {
  203.  
  204. //do nothing
  205. break;
  206.  
  207. } //END of else (interval >= verylongPress)
  208.  
  209. } //END of if (mState == STOPPED && newState == !PUSHED)
  210.  
  211. } //END of case inSwitch:
  212.  
  213. //***************************** O U T S I D E S W I T C H
  214. case outSwitch:
  215. {
  216. //if current state is STOPPED was the switch just RELEASED?
  217. if (mState == STOPPED && newState == RELEASED)
  218. {
  219. //***************************** s h o r t p u s h
  220. if (interval >= shortPress && interval < longPress)
  221. {
  222. //FSM to the OPENING state
  223. mState = OPENING;
  224.  
  225. //the time the door started moving open
  226. doorOpenMillis = millis();
  227.  
  228. //set up the direction relay
  229. digitalWrite(openCloseRelay, openDoor);
  230.  
  231. //wait 10ms for relay to transfer
  232. delay(10);
  233.  
  234. //turn on the power to the door
  235. digitalWrite(powerRelay, powerON);
  236.  
  237. } //END of if (interval <= shortPress)
  238.  
  239. //***************************** l o n g p u s h
  240. else if (interval >= longPress && interval < verylongPress)
  241. {
  242. //FSM to the KEEPOPENED state
  243. mState = KEEPOPENED;
  244.  
  245. //the time the door started moving open
  246. doorOpenMillis = millis();
  247.  
  248. //set up the direction relay
  249. digitalWrite(openCloseRelay, openDoor);
  250.  
  251. //wait 10ms for relay to transfer
  252. delay(10);
  253.  
  254. //turn on the power to the door
  255. digitalWrite(powerRelay, powerON);
  256. } //END of else if (interval >= longPress && interval < verylongPress)
  257.  
  258. //***************************** v e r y l o n g p u s h
  259. else (interval >= verylongPress)
  260. {
  261. //do nothing
  262. break;
  263.  
  264. } //END of else (interval >= verylongPress)
  265.  
  266. } //END of if (mState == STOPPED && newState == !PUSHED)
  267.  
  268. } //END of case outSwitch:
  269.  
  270. } //END of switch (whichPin)
  271.  
  272. //*****************************
  273. //Future switch area
  274. //*****************************
  275.  
  276. } //END of checkSwitches()
  277.  
  278.  
  279. // c h e c k M a c h i n e ( )
  280. //****************************************************************************
  281. void checkMachine()
  282. {
  283. //*****************************
  284. //FSM
  285. switch (mState)
  286. {
  287. //*************
  288. case STOPPED:
  289. //do nothing
  290. break;
  291.  
  292. //*************
  293. case OPENING:
  294. if (millis() - doorOpenMillis >= doorTravelTime)
  295. {
  296. //FSM to the WAIT state
  297. mState = WAIT;
  298.  
  299. //the time we entered the WAIT state
  300. doorWaitMillis = millis();
  301.  
  302. //power to door OFF
  303. digitalWrite(powerRelay, powerOFF);
  304. }
  305. break;
  306.  
  307. //*************
  308. case WAIT:
  309. if (millis() - doorWaitMillis >= waitTime)
  310. {
  311. //FSM to the CLOSING state
  312. mState = CLOSING;
  313.  
  314. //the time the door started moving closed
  315. doorClosedMillis = millis();
  316.  
  317. //set up the direction relay
  318. digitalWrite(openCloseRelay, closeDoor);
  319.  
  320. //wait 10ms for relay to transfer
  321. delay(10);
  322.  
  323.  
  324. digitalWrite(powerRelay, powerON);
  325. }
  326. break;
  327.  
  328. //*************
  329. case CLOSING:
  330. if (millis() - doorClosedMillis >= doorTravelTime)
  331. {
  332. //FSM to the STOPPED state
  333. mState = STOPPED;
  334.  
  335. //power to door OFF
  336. digitalWrite(powerRelay, powerOFF);
  337.  
  338. //wait 10ms for relay to transfer
  339. delay(10);
  340.  
  341. //set up the direction relay
  342. digitalWrite(openCloseRelay, openDoor);
  343. }
  344. break;
  345.  
  346. //*************
  347. case KEEPOPENED:
  348. if (millis() - doorOpenMillis >= doorTravelTime)
  349. {
  350. //FSM to the STOPPED state
  351. mState = STOPPED;
  352.  
  353. //power to door OFF
  354. digitalWrite(powerRelay, powerOFF);
  355. }
  356. break;
  357.  
  358. } //END of switch/case
  359.  
  360. } //END of checkMachine()
  361.  
  362.  
  363. // c h e c k F o r L o n g P u s h ( )
  364. //****************************************************************************
  365. //this function will make the speaker beep if a switch has been pressed for >= longPress seconds
  366. void checkForLongPush()
  367. {
  368. byte currentState;
  369.  
  370. //if the door is moving, do not check the switches
  371. if (mState != STOPPED)
  372. {
  373. return;
  374. }
  375.  
  376. //******************************************* i n S w i t c h
  377. currentState = digitalRead(inSwitch);
  378.  
  379. //*****************************
  380. if (currentState == RELEASED)
  381. {
  382. inSwitchFlag = timingDisabled;
  383. }
  384.  
  385. //*****************************
  386. //the switch state is PUSHED
  387. else
  388. {
  389. switch (inSwitchFlag)
  390. {
  391. //***************
  392. case timingDisabled:
  393. //initialize the timer
  394. inTimingMillis = millis();
  395.  
  396. inSwitchFlag = timingEnabled;
  397.  
  398. break;
  399.  
  400. //***************
  401. case timingEnabled:
  402. if (millis() - inTimingMillis >= longPress && interval < verylongPress)
  403. {
  404. //make a 1kHz sound
  405. tone(speaker, 1000, beepTime);
  406.  
  407. inSwitchFlag = timingFinished;
  408. }
  409.  
  410. else if (millis() - inTimingMillis >= verylongPress)
  411. {
  412.  
  413. tone(speaker, 1000, beepTime);
  414. delay(1);
  415. tone(speaker, 1000, beepTime);
  416. delay(1);
  417. tone(speaker, 1000, beepTime);
  418. delay(1);
  419. tone(speaker, 1000, beepTime);
  420. delay(1);
  421. tone(speaker, 1000, beepTime);
  422. delay(1);
  423. tone(speaker, 1000, beepTime);
  424.  
  425.  
  426. inSwitchFlag = timingFinished;
  427.  
  428. }
  429.  
  430.  
  431. break;
  432.  
  433. //***************
  434. case timingFinished:
  435. //do nothing
  436.  
  437. break;
  438.  
  439. } //END of switch/case
  440.  
  441. } //END of else
  442.  
  443. //******************************************* o u t S w i t c h
  444. currentState = digitalRead(outSwitch);
  445.  
  446. //*****************************
  447. if (currentState == RELEASED)
  448. {
  449. outSwitchFlag = timingDisabled;
  450. }
  451.  
  452. //*****************************
  453. //the switch state is PUSHED
  454. else
  455. {
  456. switch (outSwitchFlag)
  457. {
  458. //***************
  459. case timingDisabled:
  460. //initialize the timer
  461. outTimingMillis = millis();
  462.  
  463. outSwitchFlag = timingEnabled;
  464.  
  465. break;
  466.  
  467. //***************
  468. case timingEnabled:
  469. if (millis() - outTimingMillis >= longPress)
  470. {
  471. //make a 1kHz sound
  472. tone(speaker, 1000, beepTime);
  473.  
  474. outSwitchFlag = timingFinished;
  475. }
  476.  
  477. break;
  478.  
  479. //***************
  480. case timingFinished:
  481. //do nothing
  482.  
  483. break;
  484.  
  485. } //END of switch/case
  486.  
  487. } //END of else
  488.  
  489. } //END of checkForLongPush()
  490.  
  491. //****************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement