Arduino-Elvevator - Main - Ino at Master Moh-Slimani - Arduino-Elvevator GitHub
Arduino-Elvevator - Main - Ino at Master Moh-Slimani - Arduino-Elvevator GitHub
master
arduino-elvevator / main.ino
1 contributor
1 #include "PinChangeInterrupt.h"
2
3 volatile int level = 0;
4 volatile int levelsToGo[3] = {0, 0, 0};
5 volatile int doorOpen = false;
6 volatile int doorClosing = false;
7 volatile int commoingFrom = 0;
8 volatile int doorClosed = true;
9 volatile int moving = false;
10
11 int doorDelay = 2000;
12
13 void setup()
14 {
15 displayDigitInit();
16
17 limitSwitchersInit();
18
19 upDownInit();
20
21 buttonsInit();
22
23 closeOpenInit();
24
25 Serial.begin(9600);
26 Serial.println("starting ....");
27
28 levelInfo();
29 }
30
31 void loop()
32 {
33 if (doorOpen && !doorClosing)
34 {
35 delay(doorDelay);
36 closeDoor();
37 delay(50);
38 }
39 }
40
41 void levelInfo()
42 {
43 Serial.print("on level : ");
44 Serial.println(level + 1);
45
46 displayDigit();
47
48 Serial.print("levels to go : ");
49 Serial.print(levelsToGo[0]);
50 Serial.print(" ");
51 Serial.print(levelsToGo[1]);
52 Serial.print(" ");
53 Serial.println(levelsToGo[2]);
54 }
55
56 /** 7 segments **/
57
58 uint8_t a = 22;
59 uint8_t b = 23;
60 uint8_t c = 24;
61 uint8_t d = 25;
62 uint8_t e = 26;
63 uint8_t f = 27;
64 uint8_t g = 28;
65
66 void displayDigitInit()
67 {
68 pinMode(a, OUTPUT); //A
69 pinMode(b, OUTPUT); //B
70 pinMode(c, OUTPUT); //C
71 pinMode(d, OUTPUT); //D
72 pinMode(e, OUTPUT); //E
73 pinMode(f, OUTPUT); //F
74 pinMode(g, OUTPUT); //G
75 }
76
77 void displayDigit()
78 {
79 int digit = level + 1;
80
81 displayDigitOff();
82 //Conditions for displaying segment a
83 if (digit != 1 && digit != 4)
84 digitalWrite(a, HIGH);
85
86 //Conditions for displaying segment b
87 if (digit != 5 && digit != 6)
88 digitalWrite(b, HIGH);
89
90 //Conditions for displaying segment c
91 if (digit != 2)
92 digitalWrite(c, HIGH);
93
94 //Conditions for displaying segment d
95 if (digit != 1 && digit != 4 && digit != 7)
96 digitalWrite(d, HIGH);
97
98 //Conditions for displaying segment e
99 if (digit == 2 || digit == 6 || digit == 8 || digit == 0)
100 digitalWrite(e, HIGH);
101
102 //Conditions for displaying segment f
103 if (digit != 1 && digit != 2 && digit != 3 && digit != 7)
104 digitalWrite(f, HIGH);
105 if (digit != 0 && digit != 1 && digit != 7)
106 digitalWrite(g, HIGH);
107 }
108
109 void displayDigitOff()
110 {
111 digitalWrite(a, LOW);
112 digitalWrite(b, LOW);
113 digitalWrite(c, LOW);
114 digitalWrite(d, LOW);
115 digitalWrite(e, LOW);
116 digitalWrite(f, LOW);
117 digitalWrite(g, LOW);
118 }
119
120 /** 7 segments **/
121
122 /** limit switches pins **/
123
124 uint8_t LV1Swtich = 2;
125 uint8_t LV2Swtich = 3;
126 uint8_t LV3Swtich = 18;
127
128 uint8_t DoorSwtich = 19;
129
130 void limitSwitchersInit()
131 {
132 pinMode(LV1Swtich, INPUT_PULLUP);
133 pinMode(LV2Swtich, INPUT_PULLUP);
134 pinMode(LV3Swtich, INPUT_PULLUP);
135
136 pinMode(DoorSwtich, INPUT_PULLUP);
137
138 attachInterrupt(digitalPinToInterrupt(LV1Swtich), floor1reach, RISING);
139 attachInterrupt(digitalPinToInterrupt(LV2Swtich), floor2reach, RISING);
140 attachInterrupt(digitalPinToInterrupt(LV3Swtich), floor3reach, RISING);
141
142 attachInterrupt(digitalPinToInterrupt(DoorSwtich), stopDoor, RISING);
143 }
144
145 void floorReach(int lv)
146 {
147 level = lv;
148
149 if (levelsToGo[lv])
150 {
151 stopMoving();
152 levelsToGo[lv] = 0;
153 }
154
155 levelInfo();
156 }
157
158 void floor1reach()
159 {
160 floorReach(0);
161 }
162
163 void floor2reach()
164 {
165 floorReach(1);
166 }
167
168 void floor3reach()
169 {
170 floorReach(2);
171 }
172
173 /** limit switches pins **/
174
175 /** up and down **/
176
177 volatile uint8_t upPin = 9;
178 volatile uint8_t downPin = 8;
179
180 void upDownInit()
181 {
182 pinMode(upPin, OUTPUT);
183 pinMode(downPin, OUTPUT);
184 }
185
186 void goUp()
187 {
188 Serial.println("going up ....");
189 digitalWrite(downPin, LOW);
190 digitalWrite(upPin, HIGH);
191 moving = true;
192 }
193
194 void goDown()
195 {
196 Serial.println("going down ...");
197 digitalWrite(upPin, LOW);
198 digitalWrite(downPin, HIGH);
199 moving = true;
200 }
201
202 void stopMoving()
203 {
204 Serial.println("stopping ...");
205 digitalWrite(upPin, LOW);
206 digitalWrite(downPin, LOW);
207
208 moving = false;
209
210 openDoor();
211 }
212
213 /** buttons **/
214
215 uint8_t LV1btn = 53;
216 uint8_t LV2btn = 52;
217 uint8_t LV3btn = 51;
218
219 void buttonsInit()
220 {
221 pinMode(LV1btn, INPUT_PULLUP);
222 pinMode(LV2btn, INPUT_PULLUP);
223 pinMode(LV3btn, INPUT_PULLUP);
224
225 attachPCINT(digitalPinToPCINT(LV1btn), LV1btnClick, CHANGE);
226 attachPCINT(digitalPinToPCINT(LV2btn), LV2btnClick, CHANGE);
227 attachPCINT(digitalPinToPCINT(LV3btn), LV3btnClick, CHANGE);
228 }
229
230 void LV1btnClick(void)
231 {
232 if (level == 0 & !moving)
233 openDoor();
234 else
235 {
236
237 levelsToGo[0] = 1;
238 levelInfo();
239 move();
240 }
241 }
242
243 void LV2btnClick(void)
244 {
245 if (level == 1 & !moving)
246 openDoor();
247 else
248 {
249 levelsToGo[1] = 1;
250 levelInfo();
251 move();
252 }
253 }
254
255 void LV3btnClick(void)
256 {
257 if (level == 2 & !moving)
258 openDoor();
259 else
260 {
261
262 levelsToGo[2] = 1;
263 levelInfo();
264 move();
265 }
266 }
267
268 void move()
269 {
270 if (doorClosed)
271 {
272 if (level == 0 && (levelsToGo[1] || levelsToGo[2]))
273 goUp();
274
275 if (level == 2 && (levelsToGo[0] || levelsToGo[1]))
276 goDown();
277
278 if (level == 1 && levelsToGo[0])
279 if (levelsToGo[2] && commoingFrom == 0)
280 goUp();
281 else
282 goDown();
283
284 if (level == 1 && levelsToGo[2])
285 goUp();
286
287 commoingFrom = level;
288 }
289 }
290
291 /** open and close door **/
292
293 volatile uint8_t openPin = 7;
294 volatile uint8_t closePin = 6;
295
296 void closeOpenInit()
297 {
298 pinMode(openPin, OUTPUT);
299 pinMode(closePin, OUTPUT);
300 }
301
302 void openDoor()
303 {
304 doorClosing = false;
305 doorClosed = false;
306
307 Serial.println("opening door ....");
308 digitalWrite(closePin, LOW);
309 digitalWrite(openPin, HIGH);
310 }
311
312 void closeDoor()
313 {
314 doorClosing = true;
315 Serial.println("closing door ...");
316 digitalWrite(openPin, LOW);
317 digitalWrite(closePin, HIGH);
318 }
319
320 void stopDoor()
321 {
322 Serial.println("stopping door ...");
323 digitalWrite(openPin, LOW);
324 digitalWrite(closePin, LOW);
325
326 if (doorClosing)
327 {
328 doorOpen = false;
329 doorClosing = false;
330 doorClosed = true;
331 move();
332 }
333 else
334 {
335 doorOpen = true;
336 }
337 }