Components and supplies
Speaker: 0.25W, 8 ohms
SG90 Micro-servo motor
Arduino Nano R3
Ultrasonic Sensor - HC-SR04 (Generic)
Project description
Code
Fun Corona Soap Clock source code
c_cpp
1//FunCoronaSoapClock code for educational automatic handwash "analog" servo clock with sounds 2//this is free software by ElectricAvi without any warranty feel free to modify for your own needs 3#include <Servo.h> 4const bool debug = false; 5 6const byte pin_SensorTrig = 4; //pin attached to HC-SR04 distance sensor's input Trigger pin 7const byte pin_SensorEcho = 5; //pin attached to the returning Echo pin from the sensor 8const byte servoPin = 10; //pin attached to SG90 9g micro servo Signal pin (Signal=orange,Vcc=red,Gnd=brown) 9const byte pin_Speaker = 12; //pin attached to optional Speaker through 100 ohm resistor 10Servo myservo; //create servo object to control a servo (one can be ran through arduino board Vcc) 11float servoPos; //servo angle position 0-180 (Degrees) 12 13float sensorDuration, sensorDistance; //distance sensor parameters 14const float nearDistanceValue = 45; //define 45cm as a near sensor hand location 15const int washingTime = 20; //total washing time (seconds) 16const int servoStepDelay = 100; //"clock" tick time in ms 17 18int StartingSound[] = {3500,5500,3500,5500}; byte StartingSounds = 4; //starting note frequencies at wash detection 19int StartingSoundDelay = 70; //delay speed (ms) between each sound note 20 21int GoodSound[] = {250,500,2000}; byte GoodSounds = 3; //sound note frequencies after "good" 20sec washing time 22int GoodSoundDelay = 150; 23 24void setup(){ 25 pinMode(pin_SensorTrig, OUTPUT); pinMode(pin_SensorEcho, INPUT); //set sensor i/o pins 26 pinMode(pin_Speaker, OUTPUT); digitalWrite(pin_Speaker, LOW); //initialize speaker pin 27 myservo.attach(servoPin); myservo.write(20); //set servo pin and move handle to final "Good" position 28 if (debug) Serial.begin(9600); 29} 30 31void loop(){ 32 //run several repetitive sensor measurements to find average distance 33 float avgDistance = 0; 34 for (int i=0; i<15; i++) { 35 digitalWrite(pin_SensorTrig, LOW); delayMicroseconds(2); //send ultrasonic pulse 36 digitalWrite(pin_SensorTrig, HIGH); delayMicroseconds(10); 37 digitalWrite(pin_SensorTrig, LOW); 38 39 sensorDuration = pulseIn(pin_SensorEcho, HIGH); //measure sensor echo return time (us) 40 sensorDistance = (sensorDuration*.0343)/2; //calculate distance by speed of sound (0.0343 us/cm) 41 42 avgDistance += sensorDistance; 43 delay(5); 44 } 45 avgDistance /= 15; //calculate average sensor distance (cm) 46 47 if (debug) { 48 Serial.print("avgDistance="); Serial.println(avgDistance); 49 } 50 51 //check if sensor detected a hand in front of it so soap-washing has began... 52 if (avgDistance<nearDistanceValue) { 53 if (debug) Serial.println("Hand detected!!!"); 54 55 //play starting sound notes 56 for (byte soundIdx=0; soundIdx<StartingSounds; soundIdx++) { 57 tone(pin_Speaker,StartingSound[soundIdx]); delay(StartingSoundDelay); noTone(pin_Speaker); 58 } 59 60 if (debug) Serial.println("sending servo to starting position"); 61 for (servoPos = 20; servoPos < 160; servoPos += 1) { //turn back servo handle to starting position 62 myservo.write(servoPos); 63 delay(20); 64 } 65 66 if (debug) Serial.println("slowly turning the servo from initial Bad to final Good position"); 67 float servoStepDegrees = (160-20); //calculate degrees to rotate each tick 68 servoStepDegrees = servoStepDegrees/(washingTime*1000)*servoStepDelay; 69 70 //start slowly turning servo handle in the "good" direction at the right speed for total wash time 71 for (servoPos = 160; servoPos >= 20; servoPos -= servoStepDegrees) { 72 myservo.write(servoPos); 73 delay(servoStepDelay); 74 } 75 76 //play good sound notes 77 for (byte soundIdx=0; soundIdx<GoodSounds; soundIdx++) { 78 tone(pin_Speaker,GoodSound[soundIdx]); delay(GoodSoundDelay); noTone(pin_Speaker); 79 } 80 81 if (debug) Serial.println("played ending sound notes."); 82 delay(2000); //delay a few seconds before checking sensor again 83 } 84} 85
Fun Corona Soap Clock source code
c_cpp
1//FunCoronaSoapClock code for educational automatic handwash "analog" servo clock with sounds 2//this is free software by ElectricAvi without any warranty feel free to modify for your own needs 3#include <Servo.h> 4const bool debug = false; 5 6const byte pin_SensorTrig = 4; //pin attached to HC-SR04 distance sensor's input Trigger pin 7const byte pin_SensorEcho = 5; //pin attached to the returning Echo pin from the sensor 8const byte servoPin = 10; //pin attached to SG90 9g micro servo Signal pin (Signal=orange,Vcc=red,Gnd=brown) 9const byte pin_Speaker = 12; //pin attached to optional Speaker through 100 ohm resistor 10Servo myservo; //create servo object to control a servo (one can be ran through arduino board Vcc) 11float servoPos; //servo angle position 0-180 (Degrees) 12 13float sensorDuration, sensorDistance; //distance sensor parameters 14const float nearDistanceValue = 45; //define 45cm as a near sensor hand location 15const int washingTime = 20; //total washing time (seconds) 16const int servoStepDelay = 100; //"clock" tick time in ms 17 18int StartingSound[] = {3500,5500,3500,5500}; byte StartingSounds = 4; //starting note frequencies at wash detection 19int StartingSoundDelay = 70; //delay speed (ms) between each sound note 20 21int GoodSound[] = {250,500,2000}; byte GoodSounds = 3; //sound note frequencies after "good" 20sec washing time 22int GoodSoundDelay = 150; 23 24void setup(){ 25 pinMode(pin_SensorTrig, OUTPUT); pinMode(pin_SensorEcho, INPUT); //set sensor i/o pins 26 pinMode(pin_Speaker, OUTPUT); digitalWrite(pin_Speaker, LOW); //initialize speaker pin 27 myservo.attach(servoPin); myservo.write(20); //set servo pin and move handle to final "Good" position 28 if (debug) Serial.begin(9600); 29} 30 31void loop(){ 32 //run several repetitive sensor measurements to find average distance 33 float avgDistance = 0; 34 for (int i=0; i<15; i++) { 35 digitalWrite(pin_SensorTrig, LOW); delayMicroseconds(2); //send ultrasonic pulse 36 digitalWrite(pin_SensorTrig, HIGH); delayMicroseconds(10); 37 digitalWrite(pin_SensorTrig, LOW); 38 39 sensorDuration = pulseIn(pin_SensorEcho, HIGH); //measure sensor echo return time (us) 40 sensorDistance = (sensorDuration*.0343)/2; //calculate distance by speed of sound (0.0343 us/cm) 41 42 avgDistance += sensorDistance; 43 delay(5); 44 } 45 avgDistance /= 15; //calculate average sensor distance (cm) 46 47 if (debug) { 48 Serial.print("avgDistance="); Serial.println(avgDistance); 49 } 50 51 //check if sensor detected a hand in front of it so soap-washing has began... 52 if (avgDistance<nearDistanceValue) { 53 if (debug) Serial.println("Hand detected!!!"); 54 55 //play starting sound notes 56 for (byte soundIdx=0; soundIdx<StartingSounds; soundIdx++) { 57 tone(pin_Speaker,StartingSound[soundIdx]); delay(StartingSoundDelay); noTone(pin_Speaker); 58 } 59 60 if (debug) Serial.println("sending servo to starting position"); 61 for (servoPos = 20; servoPos < 160; servoPos += 1) { //turn back servo handle to starting position 62 myservo.write(servoPos); 63 delay(20); 64 } 65 66 if (debug) Serial.println("slowly turning the servo from initial Bad to final Good position"); 67 float servoStepDegrees = (160-20); //calculate degrees to rotate each tick 68 servoStepDegrees = servoStepDegrees/(washingTime*1000)*servoStepDelay; 69 70 //start slowly turning servo handle in the "good" direction at the right speed for total wash time 71 for (servoPos = 160; servoPos >= 20; servoPos -= servoStepDegrees) { 72 myservo.write(servoPos); 73 delay(servoStepDelay); 74 } 75 76 //play good sound notes 77 for (byte soundIdx=0; soundIdx<GoodSounds; soundIdx++) { 78 tone(pin_Speaker,GoodSound[soundIdx]); delay(GoodSoundDelay); noTone(pin_Speaker); 79 } 80 81 if (debug) Serial.println("played ending sound notes."); 82 delay(2000); //delay a few seconds before checking sensor again 83 } 84} 85
Downloadable files
Circuit schematic for the Corona Soap Clock
Circuit schematic for the Corona Soap Clock
Comments
Only logged in users can leave comments
ElectricAvi
0 Followers
•0 Projects
Table of contents
Intro
1
0