Components and supplies
Ultrasonic Sensor - HC-SR04 (Generic)
Arduino Nano R3
Alphanumeric LCD, 16 x 2
Passive Buzzer
170 Point Breadboard
Apps and platforms
Arduino IDE
Ultimaker Cura
Project description
Code
Hand Wash Timer Code
arduino
Code to run this project, with as many comments as I thought were necessary.
1/*Code written by Danny Pashkow 2Updated November 2020*/ 3 4#include 5 <LiquidCrystal.h> 6#include "pitches.h" 7 8bool isHand = false; 9 10#define 11 screenPower 7 //define screen power pin (to automatically turn lcd backlight on 12 and off) 13 14// defines HC-SR04 pin numbers 15const int trigPin = 9; 16const 17 int echoPin = 10; 18 19//variables for HC-SR04 20long duration; 21int distance; 22 23 24// Create an LCD object. Parameters: (RS, E, D4, D5, D6, D7): 25LiquidCrystal 26 lcd = LiquidCrystal(13, 12, 5, 4, 3, 2); 27 28int timeLeft = 20; //variable for 29 displaying time left 30 31int speakerPin = 11; 32 33int toneDuration = 100; 34int 35 tickDuration = 25; 36 37//array of notes for done tone 38int doneTone[] = { 39 40 NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5 41}; 42 43//note 44 of the ticking sound 45int tick[] = { 46 NOTE_C5 47}; 48 49void setup() { 50 51 pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output 52 pinMode(echoPin, 53 INPUT); // Sets the echoPin as an Input 54 Serial.begin(9600); // Starts the serial 55 communication 56 57 //Setup LCD rows and columns: 58 pinMode(screenPower, 59 OUTPUT); 60 digitalWrite(screenPower, HIGH); 61 delay(200); 62 lcd.begin(16, 63 2); 64 lcd.setCursor(0, 0); 65 lcd.print("Hand Wash Timer"); 66 lcd.setCursor(0, 67 1); 68 lcd.print("By:Danny Pashkow"); 69 delay(3000); 70 lcd.clear(); 71 72 lcd.setCursor(0, 0); 73 digitalWrite(screenPower, LOW); 74} 75void loop() 76 { 77 // Clears the trigPin 78 digitalWrite(trigPin, LOW); 79 delayMicroseconds(2); 80 81 // Sets the trigPin on HIGH state for 10 micro seconds 82 digitalWrite(trigPin, 83 HIGH); 84 delayMicroseconds(10); 85 digitalWrite(trigPin, LOW); 86 // Reads 87 the echoPin, returns the sound wave travel time in microseconds 88 duration = 89 pulseIn(echoPin, HIGH); 90 // Calculating the distance 91 distance = duration 92 * 0.034 / 2; 93 // Prints the distance on the Serial Monitor 94 Serial.print("Distance: 95 "); 96 Serial.println(distance); 97 98 if (distance < 10) { 99 Serial.println("Hand 100 detected"); 101 isHand = true; 102 } 103 104 if (isHand == true) { 105 handDetected(); 106 107 } 108 109 if (timeLeft < 0) { 110 noTimeLeft(); 111 } 112} 113 114void noTimeLeft() 115 { 116 lcd.clear(); 117 lcd.setCursor(6, 0); 118 lcd.print("Done!"); 119 for 120 (int thisNote = 0; thisNote < 8; thisNote++) { 121 tone(11, doneTone[thisNote], 122 toneDuration); 123 delay(100); 124 } 125 delay(5000); 126 lcd.clear(); 127 128 digitalWrite(screenPower, LOW); 129 timeLeft = 20; 130 isHand = false; 131 lcd.setCursor(0, 132 0); 133} 134 135void handDetected() { 136 digitalWrite(screenPower, HIGH); 137 138 lcd.clear(); 139 lcd.setCursor(0, 0); 140 lcd.print("Time Remaining: "); 141 142 lcd.setCursor(0, 1); 143 lcd.print(timeLeft); 144 lcd.print(" seconds"); 145 146 timeLeft -= 1; 147 for (int thisTick = 0; thisTick < 3; thisTick++) { 148 tone(speakerPin, 149 tick[thisTick], tickDuration); 150 } 151 delay(1000); 152}
Pitches.h library
c_cpp
Save this code as "pitches.h" without quotations (make sure to change the file extension to .h) and put into a zip file. From there, you can import this library like any other library.
1/************************************************* 2 * Public Constants 3 *************************************************/ 4 5#define NOTE_B0 31 6#define NOTE_C1 33 7#define NOTE_CS1 35 8#define NOTE_D1 37 9#define NOTE_DS1 39 10#define NOTE_E1 41 11#define NOTE_F1 44 12#define NOTE_FS1 46 13#define NOTE_G1 49 14#define NOTE_GS1 52 15#define NOTE_A1 55 16#define NOTE_AS1 58 17#define NOTE_B1 62 18#define NOTE_C2 65 19#define NOTE_CS2 69 20#define NOTE_D2 73 21#define NOTE_DS2 78 22#define NOTE_E2 82 23#define NOTE_F2 87 24#define NOTE_FS2 93 25#define NOTE_G2 98 26#define NOTE_GS2 104 27#define NOTE_A2 110 28#define NOTE_AS2 117 29#define NOTE_B2 123 30#define NOTE_C3 131 31#define NOTE_CS3 139 32#define NOTE_D3 147 33#define NOTE_DS3 156 34#define NOTE_E3 165 35#define NOTE_F3 175 36#define NOTE_FS3 185 37#define NOTE_G3 196 38#define NOTE_GS3 208 39#define NOTE_A3 220 40#define NOTE_AS3 233 41#define NOTE_B3 247 42#define NOTE_C4 262 43#define NOTE_CS4 277 44#define NOTE_D4 294 45#define NOTE_DS4 311 46#define NOTE_E4 330 47#define NOTE_F4 349 48#define NOTE_FS4 370 49#define NOTE_G4 392 50#define NOTE_GS4 415 51#define NOTE_A4 440 52#define NOTE_AS4 466 53#define NOTE_B4 494 54#define NOTE_C5 523 55#define NOTE_CS5 554 56#define NOTE_D5 587 57#define NOTE_DS5 622 58#define NOTE_E5 659 59#define NOTE_F5 698 60#define NOTE_FS5 740 61#define NOTE_G5 784 62#define NOTE_GS5 831 63#define NOTE_A5 880 64#define NOTE_AS5 932 65#define NOTE_B5 988 66#define NOTE_C6 1047 67#define NOTE_CS6 1109 68#define NOTE_D6 1175 69#define NOTE_DS6 1245 70#define NOTE_E6 1319 71#define NOTE_F6 1397 72#define NOTE_FS6 1480 73#define NOTE_G6 1568 74#define NOTE_GS6 1661 75#define NOTE_A6 1760 76#define NOTE_AS6 1865 77#define NOTE_B6 1976 78#define NOTE_C7 2093 79#define NOTE_CS7 2217 80#define NOTE_D7 2349 81#define NOTE_DS7 2489 82#define NOTE_E7 2637 83#define NOTE_F7 2794 84#define NOTE_FS7 2960 85#define NOTE_G7 3136 86#define NOTE_GS7 3322 87#define NOTE_A7 3520 88#define NOTE_AS7 3729 89#define NOTE_B7 3951 90#define NOTE_C8 4186 91#define NOTE_CS8 4435 92#define NOTE_D8 4699 93#define NOTE_DS8 4978
Hand Wash Timer Code
arduino
Code to run this project, with as many comments as I thought were necessary.
1/*Code written by Danny Pashkow 2Updated November 2020*/ 3 4#include <LiquidCrystal.h> 5#include "pitches.h" 6 7bool isHand = false; 8 9#define screenPower 7 //define screen power pin (to automatically turn lcd backlight on and off) 10 11// defines HC-SR04 pin numbers 12const int trigPin = 9; 13const int echoPin = 10; 14 15//variables for HC-SR04 16long duration; 17int distance; 18 19// Create an LCD object. Parameters: (RS, E, D4, D5, D6, D7): 20LiquidCrystal lcd = LiquidCrystal(13, 12, 5, 4, 3, 2); 21 22int timeLeft = 20; //variable for displaying time left 23 24int speakerPin = 11; 25 26int toneDuration = 100; 27int tickDuration = 25; 28 29//array of notes for done tone 30int doneTone[] = { 31 NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5 32}; 33 34//note of the ticking sound 35int tick[] = { 36 NOTE_C5 37}; 38 39void setup() { 40 pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output 41 pinMode(echoPin, INPUT); // Sets the echoPin as an Input 42 Serial.begin(9600); // Starts the serial communication 43 44 //Setup LCD rows and columns: 45 pinMode(screenPower, OUTPUT); 46 digitalWrite(screenPower, HIGH); 47 delay(200); 48 lcd.begin(16, 2); 49 lcd.setCursor(0, 0); 50 lcd.print("Hand Wash Timer"); 51 lcd.setCursor(0, 1); 52 lcd.print("By:Danny Pashkow"); 53 delay(3000); 54 lcd.clear(); 55 lcd.setCursor(0, 0); 56 digitalWrite(screenPower, LOW); 57} 58void loop() { 59 // Clears the trigPin 60 digitalWrite(trigPin, LOW); 61 delayMicroseconds(2); 62 // Sets the trigPin on HIGH state for 10 micro seconds 63 digitalWrite(trigPin, HIGH); 64 delayMicroseconds(10); 65 digitalWrite(trigPin, LOW); 66 // Reads the echoPin, returns the sound wave travel time in microseconds 67 duration = pulseIn(echoPin, HIGH); 68 // Calculating the distance 69 distance = duration * 0.034 / 2; 70 // Prints the distance on the Serial Monitor 71 Serial.print("Distance: "); 72 Serial.println(distance); 73 74 if (distance < 10) { 75 Serial.println("Hand detected"); 76 isHand = true; 77 } 78 79 if (isHand == true) { 80 handDetected(); 81 } 82 83 if (timeLeft < 0) { 84 noTimeLeft(); 85 } 86} 87 88void noTimeLeft() { 89 lcd.clear(); 90 lcd.setCursor(6, 0); 91 lcd.print("Done!"); 92 for (int thisNote = 0; thisNote < 8; thisNote++) { 93 tone(11, doneTone[thisNote], toneDuration); 94 delay(100); 95 } 96 delay(5000); 97 lcd.clear(); 98 digitalWrite(screenPower, LOW); 99 timeLeft = 20; 100 isHand = false; 101 lcd.setCursor(0, 0); 102} 103 104void handDetected() { 105 digitalWrite(screenPower, HIGH); 106 lcd.clear(); 107 lcd.setCursor(0, 0); 108 lcd.print("Time Remaining: "); 109 lcd.setCursor(0, 1); 110 lcd.print(timeLeft); 111 lcd.print(" seconds"); 112 timeLeft -= 1; 113 for (int thisTick = 0; thisTick < 3; thisTick++) { 114 tone(speakerPin, tick[thisTick], tickDuration); 115 } 116 delay(1000); 117}
Downloadable files
Hand Wash Timer Schematic
Hand Wash Timer Schematic
Hand Wash Timer Schematic
Hand Wash Timer Schematic
Comments
Only logged in users can leave comments
dlpashkow
0 Followers
•0 Projects
Table of contents
Intro
0
0