ElectricAvi
Published © GPL3+

Fun Coronavirus Kids Soap Clock

Easy to make educational kids automatic detection "analog" clock for making hand washing as fun as possible

IntermediateFull instructions provided2,181
Fun Coronavirus Kids Soap Clock

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Speaker: 0.25W, 8 ohms
Speaker: 0.25W, 8 ohms
×1

Story

Read more

Schematics

Circuit schematic for the Corona Soap Clock

Code

Fun Corona Soap Clock source code

C/C++
//FunCoronaSoapClock code for educational automatic handwash "analog" servo clock with sounds
//this is free software by ElectricAvi without any warranty feel free to modify for your own needs
#include <Servo.h>
const bool debug = false;

const byte pin_SensorTrig = 4; //pin attached to HC-SR04 distance sensor's input Trigger pin
const byte pin_SensorEcho = 5; //pin attached to the returning Echo pin from the sensor
const byte servoPin = 10; //pin attached to SG90 9g micro servo Signal pin (Signal=orange,Vcc=red,Gnd=brown)
const byte pin_Speaker = 12; //pin attached to optional Speaker through 100 ohm resistor
Servo myservo;  //create servo object to control a servo (one can be ran through arduino board Vcc)
float servoPos; //servo angle position 0-180 (Degrees)

float sensorDuration, sensorDistance; //distance sensor parameters
const float nearDistanceValue = 45; //define 45cm as a near sensor hand location
const int washingTime = 20; //total washing time (seconds)
const int servoStepDelay = 100; //"clock" tick time in ms

int StartingSound[] = {3500,5500,3500,5500}; byte StartingSounds = 4; //starting note frequencies at wash detection
int StartingSoundDelay = 70; //delay speed (ms) between each sound note
 
int GoodSound[] = {250,500,2000}; byte GoodSounds = 3; //sound note frequencies after "good" 20sec washing time
int GoodSoundDelay = 150;

void setup(){ 
  pinMode(pin_SensorTrig, OUTPUT); pinMode(pin_SensorEcho, INPUT); //set sensor i/o pins
  pinMode(pin_Speaker, OUTPUT); digitalWrite(pin_Speaker, LOW); //initialize speaker pin
  myservo.attach(servoPin); myservo.write(20); //set servo pin and move handle to final "Good" position
  if (debug) Serial.begin(9600);
}

void loop(){  
  //run several repetitive sensor measurements to find average distance
  float avgDistance = 0; 
  for (int i=0; i<15; i++) { 
    digitalWrite(pin_SensorTrig, LOW); delayMicroseconds(2); //send ultrasonic pulse
    digitalWrite(pin_SensorTrig, HIGH); delayMicroseconds(10);
    digitalWrite(pin_SensorTrig, LOW);  

    sensorDuration = pulseIn(pin_SensorEcho, HIGH); //measure sensor echo return time (us)    
    sensorDistance = (sensorDuration*.0343)/2; //calculate distance by speed of sound (0.0343 us/cm)

    avgDistance += sensorDistance; 
    delay(5);      
  }
  avgDistance /= 15; //calculate average sensor distance (cm) 

  if (debug) { 
    Serial.print("avgDistance="); Serial.println(avgDistance); 
  }
  
  //check if sensor detected a hand in front of it so soap-washing has began...
  if (avgDistance<nearDistanceValue) { 
    if (debug) Serial.println("Hand detected!!!");
              
    //play starting sound notes
    for (byte soundIdx=0; soundIdx<StartingSounds; soundIdx++) {    
      tone(pin_Speaker,StartingSound[soundIdx]); delay(StartingSoundDelay); noTone(pin_Speaker);
    }

    if (debug) Serial.println("sending servo to starting position");
    for (servoPos = 20; servoPos < 160; servoPos += 1) { //turn back servo handle to starting position
      myservo.write(servoPos);          
      delay(20);                              
    }

    if (debug) Serial.println("slowly turning the servo from initial Bad to final Good position");
    float servoStepDegrees = (160-20); //calculate degrees to rotate each tick
    servoStepDegrees = servoStepDegrees/(washingTime*1000)*servoStepDelay;
  
    //start slowly turning servo handle in the "good" direction at the right speed for total wash time
    for (servoPos = 160; servoPos >= 20; servoPos -= servoStepDegrees) { 
      myservo.write(servoPos);
      delay(servoStepDelay);              
    }

    //play good sound notes
    for (byte soundIdx=0; soundIdx<GoodSounds; soundIdx++) {    
      tone(pin_Speaker,GoodSound[soundIdx]); delay(GoodSoundDelay); noTone(pin_Speaker);
    }

    if (debug) Serial.println("played ending sound notes.");
    delay(2000); //delay a few seconds before checking sensor again    
  }
}

Credits

ElectricAvi
1 project • 1 follower
Engineer
Contact

Comments

Please log in or sign up to comment.