MrAlvin

Turn off Relay in X seconds

Jan 15th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. /*
  2.  *  Find ButtonOne Library at https://github.com/MrAlvin/ButtonOne
  3.  *
  4.  * Test circuit:
  5.  *
  6.  *  Connect a pushbutton to pin A1 (ButtonPin), connect other side of button to ground.
  7.  *  
  8.  *  Pin 13 (StatusPin) is used for output
  9.  *    many Arduino versions already have a LED connected to this pin,
  10.  *    otherwise connect: pin13 - LED - resistor - ground
  11.  *    
  12.  *  Potentiometer connected to <+ - A3 - ground> see diagram example https://labitat.dk/wiki/Arduino_beginners_workshop#CIRC08_-_Twisting_-_.28Potentiometers.29
  13.  *  
  14.  */
  15.  
  16. #include "ButtonOne.h"
  17.  
  18. int sensorPin = A3;    // select the input pin for the potentiometer
  19. int ledPin = 13;      // select the pin for the LED
  20. int buttonPin = 7;
  21. unsigned long sensorValue = 0;  // variable to store the value coming from the sensor
  22. unsigned long relay_millis = 0;
  23.  
  24.  
  25. // Setup a ButtonOne instance on pin 7.  
  26. ButtonOne button(buttonPin);
  27.  
  28.  
  29. //*********************************************
  30. // setup function - to run once:
  31. //*********************************************
  32. void setup() {
  33.   // enable the standard led on pin 13.
  34.   pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  35.  
  36.   //initiate internal button management values
  37.   button.begin();
  38.  
  39.   // link the Press function to be called on a button Press event.  
  40.   button.attachPress(btnPress);
  41.  
  42.     // initialize serial communications at 9600 bps:
  43.   Serial.begin(9600);
  44.   Serial.println( F("Hello") );
  45.  
  46. } // setup
  47.  
  48.  
  49. //*********************************************
  50. // main code -  to run repeatedly:
  51. //*********************************************
  52. void loop() {
  53.   button.check(); // keep watching the push button:
  54.  
  55.   sensorValue = analogRead(sensorPin);
  56.  
  57.   updateDisplay();
  58.  
  59.   time_out_relay();
  60.  
  61. } // loop
  62.  
  63. //*********************************************
  64. void time_out_relay() {
  65.   if(millis() - relay_millis >=  0)  {
  66.      turn_off_relay();
  67.   }          
  68. }
  69.  
  70. //*********************************************
  71. void turn_on_relay(){
  72.   digitalWrite(ledPin, HIGH);
  73.   Serial.println( F("Relay ON") );
  74. }
  75.  
  76. //*********************************************
  77. void turn_off_relay(){
  78.   digitalWrite(ledPin, LOW);
  79.   Serial.println( F("Relay OFF") );
  80. }
  81.  
  82. //*********************************************
  83. // this function will be called when the button is pressed
  84. //*********************************************
  85.  
  86. void btnPress() {
  87.   Serial.print("Off in ");
  88.   Serial.print(sensorValue);
  89.   Serial.print(" seconds");
  90.  
  91.   relay_millis = millis() + (sensorValue * 1000);
  92.   turn_on_relay();
  93. } // btnPress
  94.  
  95.  
  96. //*********************************************
  97. // update display
  98. //*********************************************
  99. void updateDisplay() {
  100.  static unsigned long last_millis = 0;
  101.  
  102.  if(millis() - last_millis >  1000)  { //update once every second
  103.      last_millis = millis();            //ready for next update
  104.      Serial.println(sensorValue);
  105.  }
  106. }
  107.  
  108. // End
Advertisement
Add Comment
Please, Sign In to add comment