├── README.md └── Sanitizer .ino /README.md: -------------------------------------------------------------------------------- 1 | # Sanitizer-with-Arduino 2 | By using Arduino, Pump, Ultrasonic Sensor, Breadboard, Jumpers and Resistors 3 | -------------------------------------------------------------------------------- /Sanitizer .ino: -------------------------------------------------------------------------------- 1 | //Ultasonic Sensor 2 | 3 | 4 | //Pins connected to the ultrasonic sensor 5 | 6 | #define trigPin 2 7 | 8 | #define echoPin 3 9 | 10 | #define pump 8 11 | 12 | int range = 5;//range in inches 13 | 14 | 15 | void setup() { 16 | 17 | // initialize serial communication: 18 | 19 | Serial.begin(9600); 20 | 21 | //initialize the sensor pins 22 | 23 | pinMode(trigPin, OUTPUT); 24 | 25 | pinMode(echoPin, INPUT); 26 | 27 | //initialize LED pins 28 | 29 | pinMode(pump, OUTPUT); 30 | 31 | digitalWrite(pump, LOW); 32 | 33 | 34 | 35 | } 36 | 37 | void loop() 38 | 39 | { 40 | 41 | // establish variables for duration of the ping, 42 | 43 | // and the distance result in inches and centimeters: 44 | 45 | long duration, inches, cm; 46 | 47 | 48 | // The PING))) is triggered by a HIGH pulse of 2 or more microseconds. 49 | 50 | // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: 51 | 52 | digitalWrite(trigPin, LOW); 53 | 54 | delayMicroseconds(2); 55 | 56 | digitalWrite(trigPin, HIGH); 57 | 58 | delayMicroseconds(10 ); 59 | 60 | digitalWrite(trigPin, LOW); 61 | 62 | 63 | // Take reading on echo pin 64 | 65 | duration = pulseIn(echoPin, HIGH); 66 | 67 | 68 | // convert the time into a distance 69 | 70 | inches = microsecondsToInches(duration); 71 | 72 | cm = microsecondsToCentimeters(duration); 73 | 74 | 75 | 76 | Serial.print(inches); 77 | 78 | Serial.print("in, "); 79 | 80 | Serial.print(cm); 81 | 82 | Serial.print("cm"); 83 | 84 | Serial.println(); 85 | 86 | 87 | 88 | if(inches < 5) { 89 | 90 | Serial.println("hand puted"); 91 | 92 | 93 | digitalWrite(pump, HIGH); 94 | 95 | 96 | 97 | delay(100); 98 | 99 | } else { 100 | 101 | Serial.println("no hand"); 102 | 103 | digitalWrite(pump, LOW); 104 | 105 | delay(100); 106 | 107 | } 108 | 109 | 110 | 111 | delay(200); 112 | 113 | } 114 | 115 | 116 | long microsecondsToInches(long microseconds) 117 | 118 | { 119 | 120 | 121 | return microseconds / 74 / 2; 122 | 123 | } 124 | 125 | 126 | long microsecondsToCentimeters(long microseconds) 127 | 128 | { 129 | 130 | // The speed of sound is 340 m/s or 29 microseconds per centimeter. 131 | 132 | // The ping travels out and back, so to find the distance of the 133 | 134 | // object we take half of the distance travelled. 135 | 136 | return microseconds / 29 / 2; 137 | 138 | } 139 | --------------------------------------------------------------------------------