Advertisement
skizziks_53

5-relay gate w/pretend objects v.1

Apr 15th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.17 KB | None | 0 0
  1. /*
  2.   5-relay gate w/pretend objects v.1 ---- written with theoretical objects
  3.   March 15, 2018
  4.  
  5.   This sketch is just for demonstration purposes--it doesn't work.
  6.   It is only to show how small the whole sketch would be, and how easy it would be to understand,
  7.   if the main features of the sketch were all written as classes and placed in external libraries.
  8.  
  9.   There would be lots of other code needed to make this sketch work, but that code would be in the library class files. It would not be here.
  10.   This keeps the sketch easy to understand.
  11.  
  12.   Also note that there are no serial.print() statements here, other than the one exiting the setup() function.
  13.   If you wanted to have serial.print() statements for all the events, it would be better to put them into the class libraries than to write them out here.
  14.   Some of the events are private, and it is easier to write them into the object class.
  15.   You could even have another property in each of the classes to turn them on and off, to allow testing with them but disabling them for the final version of the sketch.
  16. */
  17.  
  18. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  19. // Section 1:
  20. // These are the library include statements for the classes needed:
  21. #include <Custom_Button_01.h> // ------(the custom push-button switch library, that does not exist! --you would have to write this)
  22. #include <Custom_Relay_01.h> // ------(the custom relay library, that does not exist! --you would have to write this)
  23. #include <Custom_Relay_02.h> // ------(a second custom relay library, that does not exist! --you would have to write this)
  24. // The line above operates a relay, but it does so in a different manner than the one before it.
  25. // So it would be easier IMO to just create another library for it, rather than write one library that could handle both situations.
  26.  
  27. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  28. // Section 2:
  29. // Below is declaring the pins to use on the Arduino. (This section is done just like normal)
  30. const int button_Pin = 2;     // the number of the pushbutton pin
  31. const int open_right_gate_relay_pin =  10;      // the number of the relay pin
  32. const int open_left_gate_relay_pin =  11;      // the number of the relay pin
  33. const int close_right_gate_relay_pin =  8;      // the number of the relay pin
  34. const int close_left_gate_relay_pin =  9;      // the number of the relay pin
  35. const int light_gate_relay_pin =  12;      // the number of the relay pin
  36.  
  37. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  38. // Section 3:
  39.  
  40. // Below is declaring a push-button object. (this library does not exist!)
  41. Custom_Button_01 Button1 = Custom_Button_01(2, 500, 100);
  42.  
  43. // Below is declaring Gate_Relay objects. (this library does not exist!)
  44. Custom_Relay_01 Open_right_gate_relay = Custom_Relay_01(open_right_gate_relay_pin, 0, 4);
  45. Custom_Relay_01 Open_left_gate_relay = Custom_Relay_01(open_left_gate_relay_pin, 1, 4);
  46. Custom_Relay_01 Close_right_gate_relay = Custom_Relay_01(close_right_gate_relay_pin, 2, 4);
  47. Custom_Relay_01 Close_left_gate_relay = Custom_Relay_01(close_left_gate_relay_pin, 1, 4);
  48.  
  49. // below is declaring the Flasher1 object. (this library does not exist!)
  50. Custom_Relay_02 Flasher1 = Custom_Relay_02(light_gate_relay_pin, 0, 500, 7.5);
  51.  
  52. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  53. // Section 4:
  54.  
  55. // The Setup() function is done the same as normal.
  56. void setup() {
  57.   Serial.begin(9600);
  58.   pinMode(open_left_gate_relay_pin, OUTPUT);
  59.   pinMode(open_right_gate_relay_pin, OUTPUT);
  60.   pinMode(close_left_gate_relay_pin, OUTPUT);
  61.   pinMode(close_right_gate_relay_pin, OUTPUT);
  62.   pinMode(light_gate_relay_pin, OUTPUT);
  63.   pinMode(button_Pin, INPUT_PULLUP);
  64.   Serial.println("OK: exiting setup()");
  65. }
  66.  
  67. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  68. // Section 5:
  69.  
  70. // Function prototype:
  71. void updateHardware(); // I prefer to put the main loop() first, so any other functions must have prototype statements before they are called {in the main loop()}.
  72.  
  73. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  74. // Section 6:
  75.  
  76. void loop() {
  77.  
  78.   Button1.check(); // This would check the button's state, and it would also tell if the state has changed since the last time the button was checked.
  79.   if (Button1.is_pressed) { // This section would run only when the button changes from [not pressed] to [pressed]. The code for figuring that out would be in Button1.
  80.     updateHardware();
  81.   }
  82.   if (Button1.is_released) { // This section would run only when the button changes from [pressed] to [not pressed]. The code for figuring that out would be in Button1.
  83.     updateHardware();
  84.   }
  85.  
  86. } // end of main loop()
  87.  
  88. // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  89. // Section 7:
  90.  
  91. void updateHardware() {
  92.   // Any time that the button state changes, that info gets sent to all the relays:
  93.   // Each relay has code in it to tell what to do when the button gets pushed, and what to do when the button gets released.
  94.   Open_right_gate_relay.checkRelay(Button1.state);
  95.   Open_left_gate_relay.checkRelay(Button1.state);
  96.   Close_right_gate_relay.checkRelay(Button1.state);
  97.   Close_left_gate_relay.checkRelay(Button1.state);
  98.   Flasher1.checkFlasher(My_PushButton.state);
  99. }
  100.  
  101.  
  102. // ~~~~~~~ [end] ~~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement