MAYUKHMALI DAS
Published © GPL3+

Simple handwash timer

An absolute necessary home-automation project which you should do at home.

BeginnerFull instructions provided1 hour3,012
Simple handwash timer

Things used in this project

Story

Read more

Custom parts and enclosures

Waterproof case for Arduino

Schematics

Circuit diagram

Code

Automatic hand-wash timer

Arduino
#include <NewPing.h> 
#include <Ewma.h>    
#include <cppQueue.h>
#include <Servo.h>
#define TRIGGER_PIN  9  
#define ECHO_PIN     10  
#define MAX_DISTANCE 200 
#define IMPLEMENTATION FIFO 
#define OVERWRITE      true 
int queue_length = 20;
int num_buffer_frames = 50;
int frames_since_launch = 0;
boolean isFirstLaunch = true;
int difference_threshold = 15; 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
Servo countServo;
Queue window(sizeof(float), queue_length, IMPLEMENTATION, OVERWRITE);
Ewma adcFilter1(0.05); 
void setup() {
  Serial.begin(115200);
  countServo.attach(7);
  countServo.write(180);
  delay(700);
  countServo.write(0);
  delay(700);
  countServo.write(180);
  clearQueue();
}
void loop() {
  delay(50); 
  float filtered1 = adcFilter1.filter(sonar.ping_cm());
  float filtered_mean_removed_value = filtered1 - meanZero(filtered1); 
  Serial.println(filtered_mean_removed_value);
  if (isFirstLaunch) {
    frames_since_launch++;
  }
  if (filtered_mean_removed_value > 15 || filtered_mean_removed_value < -15) {
    if (!isFirstLaunch) {
      countdownServo();
    }
  }
  if (frames_since_launch >= num_buffer_frames) {
    isFirstLaunch = false;
  }
}
int meanZero(float smoothedVal) {
  window.push(&smoothedVal);
  int retval = 0;
  for (int i = 0; i < queue_length; i++) {
    float for_mean;
    window.peekIdx(&for_mean, i);
    retval = retval + (int)for_mean;
  }
  int mean = retval / queue_length;
  return mean;
}
void clearQueue() {
  for (int i = 0; i < queue_length; i++) {
    float queue_zeroer = 0;
    window.push(&queue_zeroer);
  }
}
void countdownServo() {
  Serial.println("Counting down");
  int halfseconds = 40;
  for (int i = halfseconds; i >= 0; i--) {
    countServo.write((int)(i * 4.5));
    delay(500);
  }
  countServo.write(180);
  clearQueue();
  delay(700);
  frames_since_launch = 0;
  isFirstLaunch = true;
}

Credits

MAYUKHMALI DAS

MAYUKHMALI DAS

6 projects • 8 followers
Currently pursuing Electronics and Telecommunication engineering from Jadavpur university , India .

Comments