// COVID-19: AUTOMATIC ALCOHOL DISPENSER NON-CONTACT
// FIGHTING COVID-19
// Code by: Chợ Sáng Tạo Team
// Function: Close delay, delay relay when receiving control signals.
// Characteristics:
// - Preset relay delay-on / delay-off time
// - Allows the use of resistors to adjust delay times and / or delay interrupts
// - There is an option to use for both high and low return sensors
// - There is an option to use for both the HIGH or LOW trigger relay
 
 
//#define USE_POT_DELAY_ON  //Remove the // sign if using Potentiometer to adjust the closing time
//#define USE_POT_DELAY_OFF //Remove the // sign if using Potentiometer to adjust the delay time
 
#define DELAY_ON  800       // Delay-on time
#define DELAY_OFF 1000      // Delay off time     - 1000 = 1 seconds
 
#define DELAY_ON_RANGE  10000   //Time to delay-on (from 0 to 10 seconds)
#define DELAY_OFF_RANGE 60000   //Time to delay-off (from 0 to 60 seconds)
 
#define SENSOR_PIN  2           //Sensor pin
#define RELAY_PIN   13          //Relay pin
#ifdef USE_POT_DELAY_ON
#define DELAY_ON_POT_PIN A0    //Delay-on potentiometer pin
#endif
#ifdef USE_POT_DELAY_OFF
#define DELAY_OFF_POT_PIN  A1    //Delay-off potentiometer pin
#endif
 
#define SENSOR_LOW_SIGNAL       //Use sensor with low signal detector
//#define SENSOR_HIGH_SIGNAL    //Use sensor with high signal detector
 
//#define RELAY_LOW_TRIGGER     //Low trigger relay (LOW)
#define RELAY_HIGH_TRIGGER      //High trigger relay (HIGH)
 
uint32_t pre_millis, pre_millis_2;
uint32_t delay_on = DELAY_ON;
uint32_t delay_off = DELAY_OFF;
bool relay_on;
 
void setup() {
  // put your setup code here, to run once:
  pinMode(SENSOR_PIN, INPUT_PULLUP);
  pinMode(RELAY_PIN, OUTPUT);
  Serial.begin(9600);
 
  //init relay status
  #ifdef RELAY_LOW_TRIGGER
  digitalWrite(RELAY_PIN, HIGH);
  #elif defined(RELAY_HIGH_TRIGGER)
  digitalWrite(RELAY_PIN, LOW);
  #endif
}
 
void loop() {
  //read sensor value
  int sensor_value;
  #ifdef SENSOR_LOW_SIGNAL
  sensor_value = !digitalRead(SENSOR_PIN);  
  #elif defined(SENSOR_HIGH_SIGNAL)
  sensor_value = digitalRead(SENSOR_PIN);  
  #endif
 
  #ifdef USE_POT_DELAY_ON
  //read potentialmeter to define delay on time
  delay_on = map(analogRead(DELAY_ON_POT_PIN),0,1023,0,DELAY_ON_RANGE);
  #endif
 
  #ifdef USE_POT_DELAY_OFF
  //read potentialmeter to define delay on time
  delay_off = map(analogRead(DELAY_OFF_POT_PIN),0,1023,0,DELAY_OFF_RANGE);
  #endif
 
  // received sensor signal
  if (relay_on){
    if (millis()-pre_millis_2 >= delay_off) {
      #ifdef RELAY_LOW_TRIGGER
      relay_on = false;
      digitalWrite(RELAY_PIN,HIGH);
      pre_millis = millis();
      #elif defined(RELAY_HIGH_TRIGGER)
      relay_on = false;
      digitalWrite(RELAY_PIN,LOW);
      pre_millis = millis();
      #endif         
    }
  } else {
    if (sensor_value) {
      if (millis() - pre_millis >= delay_on) {
        #ifdef RELAY_LOW_TRIGGER
        digitalWrite(RELAY_PIN, LOW);
        relay_on = true;
        pre_millis_2 = millis();
        #elif defined(RELAY_HIGH_TRIGGER)
        digitalWrite(RELAY_PIN, HIGH);   
        relay_on = true;
        pre_millis_2 = millis();         
        #endif      
      }
    } else {
      pre_millis = millis();
    }
  }
}

Add a code snippet to your website: www.paste.org