From a912c0fc688f7c2bb9a592ae8efb88f84c4b7519 Mon Sep 17 00:00:00 2001 From: Michael Reber Date: Sat, 6 Jun 2020 10:36:31 +0200 Subject: [PATCH] cleanup and adding touchless disinfection code --- .../touchless_disinfection.ino | 44 ++++++++++++++++ .../touchless_power_switch.ino | 41 ++++++--------- .../touchless_time_limited.ino | 52 ------------------- 3 files changed, 61 insertions(+), 76 deletions(-) create mode 100644 touchless_disinfection/touchless_disinfection.ino delete mode 100644 touchless_time_limited/touchless_time_limited.ino diff --git a/touchless_disinfection/touchless_disinfection.ino b/touchless_disinfection/touchless_disinfection.ino new file mode 100644 index 0000000..45378ab --- /dev/null +++ b/touchless_disinfection/touchless_disinfection.ino @@ -0,0 +1,44 @@ +const int US_sensorPin = 7; +const int relayPin = 10; +const int range = 8; //Range of detection from the sensor in cm + +bool newAction; + + +void setup() { + Serial.begin (9600); //Allows serial output to serial monitor + pinMode(relayPin, OUTPUT); + digitalWrite(relayPin, LOW); // Default relay should be OFF! +} + +void loop() { + long duration, cm; + pinMode(US_sensorPin, OUTPUT); + digitalWrite(US_sensorPin, LOW); + delayMicroseconds(2); + digitalWrite(US_sensorPin, HIGH); + delayMicroseconds(5); + digitalWrite(US_sensorPin, LOW); + pinMode(US_sensorPin, INPUT); + duration = pulseIn(US_sensorPin, HIGH); + + cm = microsecondsToCentimeters(duration); + + if (cm < range && cm > 1) { + + if (newAction) { + digitalWrite(relayPin, HIGH); // Turn Ralay ON + delay(50); // Miliseconds which it waits before turn off again + digitalWrite(relayPin,LOW); // Trun Ralay OFF + newAction = false; + } + delay(1000); // Miliseconds which it waits before does any action again + + } else { + newAction = true; + } +} + +long microsecondsToCentimeters(long microseconds) { + return microseconds / 29 / 2; +} diff --git a/touchless_power_switch/touchless_power_switch.ino b/touchless_power_switch/touchless_power_switch.ino index ab7af6c..0491aa7 100644 --- a/touchless_power_switch/touchless_power_switch.ino +++ b/touchless_power_switch/touchless_power_switch.ino @@ -3,8 +3,10 @@ const int relayPin = 10; const int range = 8; //Range of detection from the sensor in cm int valRelay = 0; // variable to store the read value long relayAction; +bool newAction; void setup() { + Serial.begin (9600); //Allows serial output to serial monitor pinMode(relayPin, OUTPUT); digitalWrite(relayPin, HIGH); // Default relay should be OFF! } @@ -12,45 +14,36 @@ void setup() { void loop() { long duration, cm; pinMode(US_sensorPin, OUTPUT); - digitalWrite(US_sensorPin, LOW); delayMicroseconds(2); digitalWrite(US_sensorPin, HIGH); delayMicroseconds(5); digitalWrite(US_sensorPin, LOW); - pinMode(US_sensorPin, INPUT); duration = pulseIn(US_sensorPin, HIGH); cm = microsecondsToCentimeters(duration); - - if (cm < range && cm > 1) { - long new_duration, new_cm; - pinMode(US_sensorPin, OUTPUT); - digitalWrite(US_sensorPin, LOW); - delayMicroseconds(2); - digitalWrite(US_sensorPin, HIGH); - delayMicroseconds(5); - digitalWrite(US_sensorPin, LOW); - pinMode(US_sensorPin, INPUT); - new_duration = pulseIn(US_sensorPin, HIGH); - new_cm = microsecondsToCentimeters(new_duration); + if (cm < range && cm > 1) { - valRelay = digitalRead(relayPin); // read the status of input pin - if (valRelay) { - relayAction = LOW; - } else { - relayAction = HIGH; - } + if (newAction) { + valRelay = digitalRead(relayPin); // read the status of input pin + if (valRelay) { + relayAction = LOW; + } else { + relayAction = HIGH; + } - if (new_cm < range && new_cm > 1) { digitalWrite(relayPin, relayAction); // Switch Ralay ON or OFF - delay(1000); // Miliseconds which it waits before does any action again + newAction = false; } + delay(1000); // Miliseconds which it waits before does any action again + + } else { + newAction = true; } } + long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; - -} +} \ No newline at end of file diff --git a/touchless_time_limited/touchless_time_limited.ino b/touchless_time_limited/touchless_time_limited.ino deleted file mode 100644 index d361f78..0000000 --- a/touchless_time_limited/touchless_time_limited.ino +++ /dev/null @@ -1,52 +0,0 @@ -const int US_sensorPin = 7; -const int relayPin = 10; -const int range = 8; //Range of detection from the sensor in cm - -void setup() { - pinMode(relayPin, OUTPUT); - digitalWrite(relayPin, HIGH); // Default relay should be OFF! -} - -void loop() { - long duration, cm; - pinMode(US_sensorPin, OUTPUT); - - digitalWrite(US_sensorPin, LOW); - delayMicroseconds(2); - digitalWrite(US_sensorPin, HIGH); - delayMicroseconds(5); - digitalWrite(US_sensorPin, LOW); - - pinMode(US_sensorPin, INPUT); - duration = pulseIn(US_sensorPin, HIGH); - - cm = microsecondsToCentimeters(duration); - - if (cm < range && cm > 1) { - long new_duration, new_cm; - pinMode(US_sensorPin, OUTPUT); - digitalWrite(US_sensorPin, LOW); - delayMicroseconds(2); - digitalWrite(US_sensorPin, HIGH); - delayMicroseconds(5); - digitalWrite(US_sensorPin, LOW); - pinMode(US_sensorPin, INPUT); - new_duration = pulseIn(US_sensorPin, HIGH); - - new_cm = microsecondsToCentimeters(new_duration); - - if (new_cm < range && new_cm > 1) { - digitalWrite(relayPin, LOW); // Turn Ralay ON - delay(500); // Miliseconds which it waits before turn off again - digitalWrite(relayPin,HIGH); // Trun Ralay OFF - } - - if (new_cm < range && new_cm > 1) { - delay(3000); // Pauses the measure for 3 seconds.. so the liquid-flow stops after above time - } - } -} -long microsecondsToCentimeters(long microseconds) { - return microseconds / 29 / 2; - -}