Components and supplies
Speaker: 0.25W, 8 ohms
Breadboard (generic)
Ultrasonic Sensor - HC-SR04 (Generic)
Resistor 221 ohm
5V 2.5A Switching Power Supply
LED (generic)
Arduino UNO
Adafruit FX Soundboard 2mb
Male/Female Jumper Wires
Project description
Code
Arduino code
arduino
Arduino IDE code
1 2 /* HC-SR04 example sketch 3 * 4 * https://create.arduino.cc/projecthub/Isaac100/getting-started-with-the-hc-sr04-ultrasonic-sensor-036380 5 * 6 * by Isaac100 7 Code 8 9First we define the pins that Trig and Echo are connected to. 10 11const int trigPin = 9; 12const int echoPin = 10; 13Then we declare 2 floats, duration and distance, which will hold the length of the sound wave and how far away the object is. 14 15float duration, distance; 16Next, in the setup, we declare the Trig pin as an output, the Echo pin as an input, and start Serial communications. 17 18void setup() { 19 pinMode(trigPin, OUTPUT); 20 pinMode(echoPin, INPUT); 21 Serial.begin(9600); 22} 23Now, in the loop, what we do is first set the trigPin low for 2 microseconds just to make sure that the pin in low first. Then, we set it high for 10 microseconds, which sends out an 8 cycle sonic burst from the transmitter, which then bounces of an object and hits the receiver(Which is connected to the Echo Pin). 24 25void loop() { 26 digitalWrite(trigPin, LOW); 27 delayMicroseconds(2); 28 digitalWrite(trigPin, HIGH); 29 delayMicroseconds(10); 30 digitalWrite(trigPin, LOW); 31When the sound waves hit the receiver, it turns the Echo pin high for however long the waves were traveling for. To get that, we can use a handy Arduino function called pulseIn(). It takes 2 arguments, the pin you are listening to(In our case, the Echo pin), and a state(HIGH or LOW). What the function does is waits for the pin to go whichever state you put in, starts timing, and then stops timing when it switches to the other state. In our case we would put HIGH since we want to start timing when the Echo pin goes high. We will store the time in the duration variable. (It returns the time in microseconds) 32 33duration = pulseIn(echoPin, HIGH); 34Now that we have the time, we can use the equation speed = distance/time, but we will make it time x speed = distance because we have the speed. What speed do we have? The speed of sound, of course! The speed of sound is approximately 340 meters per second, but since the pulseIn() function returns the time in microseconds, we will need to have a speed in microseconds also, which is easy to get. A quick Google search for "speed of sound in centimeters per microsecond" will say that it is .0343 c/S. You could do the math, but searching it is easier. Anyway, with that information, we can calculate the distance! Just multiply the duration by .0343 and then divide it by 2(Because the sound waves travel to the object AND back). We will store that in the distance variable. 35 36distance = (duration*.0343)/2; 37The rest is just printing out the results to the Serial Monitor. 38 39 Serial.print("Distance: "); 40 Serial.println(distance); 41 delay(100); 42} 43 */ 44 45#define DDT // define to turn on debugging tools 46void DDTv(String st, int vt) { // Print descriptor and value 47 #ifdef DDT 48 Serial.print(" "); 49 Serial.print(st); 50 Serial.print(" "); 51 Serial.print(vt);} 52 #endif 53void DDTl(String st, int vt) { // Print descriptor and value and new line 54 #ifdef DDT 55 DDTv(st, vt); 56 Serial.println(" ");} 57 #endif 58void DDTs(String st) { // Print string 59 #ifdef DDT 60 Serial.print(st);} 61 #endif 62void DDTt(String st) { // Print string and new line 63 #ifdef DDT 64 Serial.println(st);} 65 #endif 66void DDTf(String st, float fi) { // Print descriptor and floating point value and new line 67 #ifdef DDT 68 Serial.print(" "); 69 Serial.print(st); 70 Serial.print(" "); 71 Serial.println(fi,4);} 72 #endif 73void DDTsl(String st, String vt) { // Print descriptor and string value and new line 74 #ifdef DDT 75 Serial.print(st); 76 Serial.println(vt);} 77 #endif 78 79const int trigPin = 10; 80const int echoPin = 11; 81const int Wash_Time = 20; // seconds to wash hands 82const int LED_Qty = 8; // number of LED_Pin 83const int LED_Pin = 2; // first LED pin 84const int soundpin = 12; // trigger for song 85const int actpin = 13; // will be "HIGH" while playing 86const int Trigger_Distance = 50; // closeness to trigger a wash 87 88float duration, distance; 89 90void setup() { 91 pinMode(trigPin, OUTPUT); 92 pinMode(echoPin, INPUT); 93 pinMode(actpin, INPUT); 94 pinMode(soundpin, OUTPUT); 95 digitalWrite(soundpin,HIGH); 96 for (int i = 0; i < LED_Qty; i++) {digitalWrite (LED_Pin+i, LOW);} 97 98 Serial.begin(9600); 99 100} // end of setup 101 102void loop() { 103 digitalWrite(trigPin, LOW); 104 delayMicroseconds(2); 105 digitalWrite(trigPin, HIGH); 106 delayMicroseconds(10); 107 digitalWrite(trigPin, LOW); 108 109 duration = pulseIn(echoPin, HIGH); 110 distance = (duration*.0343)/2; // convert using speed of sound to centimeters 111 Serial.print("Distance: "); 112 Serial.println(distance); 113 if (distance <= Trigger_Distance) {Wash_Them();} 114 delay(100); 115} // end of loop 116 117 void Wash_Them() { 118 DDTt("Wash Them Called"); 119 Flash(3); // flash for two seconds 120 digitalWrite(soundpin,LOW); //trigger the song 121 delay(150); //hold just a bit then 122 digitalWrite(soundpin,HIGH); // and turn it off (it will play through) 123 do { 124 for (int i = 0; i < LED_Qty; i++) { 125 digitalWrite (LED_Pin+i, HIGH); 126 delay(400); 127 digitalWrite (LED_Pin+i, LOW); 128 } // LED loop 129 DDTl("Active pin is ",digitalRead(actpin)); 130 } while (digitalRead(actpin) == LOW); // play till the song is done 131 Flash(4); // flash again for two seconds 132 133} // end of Wash_Them 134 135 136 void Flash(int flashtime){ 137 int ftimer = flashtime; 138 for (int ii = 0; ii< flashtime; ii++){ 139 for (int i = 0; i < LED_Qty; i++) { 140 digitalWrite (LED_Pin+i, HIGH);} 141 delay(500); 142 for (int i = 0; i < LED_Qty; i++) { 143 digitalWrite (LED_Pin+i, LOW); } 144 delay(500); 145 } 146 } // end of flash 147
Arduino code
arduino
Arduino IDE code
1 2 /* HC-SR04 example sketch 3 * 4 * https://create.arduino.cc/projecthub/Isaac100/getting-started-with-the-hc-sr04-ultrasonic-sensor-036380 5 6 * 7 * by Isaac100 8 Code 9 10First we define the pins that Trig and Echo 11 are connected to. 12 13const int trigPin = 9; 14const int echoPin = 10; 15Then 16 we declare 2 floats, duration and distance, which will hold the length of the sound 17 wave and how far away the object is. 18 19float duration, distance; 20Next, 21 in the setup, we declare the Trig pin as an output, the Echo pin as an input, and 22 start Serial communications. 23 24void setup() { 25 pinMode(trigPin, OUTPUT); 26 27 pinMode(echoPin, INPUT); 28 Serial.begin(9600); 29} 30Now, in the loop, 31 what we do is first set the trigPin low for 2 microseconds just to make sure that 32 the pin in low first. Then, we set it high for 10 microseconds, which sends out 33 an 8 cycle sonic burst from the transmitter, which then bounces of an object and 34 hits the receiver(Which is connected to the Echo Pin). 35 36void loop() { 37 38 digitalWrite(trigPin, LOW); 39 delayMicroseconds(2); 40 digitalWrite(trigPin, 41 HIGH); 42 delayMicroseconds(10); 43 digitalWrite(trigPin, LOW); 44When the 45 sound waves hit the receiver, it turns the Echo pin high for however long the waves 46 were traveling for. To get that, we can use a handy Arduino function called pulseIn(). 47 It takes 2 arguments, the pin you are listening to(In our case, the Echo pin), and 48 a state(HIGH or LOW). What the function does is waits for the pin to go whichever 49 state you put in, starts timing, and then stops timing when it switches to the other 50 state. In our case we would put HIGH since we want to start timing when the Echo 51 pin goes high. We will store the time in the duration variable. (It returns the 52 time in microseconds) 53 54duration = pulseIn(echoPin, HIGH); 55Now that we 56 have the time, we can use the equation speed = distance/time, but we will make it 57 time x speed = distance because we have the speed. What speed do we have? The speed 58 of sound, of course! The speed of sound is approximately 340 meters per second, 59 but since the pulseIn() function returns the time in microseconds, we will need 60 to have a speed in microseconds also, which is easy to get. A quick Google search 61 for "speed of sound in centimeters per microsecond" will say that it is .0343 62 c/S. You could do the math, but searching it is easier. Anyway, with that information, 63 we can calculate the distance! Just multiply the duration by .0343 and then divide 64 it by 2(Because the sound waves travel to the object AND back). We will store that 65 in the distance variable. 66 67distance = (duration*.0343)/2; 68The rest is 69 just printing out the results to the Serial Monitor. 70 71 Serial.print("Distance: 72 "); 73 Serial.println(distance); 74 delay(100); 75} 76 */ 77 78#define 79 DDT // define to turn on debugging tools 80void 81 DDTv(String st, int vt) { // Print descriptor and value 82 #ifdef 83 DDT 84 Serial.print(" "); 85 Serial.print(st); 86 Serial.print(" 87 "); 88 Serial.print(vt);} 89 #endif 90void DDTl(String st, int vt) { 91 // Print descriptor and value and new line 92 #ifdef DDT 93 DDTv(st, 94 vt); 95 Serial.println(" ");} 96 #endif 97void DDTs(String st) { // 98 Print string 99 #ifdef DDT 100 Serial.print(st);} 101 #endif 102void DDTt(String 103 st) { // Print string and new line 104 #ifdef DDT 105 Serial.println(st);} 106 107 #endif 108void DDTf(String st, float fi) { // Print descriptor and 109 floating point value and new line 110 #ifdef DDT 111 Serial.print(" "); 112 113 Serial.print(st); 114 Serial.print(" "); 115 Serial.println(fi,4);} 116 117 #endif 118void DDTsl(String st, String vt) { // 119 Print descriptor and string value and new line 120 #ifdef DDT 121 Serial.print(st); 122 123 Serial.println(vt);} 124 #endif 125 126const int trigPin = 10; 127const 128 int echoPin = 11; 129const int Wash_Time = 20; // seconds to wash hands 130const 131 int LED_Qty = 8; // number of LED_Pin 132const int LED_Pin = 2; // 133 first LED pin 134const int soundpin = 12; // trigger for song 135const 136 int actpin = 13; // will be "HIGH" while playing 137const int 138 Trigger_Distance = 50; // closeness to trigger a wash 139 140float duration, 141 distance; 142 143void setup() { 144 pinMode(trigPin, OUTPUT); 145 pinMode(echoPin, 146 INPUT); 147 pinMode(actpin, INPUT); 148 pinMode(soundpin, OUTPUT); 149 digitalWrite(soundpin,HIGH); 150 151 for (int i = 0; i < LED_Qty; i++) {digitalWrite (LED_Pin+i, LOW);} 152 153 Serial.begin(9600); 154 155 156} // end of setup 157 158void loop() { 159 digitalWrite(trigPin, LOW); 160 161 delayMicroseconds(2); 162 digitalWrite(trigPin, HIGH); 163 delayMicroseconds(10); 164 165 digitalWrite(trigPin, LOW); 166 167 duration = pulseIn(echoPin, HIGH); 168 distance 169 = (duration*.0343)/2; // convert using speed 170 of sound to centimeters 171 Serial.print("Distance: "); 172 Serial.println(distance); 173 174 if (distance <= Trigger_Distance) {Wash_Them();} 175 delay(100); 176} // end 177 of loop 178 179 void Wash_Them() { 180 DDTt("Wash Them Called"); 181 Flash(3); 182 // flash for two seconds 183 184 digitalWrite(soundpin,LOW); //trigger the 185 song 186 delay(150); //hold 187 just a bit then 188 digitalWrite(soundpin,HIGH); // 189 and turn it off (it will play through) 190 do { 191 for (int i = 0; i < 192 LED_Qty; i++) { 193 digitalWrite (LED_Pin+i, HIGH); 194 delay(400); 195 196 digitalWrite (LED_Pin+i, LOW); 197 } // LED loop 198 DDTl("Active 199 pin is ",digitalRead(actpin)); 200 } while (digitalRead(actpin) == LOW); // 201 play till the song is done 202 Flash(4); // 203 flash again for two seconds 204 205} // end of Wash_Them 206 207 208 void Flash(int 209 flashtime){ 210 int ftimer = flashtime; 211 for (int ii = 0; ii< flashtime; 212 ii++){ 213 for (int i = 0; i < LED_Qty; i++) { 214 digitalWrite (LED_Pin+i, 215 HIGH);} 216 delay(500); 217 for (int i = 0; i < LED_Qty; i++) { 218 digitalWrite 219 (LED_Pin+i, LOW); } 220 delay(500); 221 } 222 } // end of flash 223
Downloadable files
Hand Wash Timer connections
Hand Wash Timer connections
Comments
Only logged in users can leave comments
spudnut1
0 Followers
•0 Projects
Table of contents
Intro
3
0