-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomework2-ElevatorWannabe.ino
217 lines (169 loc) · 5.78 KB
/
Homework2-ElevatorWannabe.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const int buzzerPin = 10;
int buzzerArrivalTone = 3000;
int buzzerMovingTone = 500;
int buzzerClosingDoorTone = 1000;
unsigned long lastClosingDoors = 0;
const int flickeringLed = 9;
int flickeringLedState = HIGH;
bool arrived = true;
int movingIndicatorInterval = 500;
int lastFlicker = 0;
unsigned int debounceDelay = 50;
unsigned int floorChangeTime = 2000;
unsigned long lastFloorChange = 0;
bool buzzed = false;
bool doorsClosing = false;
//We create a generic structure that encapsulates all the necessary details about a certain floor
struct Floor {
const int floorButton;
const int floorLed;
bool floorLedState = LOW;
byte buttonState = LOW;
byte lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
bool buttonStateReading = LOW;
Floor(int buttonPin, int ledPin)
: floorButton(buttonPin), floorLed(ledPin) {}
};
Floor floors[3] = {
Floor(2, 11),
Floor(3, 12),
Floor(4, 13)
};
int currentFloor = 0;
int destination = 0;
int totalFloors = sizeof(floors) / sizeof(floors[0]);
int closingDoorFloor = 0;
void setup() {
Serial.begin(9600);
floors[0].floorLedState = HIGH;
for (int i = 0; i < totalFloors; i++) {
pinMode(floors[i].floorButton, INPUT_PULLUP);
pinMode(floors[i].floorLed, OUTPUT);
digitalWrite(floors[i].floorLed, floors[i].floorLedState);
}
pinMode(flickeringLed, OUTPUT);
digitalWrite(flickeringLed, flickeringLedState);
}
void loop() {
readButtonValues();
checkButtonPress();
if (currentFloor != destination) {
arrived = false;
//The buzzed variable is false as long as the elevator is moving towards its destination
buzzed = false;
//We wait for the "doors to close" which translates to the floorChangeTime variable to pass
if (millis() - lastClosingDoors < floorChangeTime) {
closingDoor();
}
else {
doorsClosing = false;
digitalWrite(floors[closingDoorFloor].floorLed, LOW);
if (destination < currentFloor) {
movingDownwards();
}
if (destination > currentFloor) {
movingUpwards();
}
}
}
else {
arrived = true;
}
if (!arrived) {
flickerOperationalLed();
//The doors are closed therefore we make a transition between the sound for closing doors and the sound for the moving elevator
if (!doorsClosing) {
tone(buzzerPin, buzzerMovingTone, 1000);
}
}
else {
flickeringLedState = HIGH;
digitalWrite(flickeringLed, flickeringLedState);
if (!buzzed) {
tone(buzzerPin, buzzerArrivalTone, 1000);
buzzed = true;
}
}
}
void readButtonValues() {
//We read the states of all the buttons
for (int i = 0; i < totalFloors; i++) {
floors[i].buttonStateReading = digitalRead(floors[i].floorButton);
}
}
void checkButtonPress() {
for (int i = 0; i < totalFloors; i++) {
if (floors[i].buttonStateReading != floors[i].lastButtonState) {
floors[i].lastDebounceTime = millis();
}
for (int i = 0; i < totalFloors; i++) {
if ((millis() - floors[i].lastDebounceTime) > debounceDelay) {
if (floors[i].buttonStateReading != floors[i].buttonState) {
floors[i].buttonState = floors[i].buttonStateReading;
//The button is pressed
if (floors[i].buttonState == LOW && arrived == true) {
destination = i;
lastClosingDoors = millis();
}
}
}
floors[i].lastButtonState = floors[i].buttonStateReading;
}
}
}
void movingDownwards() {
/*
The LED for the current floor has been in the HIGH state for long enough ( for the floorChangeTime period ) and we change its state
to LOW, wishing it to stay in this state for the same time period
*/
if (floors[currentFloor].floorLedState == HIGH && (millis() - lastFloorChange > floorChangeTime)) {
floors[currentFloor].floorLedState = LOW;
digitalWrite(floors[currentFloor].floorLed, floors[currentFloor].floorLedState);
lastFloorChange = millis();
}
/*
The LED for the previous floor has been in the LOW state for the desired time period therefore we continue to the next floor
and we turn the corresponding LED on
*/
if (floors[currentFloor].floorLedState == LOW && (millis() - lastFloorChange > floorChangeTime)) {
currentFloor--;
floors[currentFloor].floorLedState = HIGH;
digitalWrite(floors[currentFloor].floorLed, floors[currentFloor].floorLedState);
lastFloorChange = millis();
}
}
void movingUpwards() {
//We repeat the process in the oposite direction
if (floors[currentFloor].floorLedState == HIGH && (millis() - lastFloorChange > floorChangeTime)) {
floors[currentFloor].floorLedState = LOW;
digitalWrite(floors[currentFloor].floorLed, floors[currentFloor].floorLedState);
lastFloorChange = millis();
}
if (floors[currentFloor].floorLedState == LOW && (millis() - lastFloorChange > floorChangeTime)) {
currentFloor++;
floors[currentFloor].floorLedState = HIGH;
digitalWrite(floors[currentFloor].floorLed, floors[currentFloor].floorLedState);
lastFloorChange = millis();
}
}
void flickerOperationalLed() {
//As long as the elevator moves we flicker the LED with an interval of "movingIndicatorInterval" staying ON and OFF
if (flickeringLedState == HIGH && (millis() - lastFlicker) > movingIndicatorInterval) {
flickeringLedState = LOW;
digitalWrite(flickeringLed, flickeringLedState);
lastFlicker = millis();
}
if (flickeringLedState == LOW && (millis() - lastFlicker) > movingIndicatorInterval) {
flickeringLedState = HIGH;
digitalWrite(flickeringLed, flickeringLedState);
lastFlicker = millis();
}
}
void closingDoor() {
tone(buzzerPin, buzzerClosingDoorTone, 1000);
doorsClosing = true;
closingDoorFloor = currentFloor;
//As long as the "doors close" the led for that floor stays on
digitalWrite(floors[closingDoorFloor].floorLed, HIGH);
}