Advertisement
skizziks_53

two_relays

Aug 6th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. // constnts that won't change. They're used here to set pin numbers
  2. const int button1 = 2; // the number of the pushbutton pin
  3. const int button2 = 3; // the number of the pushbutton pin
  4. const int relay1 = 11; // the number of the relay1 pin
  5. const int relay2 = 12; // the number of the relay2 pin
  6.  
  7. // variables that will change
  8. int buttonState1 = 1; // variable for reading the pushbutton1 status
  9. int buttonState2 = 1; // variable for reading the pushbutton2 status
  10.  
  11. boolean button1_pressed = false; // This is a state flag for button1.
  12. int relay1_onTime = 1000; // This is the time in milliseconds that relay1 will turn on.
  13. unsigned long button1_previousTime = 0; // This is for saving the millis() time that the button was pressed.
  14. unsigned long button1_currentTime = 0; // This is for saving a later time to compare to the above time.
  15.  
  16. boolean button2_pressed = false; // This is a state flag for button2.
  17. int relay2_onTime = 5000; // This is the time in milliseconds that relay2 will turn on.
  18. unsigned long button2_previousTime = 0;// This is for saving the millis() time that the button was pressed.
  19. unsigned long button2_currentTime = 0;// This is for saving a later time to compare to the above time.
  20.  
  21. void setup()
  22. {
  23. // initialize the relay pin as an output
  24. pinMode(relay1, OUTPUT);
  25. pinMode(relay2, OUTPUT);
  26.  
  27. digitalWrite(relay1, HIGH);
  28. digitalWrite(relay2, HIGH);
  29.  
  30. // initialize the pushbutton pin as an input
  31. pinMode(button1, INPUT_PULLUP); // PULLUP for add 5v to pin without hardware prevent pin from floating between HIGH and LOW
  32. pinMode(button2, INPUT_PULLUP); // PULLUP for add 5v to pin without hardware prevent pin from floating between HIGH and LOW
  33. }
  34.  
  35. void loop()
  36. {
  37.  
  38. if (button1_pressed == false) {
  39. // This check is here to de-bounce the button input.
  40. // Also it only tests for the button press if the relay is turned [off].
  41.  
  42. // read the state of the pushbutton value
  43. buttonState1 = digitalRead(button1);
  44.  
  45. // check if the pushbutton is pressed. If it is, the buttonState is LOW.
  46. if (buttonState1 == LOW)
  47. {
  48. digitalWrite(relay1, LOW); // pin 2 is pressed and connected to GND so it will be LOW
  49. //delay (1000); // remove 5v from pin 11 so relay in1 will be 0v and this will make relay on
  50. // wait 1 sec.
  51. button1_pressed = true;
  52. button1_previousTime = millis();
  53. }
  54. //else
  55. //{
  56. //digitalWrite(relay1, HIGH); // add 5v to arduino pin 11 so relay in1 will be 5v and this will make the relay turn off.
  57. // turning off the relay is done further below
  58. //}
  59. }
  60.  
  61.  
  62.  
  63. if (button2_pressed == false) {
  64. // This check is here to de-bounce the button input.
  65. // Also it only tests for the button press if the relay is turned [off].
  66.  
  67. // read the state of the pushbutton value
  68. buttonState2 = digitalRead(button2);
  69.  
  70. // check if the pushbutton is pressed
  71. // if it is, the buttonState is LOW
  72. if (buttonState2 == LOW) // pin 3 is pressed and connected to GND so it will be LOW
  73. { // remove 5v from pin 12 so relay in2 will be 0v and this will make the relay turn on
  74. digitalWrite(relay2, LOW); // and wait 5 seconds.
  75. //delay (5000);
  76. button2_pressed = true;
  77. button2_previousTime = millis();
  78. }
  79. //else
  80. //{
  81. //digitalWrite(relay2, HIGH); // add 5v to arduino pin 12 so relay in2 will be 5v and this will make the relay turn off.
  82. // turning off the relay is done further below
  83. //}
  84. }
  85.  
  86.  
  87. if (button1_pressed == true) {
  88. button1_currentTime = millis();
  89. if (button1_currentTime >= button1_previousTime) {
  90. if (button1_currentTime >= (button1_previousTime + relay1_onTime)) {
  91. // This is now where the relay gets turned off, but only if it is on,
  92. // and has been on for at least (relay1_onTime) milliseconds.
  93. button1_pressed = false; // This turns off the state flag so you can press the button again.
  94. digitalWrite(relay1, HIGH); // This turns off the relay.
  95. }
  96. }
  97. else {
  98. button1_previousTime = millis();
  99. // this is millis() rollover protection, not likely to happen but a good habit.
  100. }
  101. }
  102.  
  103. if (button2_pressed == true) {
  104. button2_currentTime = millis();
  105. if (button2_currentTime >= button2_previousTime) {
  106. if (button2_currentTime >= (button2_previousTime + relay2_onTime)) {
  107. // This is now where the relay gets turned off, but only if it is on,
  108. // and has been on for at least (relay2_onTime) milliseconds.
  109. button2_pressed = false;// This turns off the state flag so you can press the button again.
  110. digitalWrite(relay2, HIGH); // This turns off the relay.
  111. }
  112. }
  113. else {
  114. button2_previousTime = millis();
  115. // this is millis() rollover protection, not likely to happen but a good habit.
  116. }
  117. }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement