-
Notifications
You must be signed in to change notification settings - Fork 1
/
arduino code.ino
281 lines (243 loc) · 6.85 KB
/
arduino code.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
int motorPin1 = 5;
int motorPin2 = 6;
int enA = 7;
int display1 = 4;
int display2 = 12;
int display3 = 13;
//floor markers
boolean level1 = true;
boolean level2 = false;
boolean level3 = false;
// constants won't change. They're used here to set pin numbers:
const int buttonPin1 = 8; // the number of the pushbutton pin
const int buttonPin2 = 9;
const int buttonPin3 = 11;
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState1; // the current reading from the input pin
int buttonState2;
int buttonState3;
int lastButtonState1 = LOW; // the previous reading from the input pin
int lastButtonState2 = LOW;
int lastButtonState3 = LOW;
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime1 = 0; // the last time the output pin was toggled
unsigned long lastDebounceTime2 = 0;
unsigned long lastDebounceTime3 = 0;
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
//IR varibales
int IRpin = 2;
unsigned long lastIRdebounceTime = 0;
unsigned long IRdebounceDelay = 50;
int IRstate = HIGH;
int lastIRstate = HIGH;
void setup() {
// buttons
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
// LED
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
// Motor
pinMode(enA, OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
digitalWrite(enA, HIGH);
// IR sensor
pinMode(IRpin, INPUT);
// 7 segment pins
pinMode(display1, OUTPUT);
pinMode(display2, OUTPUT);
pinMode(display3, OUTPUT);
digitalWrite(display1, HIGH);
digitalWrite(display2, LOW);
digitalWrite(display3, LOW);
}
void clockwise()
{
int i = 0;
while (i != 6){
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, HIGH);
int IRvalue = digitalRead(IRpin);
if (IRvalue != lastIRstate) {
// reset the debouncing timer
lastIRdebounceTime = millis();
}
if ((millis() - lastIRdebounceTime) > IRdebounceDelay){
if (IRvalue != IRstate) {
IRstate = IRvalue;
if (IRvalue == LOW){
i++;
}
}
}
lastIRstate = IRvalue;
}
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
}
void anticlockwise()
{
int i = 0;
while (i != 6){
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, LOW);
int IRvalue = digitalRead(IRpin);
if (IRvalue != lastIRstate) {
// reset the debouncing timer
lastIRdebounceTime = millis();
}
if ((millis() - lastIRdebounceTime) > IRdebounceDelay){
if (IRvalue != IRstate) {
IRstate = IRvalue;
if (IRvalue == LOW){
i++;
}
}
}
lastIRstate = IRvalue;
}
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
}
void norotate()
{
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, LOW);
}
// Functions to show floor number on 7 segment display
void show1()
{
digitalWrite(display1, HIGH);
digitalWrite(display2, LOW);
digitalWrite(display3, LOW);
}
void show2()
{
digitalWrite(display1, LOW);
digitalWrite(display2, HIGH);
digitalWrite(display3, LOW);
}
void show3()
{
digitalWrite(display1, LOW);
digitalWrite(display2, LOW);
digitalWrite(display3, HIGH);
}
void loop() {
// read the state of the switch into a local variable:
int reading1 = digitalRead(buttonPin1);
int reading2 = digitalRead(buttonPin2);
int reading3 = digitalRead(buttonPin3);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading1 != lastButtonState1) {
// reset the debouncing timer
lastDebounceTime1 = millis();
}
if (reading2 != lastButtonState2) {
// reset the debouncing timer
lastDebounceTime2 = millis();
}
if (reading3 != lastButtonState3) {
// reset the debouncing timer
lastDebounceTime3 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading1 != buttonState1) {
buttonState1 = reading1;
// only toggle the LED if the new button state is HIGH
if (buttonState1 == HIGH) {
ledState = !ledState;
if (level1 == true){
norotate();
}
if (level2 == true){
// go from level2 to level1
clockwise();
level2 = false;
level1 = true;
}
if (level3 == true){
// go from level3 to level1
clockwise();
clockwise();
level3 = false;
level1 = true;
}
}
show1();
}
}
if ((millis() - lastDebounceTime2) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading2 != buttonState2) {
buttonState2 = reading2;
// only toggle the LED if the new button state is HIGH
if (buttonState2 == HIGH) {
ledState = !ledState;
if (level1 == true){
// go from level1 to level2
anticlockwise();
level2 = true;
level1 = false;
}
if (level2 == true){
norotate();
}
if (level3 == true){
// go from level3 to level1
clockwise();
level3 = false;
level2 = true;
}
}
show2();
}
}
if ((millis() - lastDebounceTime3) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading3 != buttonState3) {
buttonState3 = reading3;
// only toggle the LED if the new button state is HIGH
if (buttonState3 == HIGH) {
ledState = !ledState;
if (level1 == true){
// go from level1 to level3
anticlockwise();
anticlockwise();
level1 = false;
level3 = true;
}
if (level2 == true){
// go from level2 to level3
anticlockwise();
level3 = true;
level2 = false;
}
if (level3 == true){
norotate();
}
}
show3();
}
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState1 = reading1;
lastButtonState2 = reading2;
lastButtonState3 = reading3;
}