forked from rickymedrano/doorman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoorman.ino
142 lines (129 loc) · 3.87 KB
/
Doorman.ino
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*This Arduino code is for the "Doorman" project I recreated. The original "Doorman" project can be seen here
* https://www.thingiverse.com/thing:2753756
* This is updated and cleaned up code that is easier to read and works for my servo,LED,and sensor set up.
*/
#include <Servo.h>
#define ENABLE_PIR false
const int openEyesTimeMs = 2000;
const int timeBetweenRutineMs = 6000;
//Define pins that servos are hooked up to on Arduino Nano
const int servoPinEyeLids = 3;
const int servoPinRightEyeX = 6;
const int servoPinLeftEyeX = 5;
const int servoPinRightEyeY = 10;
const int servoPinLeftEyeY = 9;
//Define servo objects
Servo servoEyeLids;
Servo servoRightEyeY;
Servo servoLeftEyeY;
Servo servoRightEyeX;
Servo servoLeftEyeX;
//Define pin for 2 LEDs hooked in parallel
const int pinLED = 13;
const int inputPinPIR = 2; // Define pin for PIR sensor
int statePIR = LOW; // Define initial PIR sensor state
int readStatePIR = 0; // Variable to hold read in state from PIR sensor
void setup() {
//Declare PIR sensor as an INPUT
pinMode(inputPinPIR, INPUT);
//Declare LEDs as OUTPUT as we are sending it a signal, not reading in a signal
pinMode(pinLED, OUTPUT);
//Attach servo objects to correct pins on Arduino Nano
servoEyeLids.attach(servoPinEyeLids);
servoRightEyeX.attach(servoPinRightEyeX);
servoLeftEyeX.attach(servoPinLeftEyeX);
servoRightEyeY.attach(servoPinRightEyeY);
servoLeftEyeY.attach(servoPinLeftEyeY);
/*Set initial positions of SG90 micro servos.
These positions depend how you hook up the attachments to the servos,
therefore most likely will be different for you.*/
servoEyeLids.write(150);
servoRightEyeX.write(95);
servoLeftEyeX.write(100);
servoRightEyeY.write(75);
servoLeftEyeY.write(50);
//For debugging purposes, you can use print statements to to make sure you're reaching certain parts of the code
Serial.begin(9600);
Serial.println("Start");
}
void loop() {
if (ENABLE_PIR) {
//Read the state of the PIR sensor to check for motion
readStatePIR = digitalRead(inputPinPIR);
if (readStatePIR == HIGH) { //motion was detected
//Turn on LEDs to light up the eyes
digitalWrite(pinLED, HIGH);
if (statePIR == LOW) { //check we weren't initiating eye movements before this
//Iniatate eye movements
startMovementRutine();
//Set PIR state to HIGH so eye movements dont keep looping
statePIR = HIGH;
}
} else {
if (statePIR == HIGH) { //we just went through eye movements 1 time
endMovementRutine();
statePIR = LOW;
}
}
} else {
startMovementRutine();
delay(openEyesTimeMs);
endMovementRutine();
delay(timeBetweenRutineMs);
}
}
void startMovementRutine() {
openEyeLids();
delay(2000);
turnEyesLeft();
delay(1000);
turnEyesRight();
delay(1000);
turnEyesStraight();
delay(1000);
turnEyesUpAndLeft();
delay(2000);
turnEyesUpAndRight();
delay(1000);
turnEyesStraight();
delay(1000);
}
void endMovementRutine() {
delay(3000);
//Turn off LEDs
digitalWrite(pinLED, LOW);
closeEyeLids();
delay(1000);
}
void openEyeLids() {
servoEyeLids.write(160);
}
void closeEyeLids() {
servoEyeLids.write(80);
}
void turnEyesLeft() {
servoRightEyeX.write(125);
servoLeftEyeX.write(130);
}
void turnEyesRight() {
servoRightEyeX.write(56);
servoLeftEyeX.write(66);
}
void turnEyesStraight() {
servoRightEyeY.write(75);
servoRightEyeX.write(95);
servoLeftEyeY.write(50);
servoLeftEyeX.write(100);
}
void turnEyesUpAndLeft() {
servoRightEyeY.write(50);
servoRightEyeX.write(120);
servoLeftEyeY.write(80);
servoLeftEyeX.write(120);
}
void turnEyesUpAndRight() {
servoRightEyeY.write(50);
servoRightEyeX.write(75);
servoLeftEyeY.write(80);
servoLeftEyeX.write(75);
}