Devices & Components
Arduino Uno Rev3
SG90 Micro-servo motor
LED, Blue Green
Ultrasonic Sensor - HC-SR04 (Generic)
Software & Tools
Arduino IDE
Project description
Code
project_4.ino
c_cpp
1//Arduino project 4.Automatic open gate. 2 3#define trig 2 4#define echo 4 5#define led 8 6 7//Servo 8#include <Servo.h> 9 10Servo gate; 11 12 13void setup() { 14 Serial.begin(9600); 15 16 pinMode(trig, OUTPUT); 17 pinMode(echo, INPUT); 18 pinMode(led,OUTPUT); 19 20 gate.attach(9); 21 22 23} 24 25void loop() { 26 27//ultra sonic sensor 28 29 digitalWrite(trig,LOW); 30 delayMicroseconds(2); 31 digitalWrite(trig,HIGH); 32 delayMicroseconds(10); 33 digitalWrite(trig,LOW); 34 35 long t =pulseIn(echo,HIGH); 36 37 long cm = t /29 / 2; 38 39 Serial.print(cm); 40 Serial.println("cm"); 41 42//led 43 if (cm <=19){ 44 digitalWrite(led, HIGH); 45 46 } 47 else{ 48 digitalWrite(led,LOW); 49 } 50 51 //Gate 52 53 gate.write(170); 54 55 if (cm<= 19){ 56 57 for( int j=170;j>=90;j--){ 58 gate.write(j); 59 delay(50); 60 } 61 62 delay(2500); 63 64 for( int i=90;i<=170;i++){ 65 gate.write(i); 66 delay(50); 67 } 68 69 } 70 else{ 71 gate.write(170); 72 } 73 74} 75
Downloadable files
Circuit diagram
Circuit diagram
Circuit diagram
Circuit diagram
Comments
Only logged in users can leave comments