Components and supplies
Buzzer
Resistor 220 ohm
Linear Solenoid, 12 VDC
LED (generic)
Ultrasonic Sensor - HC-SR04 (Generic)
Arduino UNO
Project description
Code
Code
arduino
1const int trigPin = 10; 2const int echoPin = 11; 3const int buzzer = 12; 4const int lock = 4; 5const int led = 13; 6 7float duration, distance, note, number; 8 9 10void setup() 11{ 12 pinMode(trigPin, OUTPUT); 13 pinMode(echoPin, INPUT); 14 pinMode(buzzer, OUTPUT); // Set buzzer - pin 12 as an output 15 pinMode(lock, OUTPUT); 16 pinMode(led, OUTPUT); 17 Serial.begin(9600); 18} 19 20void loop() 21{ 22 digitalWrite(trigPin, LOW); 23 delayMicroseconds(2); 24 digitalWrite(trigPin, HIGH); 25 delayMicroseconds(10); 26 digitalWrite(trigPin, LOW); 27 28 duration = pulseIn(echoPin, HIGH); 29 number = (duration*.0343)/2; 30 distance = round(number); 31 delay(100); 32 Serial.print("Distance: "); 33 Serial.println(distance); 34 35 if(distance < 3) 36 { 37 delay(1000); 38 Serial.print("START"); 39 Sequence(); 40 41 } 42} 43 44 45void Sequence() 46 { 47 digitalWrite(lock, LOW); 48 digitalWrite(led, LOW); 49 delay(2000); 50 if(distance < 5) 51 { 52 delay(1000); 53 tone(buzzer, 262); 54 Serial.print("C"); 55 delay(2000); 56 digitalWrite(trigPin, LOW); 57 delayMicroseconds(2); 58 digitalWrite(trigPin, HIGH); 59 delayMicroseconds(10); 60 digitalWrite(trigPin, LOW); 61 62 duration = pulseIn(echoPin, HIGH); 63 number = (duration*.0343)/2; 64 distance = round(number); 65 delay(100); 66 Serial.print("Distance: "); 67 Serial.println(distance); 68 69 if(distance == 5) 70 { 71 delay(1000); 72 tone(buzzer, 294); 73 Serial.print("D"); 74 delay(2000); 75 digitalWrite(trigPin, LOW); 76 delayMicroseconds(2); 77 digitalWrite(trigPin, HIGH); 78 delayMicroseconds(10); 79 digitalWrite(trigPin, LOW); 80 81 duration = pulseIn(echoPin, HIGH); 82 number = (duration*.0343)/2; 83 distance = round(number); 84 delay(100); 85 Serial.print("Distance: "); 86 Serial.println(distance); 87 88 if(distance >= 6) 89 { 90 delay(1000); 91 tone(buzzer, 330); 92 Serial.print("It Worked"); 93 digitalWrite(lock, HIGH); 94 digitalWrite(led, HIGH); 95 delay(1000); 96 noTone(buzzer); 97 } 98 99 else if(distance < 6) 100 { 101 Serial.print("Didn't work 2"); 102 tone(buzzer, 262); 103 delay(500); 104 noTone(buzzer); 105 delay(500); 106 tone(buzzer, 262); 107 delay(500); 108 noTone(buzzer); 109 delay(500); 110 tone(buzzer, 262); 111 delay(500); 112 noTone(buzzer); 113 } 114 } 115 else if((distance < 5) || (distance >6)) 116 { 117 Serial.print("Didn't work 1"); 118 tone(buzzer, 262); 119 delay(500); 120 noTone(buzzer); 121 delay(500); 122 tone(buzzer, 262); 123 delay(500); 124 noTone(buzzer); 125 delay(500); 126 tone(buzzer, 262); 127 delay(500); 128 noTone(buzzer); 129 } 130 } 131 132 else 133 { 134 Serial.print("Restart"); 135 } 136 }
Code
arduino
1const int trigPin = 10; 2const int echoPin = 11; 3const int buzzer = 12; 4const int lock = 4; 5const int led = 13; 6 7float duration, distance, note, number; 8 9 10void setup() 11{ 12 pinMode(trigPin, OUTPUT); 13 pinMode(echoPin, INPUT); 14 pinMode(buzzer, OUTPUT); // Set buzzer - pin 12 as an output 15 pinMode(lock, OUTPUT); 16 pinMode(led, OUTPUT); 17 Serial.begin(9600); 18} 19 20void loop() 21{ 22 digitalWrite(trigPin, LOW); 23 delayMicroseconds(2); 24 digitalWrite(trigPin, HIGH); 25 delayMicroseconds(10); 26 digitalWrite(trigPin, LOW); 27 28 duration = pulseIn(echoPin, HIGH); 29 number = (duration*.0343)/2; 30 distance = round(number); 31 delay(100); 32 Serial.print("Distance: "); 33 Serial.println(distance); 34 35 if(distance < 3) 36 { 37 delay(1000); 38 Serial.print("START"); 39 Sequence(); 40 41 } 42} 43 44 45void Sequence() 46 { 47 digitalWrite(lock, LOW); 48 digitalWrite(led, LOW); 49 delay(2000); 50 if(distance < 5) 51 { 52 delay(1000); 53 tone(buzzer, 262); 54 Serial.print("C"); 55 delay(2000); 56 digitalWrite(trigPin, LOW); 57 delayMicroseconds(2); 58 digitalWrite(trigPin, HIGH); 59 delayMicroseconds(10); 60 digitalWrite(trigPin, LOW); 61 62 duration = pulseIn(echoPin, HIGH); 63 number = (duration*.0343)/2; 64 distance = round(number); 65 delay(100); 66 Serial.print("Distance: "); 67 Serial.println(distance); 68 69 if(distance == 5) 70 { 71 delay(1000); 72 tone(buzzer, 294); 73 Serial.print("D"); 74 delay(2000); 75 digitalWrite(trigPin, LOW); 76 delayMicroseconds(2); 77 digitalWrite(trigPin, HIGH); 78 delayMicroseconds(10); 79 digitalWrite(trigPin, LOW); 80 81 duration = pulseIn(echoPin, HIGH); 82 number = (duration*.0343)/2; 83 distance = round(number); 84 delay(100); 85 Serial.print("Distance: "); 86 Serial.println(distance); 87 88 if(distance >= 6) 89 { 90 delay(1000); 91 tone(buzzer, 330); 92 Serial.print("It Worked"); 93 digitalWrite(lock, HIGH); 94 digitalWrite(led, HIGH); 95 delay(1000); 96 noTone(buzzer); 97 } 98 99 else if(distance < 6) 100 { 101 Serial.print("Didn't work 2"); 102 tone(buzzer, 262); 103 delay(500); 104 noTone(buzzer); 105 delay(500); 106 tone(buzzer, 262); 107 delay(500); 108 noTone(buzzer); 109 delay(500); 110 tone(buzzer, 262); 111 delay(500); 112 noTone(buzzer); 113 } 114 } 115 else if((distance < 5) || (distance >6)) 116 { 117 Serial.print("Didn't work 1"); 118 tone(buzzer, 262); 119 delay(500); 120 noTone(buzzer); 121 delay(500); 122 tone(buzzer, 262); 123 delay(500); 124 noTone(buzzer); 125 delay(500); 126 tone(buzzer, 262); 127 delay(500); 128 noTone(buzzer); 129 } 130 } 131 132 else 133 { 134 Serial.print("Restart"); 135 } 136 }
Downloadable files
Schematic
Schematic
Schematic
Schematic
Solenoid Schematic
Solenoid Schematic
Comments
Only logged in users can leave comments
Anonymous user
4 years ago
Can you share a demo video of this? Thank you
oquinn
4 years ago
I strive to be this smart
mmg220
1 Followers
•1 Projects
7
3
Musical Door Lock | Arduino Project Hub
oquinn
2 years ago
I strive to be this smart