-
Notifications
You must be signed in to change notification settings - Fork 0
/
wright_arduinofinal
61 lines (39 loc) · 1.39 KB
/
wright_arduinofinal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <Servo.h>
Servo myservo; // create servo object to control a servo
#define ServoM 12 //Connected to the servo motor.
#define In 8 //Pin connected to the IN button.
#define BarLow 180 //Low position of the barrier.
#define BarUp -90 //Up position of the barrier.
#define trigPin 7 //TRING PIN 7
#define echoPin 6 //ECHO PIN 6
//================================================================
void setup(){
myservo.attach(ServoM); // attaches the servo.
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(In, INPUT); // set "IN" button pin to input
digitalWrite(In, HIGH); // Connect Pull-Up resistor.
myservo.write(BarLow); //Barrier in the low position
// delay(15);
}
//================================================================
void loop(){
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
//================================================================
if(digitalRead(In)==0)
{
myservo.write(BarLow);
}
if (distance <6)
{
myservo.write(BarUp);}
}
//================================================================