Mohammed Alshiekh
/
elevator_with_queue
course work
elevator.cpp@2:07f2fe858dbd, 2015-10-08 (annotated)
- Committer:
- sahabi
- Date:
- Thu Oct 08 18:25:07 2015 +0000
- Revision:
- 2:07f2fe858dbd
- Parent:
- 1:28120714ad80
- Child:
- 3:19106c977df1
working, not voltage reading yet.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahabi | 0:69be3faae0a0 | 1 | #include "mbed.h" |
sahabi | 0:69be3faae0a0 | 2 | |
sahabi | 0:69be3faae0a0 | 3 | PwmOut servo1(p21); |
sahabi | 0:69be3faae0a0 | 4 | PwmOut servo2(p22); |
sahabi | 0:69be3faae0a0 | 5 | PwmOut dc_motor(p23); |
sahabi | 0:69be3faae0a0 | 6 | DigitalInOut in1(p5); |
sahabi | 0:69be3faae0a0 | 7 | DigitalInOut in2(p6); |
sahabi | 0:69be3faae0a0 | 8 | InterruptIn firstfloor(p7); |
sahabi | 0:69be3faae0a0 | 9 | InterruptIn secondfloor(p8); |
sahabi | 0:69be3faae0a0 | 10 | InterruptIn thirdfloor(p9); |
sahabi | 0:69be3faae0a0 | 11 | InterruptIn fourthfloor(p10); |
sahabi | 0:69be3faae0a0 | 12 | InterruptIn fifthfloor(p11); |
sahabi | 1:28120714ad80 | 13 | InterruptIn sensor(p12); |
sahabi | 1:28120714ad80 | 14 | DigitalOut led4(LED4); |
sahabi | 1:28120714ad80 | 15 | DigitalOut led3(LED3); |
sahabi | 1:28120714ad80 | 16 | DigitalOut led2(LED2); |
sahabi | 1:28120714ad80 | 17 | DigitalOut led1(LED1); |
sahabi | 0:69be3faae0a0 | 18 | Timeout timeout; |
sahabi | 2:07f2fe858dbd | 19 | int first = 0; |
sahabi | 0:69be3faae0a0 | 20 | Timer time1; |
sahabi | 2:07f2fe858dbd | 21 | int once = 0; |
sahabi | 0:69be3faae0a0 | 22 | float stationary = 0.075; |
sahabi | 0:69be3faae0a0 | 23 | int closed; |
sahabi | 0:69be3faae0a0 | 24 | float ccw = 0.085; // counter clock-wise |
sahabi | 0:69be3faae0a0 | 25 | float cw = 0.065; // clock-wise |
sahabi | 1:28120714ad80 | 26 | int reached = 0; |
sahabi | 0:69be3faae0a0 | 27 | int location = 1; |
sahabi | 2:07f2fe858dbd | 28 | float frequency; |
sahabi | 0:69be3faae0a0 | 29 | int requested_floor; |
sahabi | 0:69be3faae0a0 | 30 | int busy = 0; |
sahabi | 2:07f2fe858dbd | 31 | int currentfloor; |
sahabi | 1:28120714ad80 | 32 | |
sahabi | 2:07f2fe858dbd | 33 | void serve(){ |
sahabi | 2:07f2fe858dbd | 34 | if(busy == 1){ |
sahabi | 2:07f2fe858dbd | 35 | |
sahabi | 2:07f2fe858dbd | 36 | if (first == 0){ |
sahabi | 2:07f2fe858dbd | 37 | time1.reset(); |
sahabi | 2:07f2fe858dbd | 38 | time1.start(); |
sahabi | 2:07f2fe858dbd | 39 | first = 1; |
sahabi | 2:07f2fe858dbd | 40 | } |
sahabi | 2:07f2fe858dbd | 41 | |
sahabi | 2:07f2fe858dbd | 42 | else if (first == 1){ |
sahabi | 2:07f2fe858dbd | 43 | time1.stop(); |
sahabi | 2:07f2fe858dbd | 44 | first = 0; |
sahabi | 2:07f2fe858dbd | 45 | once++; |
sahabi | 2:07f2fe858dbd | 46 | frequency = 1./time1.read(); |
sahabi | 2:07f2fe858dbd | 47 | |
sahabi | 2:07f2fe858dbd | 48 | if (once ==2){ |
sahabi | 2:07f2fe858dbd | 49 | once = 0; |
sahabi | 2:07f2fe858dbd | 50 | if (frequency >= 900 && frequency <= 1100){currentfloor = 5;} |
sahabi | 2:07f2fe858dbd | 51 | else if (frequency >= 700 && frequency <= 800){currentfloor = 4;led4 = !led4;} |
sahabi | 2:07f2fe858dbd | 52 | else if (frequency >= 450 && frequency <= 550){currentfloor = 3;led3 = !led3;} |
sahabi | 2:07f2fe858dbd | 53 | else if (frequency >= 220 && frequency <= 290){currentfloor = 2;led2 = !led2;} |
sahabi | 2:07f2fe858dbd | 54 | else if (frequency >= 70 && frequency <= 150){currentfloor = 1;led1 = !led1;} |
sahabi | 1:28120714ad80 | 55 | |
sahabi | 2:07f2fe858dbd | 56 | if (currentfloor == requested_floor){ |
sahabi | 2:07f2fe858dbd | 57 | led1 = 0; |
sahabi | 2:07f2fe858dbd | 58 | led2 = 0; |
sahabi | 2:07f2fe858dbd | 59 | led3 = 0; |
sahabi | 2:07f2fe858dbd | 60 | led4 = 0; |
sahabi | 2:07f2fe858dbd | 61 | in1 = 0; |
sahabi | 2:07f2fe858dbd | 62 | in2 = 0; |
sahabi | 2:07f2fe858dbd | 63 | servo1.write(cw); |
sahabi | 2:07f2fe858dbd | 64 | servo2.write(ccw); |
sahabi | 2:07f2fe858dbd | 65 | wait(0.33); |
sahabi | 2:07f2fe858dbd | 66 | servo1.write(stationary); |
sahabi | 2:07f2fe858dbd | 67 | servo2.write(stationary); |
sahabi | 2:07f2fe858dbd | 68 | busy = 0; |
sahabi | 2:07f2fe858dbd | 69 | location = requested_floor; |
sahabi | 2:07f2fe858dbd | 70 | } |
sahabi | 2:07f2fe858dbd | 71 | |
sahabi | 0:69be3faae0a0 | 72 | } |
sahabi | 2:07f2fe858dbd | 73 | |
sahabi | 2:07f2fe858dbd | 74 | } |
sahabi | 1:28120714ad80 | 75 | } |
sahabi | 0:69be3faae0a0 | 76 | } |
sahabi | 1:28120714ad80 | 77 | |
sahabi | 0:69be3faae0a0 | 78 | void moveUp(){ |
sahabi | 1:28120714ad80 | 79 | dc_motor.write(0.6f); |
sahabi | 0:69be3faae0a0 | 80 | in1 = 1; |
sahabi | 0:69be3faae0a0 | 81 | in2 = 0; |
sahabi | 0:69be3faae0a0 | 82 | } |
sahabi | 0:69be3faae0a0 | 83 | |
sahabi | 0:69be3faae0a0 | 84 | void moveDown(){ |
sahabi | 1:28120714ad80 | 85 | dc_motor.write(0.6f); |
sahabi | 0:69be3faae0a0 | 86 | in1 = 0; |
sahabi | 0:69be3faae0a0 | 87 | in2 = 1; |
sahabi | 0:69be3faae0a0 | 88 | } |
sahabi | 0:69be3faae0a0 | 89 | |
sahabi | 0:69be3faae0a0 | 90 | void stop(){ |
sahabi | 0:69be3faae0a0 | 91 | in1 = 0; |
sahabi | 0:69be3faae0a0 | 92 | in2 = 0; |
sahabi | 0:69be3faae0a0 | 93 | } |
sahabi | 0:69be3faae0a0 | 94 | |
sahabi | 0:69be3faae0a0 | 95 | void closeDoor(){ |
sahabi | 0:69be3faae0a0 | 96 | servo1.write(ccw); |
sahabi | 0:69be3faae0a0 | 97 | servo2.write(cw); |
sahabi | 1:28120714ad80 | 98 | wait(0.33); |
sahabi | 0:69be3faae0a0 | 99 | servo1.write(stationary); |
sahabi | 0:69be3faae0a0 | 100 | servo2.write(stationary); |
sahabi | 0:69be3faae0a0 | 101 | } |
sahabi | 0:69be3faae0a0 | 102 | |
sahabi | 0:69be3faae0a0 | 103 | void openDoor(){ |
sahabi | 0:69be3faae0a0 | 104 | servo1.write(cw); |
sahabi | 0:69be3faae0a0 | 105 | servo2.write(ccw); |
sahabi | 1:28120714ad80 | 106 | wait(0.33); |
sahabi | 0:69be3faae0a0 | 107 | servo1.write(stationary); |
sahabi | 0:69be3faae0a0 | 108 | servo2.write(stationary); |
sahabi | 0:69be3faae0a0 | 109 | } |
sahabi | 0:69be3faae0a0 | 110 | |
sahabi | 0:69be3faae0a0 | 111 | void call(int next) { |
sahabi | 1:28120714ad80 | 112 | if (busy == 0){ |
sahabi | 1:28120714ad80 | 113 | requested_floor = next; |
sahabi | 1:28120714ad80 | 114 | if (location > next){ |
sahabi | 1:28120714ad80 | 115 | closeDoor(); |
sahabi | 1:28120714ad80 | 116 | moveDown(); |
sahabi | 1:28120714ad80 | 117 | busy = 1; |
sahabi | 1:28120714ad80 | 118 | } |
sahabi | 1:28120714ad80 | 119 | |
sahabi | 1:28120714ad80 | 120 | else if (location < next){ |
sahabi | 1:28120714ad80 | 121 | closeDoor(); |
sahabi | 1:28120714ad80 | 122 | moveUp(); |
sahabi | 1:28120714ad80 | 123 | busy = 1; |
sahabi | 1:28120714ad80 | 124 | } |
sahabi | 0:69be3faae0a0 | 125 | } |
sahabi | 1:28120714ad80 | 126 | } |
sahabi | 0:69be3faae0a0 | 127 | |
sahabi | 0:69be3faae0a0 | 128 | void call1(void) { call(1); } |
sahabi | 0:69be3faae0a0 | 129 | void call2(void) { call(2); } |
sahabi | 0:69be3faae0a0 | 130 | void call3(void) { call(3); } |
sahabi | 0:69be3faae0a0 | 131 | void call4(void) { call(4); } |
sahabi | 0:69be3faae0a0 | 132 | void call5(void) { call(5); } |
sahabi | 0:69be3faae0a0 | 133 | |
sahabi | 0:69be3faae0a0 | 134 | |
sahabi | 0:69be3faae0a0 | 135 | int main() { |
sahabi | 0:69be3faae0a0 | 136 | servo1.period(0.02f); |
sahabi | 0:69be3faae0a0 | 137 | servo2.period(0.02f); |
sahabi | 0:69be3faae0a0 | 138 | dc_motor.period(0.02f); |
sahabi | 0:69be3faae0a0 | 139 | servo1.write(stationary); |
sahabi | 0:69be3faae0a0 | 140 | servo2.write(stationary); |
sahabi | 0:69be3faae0a0 | 141 | dc_motor.write(0); |
sahabi | 0:69be3faae0a0 | 142 | in1.output(); |
sahabi | 0:69be3faae0a0 | 143 | in1 = 0; |
sahabi | 0:69be3faae0a0 | 144 | in2.output(); |
sahabi | 0:69be3faae0a0 | 145 | in2 = 0; |
sahabi | 0:69be3faae0a0 | 146 | firstfloor.rise(&call1); |
sahabi | 0:69be3faae0a0 | 147 | secondfloor.rise(&call2); |
sahabi | 0:69be3faae0a0 | 148 | thirdfloor.rise(&call3); |
sahabi | 0:69be3faae0a0 | 149 | fourthfloor.rise(&call4); |
sahabi | 0:69be3faae0a0 | 150 | fifthfloor.rise(&call5); |
sahabi | 2:07f2fe858dbd | 151 | |
sahabi | 2:07f2fe858dbd | 152 | sensor.rise(&serve); |
sahabi | 0:69be3faae0a0 | 153 | |
sahabi | 0:69be3faae0a0 | 154 | while(1){ |
sahabi | 0:69be3faae0a0 | 155 | } |
sahabi | 0:69be3faae0a0 | 156 | } |
sahabi | 0:69be3faae0a0 | 157 | |
sahabi | 0:69be3faae0a0 | 158 |