Devices & Components
Arduino Uno Rev3
Capacitor 1000 µF
MG996 servo motor
Ultrasonic Sensor - HC-SR04 (Generic)
Male/Male Jumper Wires
Male/Female Jumper Wires
Hardware & Tools
3D Printer (generic)
Laser cutter (generic)
Software & Tools
Arduino IDE
Project description
Code
Code
c_cpp
1/* 2echo = 3 3trig = 4 4servo = 5 5*/ 6 7#include <Servo.h> 8Servo myservo; 9 10int first_angle = 100; //you can change the value. 11int second_angle = 50; //you can change the value. 12int detection_distance = 20; //(cm)you can change the value. 13 14int sw = 12; //Switch 15int echoPin = 3; //ultrasonic sensor echoPIN 16int trigPin = 4; //ultrasonic sensor trigPIN 17 18void setup() { 19 myservo.attach(5); 20 myservo.write(first_angle); 21 pinMode(sw, INPUT_PULLUP); 22 pinMode(trigPin, OUTPUT); 23 pinMode(echoPin, INPUT); 24} 25 26void loop(){ 27 digitalWrite(trigPin, LOW); 28 digitalWrite(echoPin, LOW); 29 delayMicroseconds(2); 30 digitalWrite(trigPin, HIGH); 31 delayMicroseconds(10); 32 digitalWrite(trigPin, LOW); 33 34 unsigned long duration = pulseIn(echoPin, HIGH); 35 float distance = duration / 29.0 / 2.0; 36 37 if (digitalRead(sw) == LOW){ 38 if (distance <= detection_distance){ 39 myservo.write(second_angle); 40 delay(400); 41 myservo.write(first_angle); 42 delay(800); 43 } 44 } 45 else{ 46 myservo.write(first_angle); 47 } 48}
Code
c_cpp
1/* 2echo = 3 3trig = 4 4servo = 5 5*/ 6 7#include <Servo.h> 8Servo myservo; 9 10int first_angle = 100; //you can change the value. 11int second_angle = 50; //you can change the value. 12int detection_distance = 20; //(cm)you can change the value. 13 14int sw = 12; //Switch 15int echoPin = 3; //ultrasonic sensor echoPIN 16int trigPin = 4; //ultrasonic sensor trigPIN 17 18void setup() { 19 myservo.attach(5); 20 myservo.write(first_angle); 21 pinMode(sw, INPUT_PULLUP); 22 pinMode(trigPin, OUTPUT); 23 pinMode(echoPin, INPUT); 24} 25 26void loop(){ 27 digitalWrite(trigPin, LOW); 28 digitalWrite(echoPin, LOW); 29 delayMicroseconds(2); 30 digitalWrite(trigPin, HIGH); 31 delayMicroseconds(10); 32 digitalWrite(trigPin, LOW); 33 34 unsigned long duration = pulseIn(echoPin, HIGH); 35 float distance = duration / 29.0 / 2.0; 36 37 if (digitalRead(sw) == LOW){ 38 if (distance <= detection_distance){ 39 myservo.write(second_angle); 40 delay(400); 41 myservo.write(first_angle); 42 delay(800); 43 } 44 } 45 else{ 46 myservo.write(first_angle); 47 } 48}
Downloadable files
Circuit
Circuit

Documentation
STL FILE
STL FILE
STL FILE
STL FILE
STL FILE
STL FILE
STL FILE
STL FILE
STL FILE
STL FILE
STL FILE
STL FILE
STL FILE
STL FILE
STL FILE
STL FILE
Comments
Only logged in users can leave comments