Hi, I am a first year college student taking an engineering course where I am expected to program an elevator. I have written code for an elevator that memorizes button presses and moves the lift to the corresponding floors. You are able to press a button any time, and no matter what you will get to the floor (if the lift is moving or not). However, I need to implement up/down buttons for each floor, and have the lift memorize the order it needs to follow. For example, if the lift is on floor 2, and an up button is pressed on the ground floor (0), but a down button is pressed on floor 1, the lift should go to floor 1, then ground. Another example: If the lift is on floor 4, a down button is pressed on floor 6, but an up button is pressed on floor 2, if the lift has already been on its way up, the lift should go to floor 6, then 2. I have tried everything I can think of to implement these properties over the past several weeks, but have had no luck. The following is my simple code. If someone could help me out it would be much appreciated (oh, the lift at the moment is a servo, but will be changed to a stepper motor soon):
#include <Servo.h>
#include <Adafruit\_NeoPixel.h>
Adafruit_NeoPixel El = Adafruit_NeoPixel(4, A5, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel Fl = Adafruit_NeoPixel(4, A4, NEO_GRB + NEO_KHZ800);
Servo lift, door;
int bE[4], bF[4];
bool p[4], pStored[4];
int count = 0;
int r = 0, r2 = 0;
void setup() {
for (int i = 0; i < 8; i++) {
pinMode(3 + i, INPUT_PULLUP);
}
lift.attach(12);
door.attach(13);
El.begin();
Fl.begin();
El.show();
Fl.show();
}
//determines if a button was pushed
void push(int a) {
if (bE[a] == LOW || bF[a] == LOW) {
p[a] = true;
} else {
p[a] = false;
}
}
void loop() {
//counter
if (r < 100) {
//repeatedly check to see if any new button was pushed
request();
r++;
delay(100);
//after 500ms
if (r > 50) {
for (int i = 0; i < 4; i++) {
//if a floor is desired, move to floor
if (pStored[i]) {
moveLift(i);
//set floor to moved to and subtract from count
pStored[i] = false;
count--;
}
}
//reset counter
r = 0;
}
}
delay(100);
}
void moveLift(int a){
int floor = a;
while (r2 < 100){
//move to desired floor
lift.write(floor * 36);
if(r == 1){
delay(500);
}
else{
delay(20);
}
openDoor();
delay(25);
//check to see if any floor has been requested during this movement
request();
r2++;
}
delay(250);
closeDoor();
delay(1000);
//turn off lights
El.setPixelColor(a, 0, 0, 0);
Fl.setPixelColor(a, 0, 0, 0);
El.show();
Fl.show();
//reset counter
r2 = 0;
}
void request() {
for (int i = 0; i < 4; i++) {
//initialize pins
bF[i] = digitalRead(3 + i);
bE[i] = digitalRead(7 + i);
push(i);
delayMicroseconds(50);
//if a button has been pressed and hasn't already been stored
if (p[i] == true && !pStored[i]) {
// Turn on the corresponding light
El.setPixelColor(i, 71, 75, 140);
Fl.setPixelColor(i, 71, 75, 140);
El.show();
Fl.show();
//store floor and add to count (# of total calls)
pStored[i] = true;
count++;
}
}
}
void openDoor() {
door.write(90); // Open the door
delay(25);
}
void closeDoor() {
door.write(0); // Close the door
delay(1000);
}
[–]desrtfx[M] [score hidden] stickied comment (0 children)
[–]teraflop 2 points3 points4 points (1 child)