![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Codigo que funciona
Dependencies: DebouncedIn Door HCSR04 mbed
Fork of Ascensor by
main.cpp@1:27e51c864014, 2018-06-07 (annotated)
- Committer:
- laura015
- Date:
- Thu Jun 07 21:25:25 2018 +0000
- Revision:
- 1:27e51c864014
- Parent:
- 0:4f72e674f36d
C?digo manual ascensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aftorob | 0:4f72e674f36d | 1 | #include "mbed.h" |
aftorob | 0:4f72e674f36d | 2 | #include "DebouncedIn.h" |
aftorob | 0:4f72e674f36d | 3 | #include "hcsr04.h" |
aftorob | 0:4f72e674f36d | 4 | |
aftorob | 0:4f72e674f36d | 5 | DigitalIn B_stop(PTC8); //Boton de paro de emergencia |
aftorob | 0:4f72e674f36d | 6 | DigitalIn B_close(PTC9); //Boton “Cerrar” de la cabina |
aftorob | 0:4f72e674f36d | 7 | DigitalIn B_open(PTA13); //Boton “Abrir” de la cabina |
aftorob | 0:4f72e674f36d | 8 | |
aftorob | 0:4f72e674f36d | 9 | DigitalIn B_c1(PTB1); //Boton "1" de la cabina |
aftorob | 0:4f72e674f36d | 10 | DigitalIn B_c2(PTB0); //Boton "2" de la cabina |
aftorob | 0:4f72e674f36d | 11 | DigitalIn B_c3(PTA5); //Boton "3" de la cabina |
aftorob | 0:4f72e674f36d | 12 | |
aftorob | 0:4f72e674f36d | 13 | DigitalIn B_p1(PTB2); //Boton piso 1 |
aftorob | 0:4f72e674f36d | 14 | DigitalIn B_p3(PTC1); //Boton piso 3 |
aftorob | 0:4f72e674f36d | 15 | DigitalIn B_p2u(PTB3); //Boton piso 2 arriba |
aftorob | 0:4f72e674f36d | 16 | DigitalIn B_p2d(PTC2); //Boton piso 2 abajo |
aftorob | 0:4f72e674f36d | 17 | |
aftorob | 0:4f72e674f36d | 18 | PwmOut S_1(PTD2); //Servomotor piso 1 |
aftorob | 0:4f72e674f36d | 19 | PwmOut S_2(PTD0); //Servomotor piso 2 |
aftorob | 0:4f72e674f36d | 20 | PwmOut S_3(PTD5); //Servomotor piso 3 |
aftorob | 0:4f72e674f36d | 21 | |
aftorob | 0:4f72e674f36d | 22 | PwmOut Motor_en(PTA12); //Motor enable |
aftorob | 0:4f72e674f36d | 23 | DigitalOut Motor_up(PTD4); //Motor arriba |
aftorob | 0:4f72e674f36d | 24 | DigitalOut Motor_down(PTA4); //Motor abajo |
aftorob | 0:4f72e674f36d | 25 | |
aftorob | 0:4f72e674f36d | 26 | DigitalOut Trigger(PTD3); //Trigger Ultrasonido |
aftorob | 0:4f72e674f36d | 27 | DigitalIn Echo(PTD1); //Echo ultrasonido |
aftorob | 0:4f72e674f36d | 28 | DigitalOut myled(LED1); //monitor trigger |
aftorob | 0:4f72e674f36d | 29 | DigitalOut myled2(LED2); //monitor echo |
aftorob | 0:4f72e674f36d | 30 | |
aftorob | 0:4f72e674f36d | 31 | Serial pc(PTA2, PTA1); |
aftorob | 0:4f72e674f36d | 32 | |
aftorob | 0:4f72e674f36d | 33 | int distance = 0; |
aftorob | 0:4f72e674f36d | 34 | int correction = 0; |
aftorob | 0:4f72e674f36d | 35 | Timer sonar; |
aftorob | 0:4f72e674f36d | 36 | |
aftorob | 0:4f72e674f36d | 37 | int sensordist(void); |
aftorob | 0:4f72e674f36d | 38 | void comp(int dmeasured); |
aftorob | 0:4f72e674f36d | 39 | |
laura015 | 1:27e51c864014 | 40 | int Piso_trans; |
laura015 | 1:27e51c864014 | 41 | int Piso_act; |
laura015 | 1:27e51c864014 | 42 | |
aftorob | 0:4f72e674f36d | 43 | int main(){ |
laura015 | 1:27e51c864014 | 44 | sonar.reset(); // Se resetea el temporizador |
laura015 | 1:27e51c864014 | 45 | sonar.start(); // Se inicia el temporizador |
aftorob | 0:4f72e674f36d | 46 | while(Echo == 2){}; |
aftorob | 0:4f72e674f36d | 47 | myled=0; |
aftorob | 0:4f72e674f36d | 48 | sonar.stop(); |
laura015 | 1:27e51c864014 | 49 | correction = sonar.read_us(); // Se halla el factor de corrección |
aftorob | 0:4f72e674f36d | 50 | |
aftorob | 0:4f72e674f36d | 51 | while(true){ |
aftorob | 0:4f72e674f36d | 52 | |
laura015 | 1:27e51c864014 | 53 | int d = sensordist(); // Lectura del sensor de ultrasonido |
laura015 | 1:27e51c864014 | 54 | comp(d); // Entrego el valor del piso actual |
aftorob | 0:4f72e674f36d | 55 | wait_ms(50); |
aftorob | 0:4f72e674f36d | 56 | |
aftorob | 0:4f72e674f36d | 57 | while((B_p2u == 0)){ |
aftorob | 0:4f72e674f36d | 58 | pc.printf("u"); |
aftorob | 0:4f72e674f36d | 59 | Motor_up = 1; |
aftorob | 0:4f72e674f36d | 60 | Motor_down = 0; |
aftorob | 0:4f72e674f36d | 61 | Motor_en.period_ms(50); |
laura015 | 1:27e51c864014 | 62 | Motor_en.pulsewidth_us(3500); |
aftorob | 0:4f72e674f36d | 63 | } |
aftorob | 0:4f72e674f36d | 64 | while((B_p2d == 0)){ |
aftorob | 0:4f72e674f36d | 65 | pc.printf("d"); |
aftorob | 0:4f72e674f36d | 66 | Motor_up = 0; |
aftorob | 0:4f72e674f36d | 67 | Motor_down = 1; |
aftorob | 0:4f72e674f36d | 68 | Motor_en.period_ms(50); |
laura015 | 1:27e51c864014 | 69 | Motor_en.pulsewidth_us(150); |
aftorob | 0:4f72e674f36d | 70 | } |
aftorob | 0:4f72e674f36d | 71 | if ((B_p2u == 1) && (B_p2d == 1)){ |
aftorob | 0:4f72e674f36d | 72 | pc.printf("p"); |
aftorob | 0:4f72e674f36d | 73 | Motor_up = 0; |
aftorob | 0:4f72e674f36d | 74 | Motor_down = 0; |
laura015 | 1:27e51c864014 | 75 | } |
aftorob | 0:4f72e674f36d | 76 | |
laura015 | 1:27e51c864014 | 77 | if (Piso_act==1){ |
laura015 | 1:27e51c864014 | 78 | if(B_open == 0){ // Si presiono el boton de apertura |
laura015 | 1:27e51c864014 | 79 | S_1.period_ms(30) ; |
laura015 | 1:27e51c864014 | 80 | S_1.pulsewidth_us(2300); |
laura015 | 1:27e51c864014 | 81 | } |
laura015 | 1:27e51c864014 | 82 | if(B_close == 0){ // Si presiono el boton de apertura |
laura015 | 1:27e51c864014 | 83 | S_1.period_ms(30) ; |
laura015 | 1:27e51c864014 | 84 | S_1.pulsewidth_us(600); |
aftorob | 0:4f72e674f36d | 85 | } |
laura015 | 1:27e51c864014 | 86 | } |
laura015 | 1:27e51c864014 | 87 | if (Piso_act==3){ |
laura015 | 1:27e51c864014 | 88 | if(B_open == 0){ // Si presiono el boton de apertura |
laura015 | 1:27e51c864014 | 89 | S_3.period_ms(30) ; |
laura015 | 1:27e51c864014 | 90 | S_3.pulsewidth_us(2300); |
laura015 | 1:27e51c864014 | 91 | } |
laura015 | 1:27e51c864014 | 92 | if(B_close == 0){ // Si presiono el boton de apertura |
laura015 | 1:27e51c864014 | 93 | S_3.period_ms(30) ; |
laura015 | 1:27e51c864014 | 94 | S_3.pulsewidth_us(600); |
laura015 | 1:27e51c864014 | 95 | } |
laura015 | 1:27e51c864014 | 96 | } |
laura015 | 1:27e51c864014 | 97 | |
laura015 | 1:27e51c864014 | 98 | if(B_p1 == 0){ |
laura015 | 1:27e51c864014 | 99 | while(Piso_act!=1){ |
aftorob | 0:4f72e674f36d | 100 | |
aftorob | 0:4f72e674f36d | 101 | } |
aftorob | 0:4f72e674f36d | 102 | } |
laura015 | 1:27e51c864014 | 103 | |
laura015 | 1:27e51c864014 | 104 | |
laura015 | 1:27e51c864014 | 105 | wait_ms(300); |
laura015 | 1:27e51c864014 | 106 | |
laura015 | 1:27e51c864014 | 107 | } |
aftorob | 0:4f72e674f36d | 108 | } |
aftorob | 0:4f72e674f36d | 109 | |
aftorob | 0:4f72e674f36d | 110 | |
aftorob | 0:4f72e674f36d | 111 | |
aftorob | 0:4f72e674f36d | 112 | int sensordist(void){ |
aftorob | 0:4f72e674f36d | 113 | Trigger = 1; |
aftorob | 0:4f72e674f36d | 114 | myled = 1; |
aftorob | 0:4f72e674f36d | 115 | myled2 = 0; |
aftorob | 0:4f72e674f36d | 116 | sonar.reset(); |
aftorob | 0:4f72e674f36d | 117 | wait_us(10.0); |
aftorob | 0:4f72e674f36d | 118 | Trigger = 0; |
aftorob | 0:4f72e674f36d | 119 | myled = 0; |
aftorob | 0:4f72e674f36d | 120 | while (Echo==0) {}; |
aftorob | 0:4f72e674f36d | 121 | myled2=Echo; |
aftorob | 0:4f72e674f36d | 122 | sonar.start(); |
aftorob | 0:4f72e674f36d | 123 | while (Echo==1) {}; |
aftorob | 0:4f72e674f36d | 124 | sonar.stop(); |
aftorob | 0:4f72e674f36d | 125 | distance = (sonar.read_us()-correction)/58.0; |
aftorob | 0:4f72e674f36d | 126 | myled2 = 0; |
aftorob | 0:4f72e674f36d | 127 | return distance; |
aftorob | 0:4f72e674f36d | 128 | } |
aftorob | 0:4f72e674f36d | 129 | |
laura015 | 1:27e51c864014 | 130 | void comp(int dmeasured){ // Comparación de distancia para determinar piso actual |
laura015 | 1:27e51c864014 | 131 | if((dmeasured <= 20)){ |
laura015 | 1:27e51c864014 | 132 | pc.printf("3"); |
laura015 | 1:27e51c864014 | 133 | Piso_trans=3; |
laura015 | 1:27e51c864014 | 134 | if (dmeasured <= 6){ |
laura015 | 1:27e51c864014 | 135 | Piso_act=3; |
laura015 | 1:27e51c864014 | 136 | } |
aftorob | 0:4f72e674f36d | 137 | } |
laura015 | 1:27e51c864014 | 138 | /*else if ((dmeasured > 5 && dmeasured <= 10)){ |
aftorob | 0:4f72e674f36d | 139 | pc.printf("8"); |
aftorob | 0:4f72e674f36d | 140 | } |
aftorob | 0:4f72e674f36d | 141 | else if ((dmeasured > 10 && dmeasured <= 15)){ |
aftorob | 0:4f72e674f36d | 142 | pc.printf("7"); |
aftorob | 0:4f72e674f36d | 143 | } |
aftorob | 0:4f72e674f36d | 144 | else if ((dmeasured > 15 && dmeasured <= 20)){ |
aftorob | 0:4f72e674f36d | 145 | pc.printf("6"); |
aftorob | 0:4f72e674f36d | 146 | } |
aftorob | 0:4f72e674f36d | 147 | else if ((dmeasured > 20 && dmeasured <= 25)){ |
aftorob | 0:4f72e674f36d | 148 | pc.printf("5"); |
laura015 | 1:27e51c864014 | 149 | }*/ |
laura015 | 1:27e51c864014 | 150 | else if ((dmeasured > 20 && dmeasured <= 35)){ |
laura015 | 1:27e51c864014 | 151 | pc.printf("2"); |
laura015 | 1:27e51c864014 | 152 | Piso_trans=2; |
laura015 | 1:27e51c864014 | 153 | if ((dmeasured > 25 && dmeasured <= 27)){ |
laura015 | 1:27e51c864014 | 154 | Piso_act=2; |
laura015 | 1:27e51c864014 | 155 | } |
aftorob | 0:4f72e674f36d | 156 | } |
laura015 | 1:27e51c864014 | 157 | /*else if ((dmeasured > 30 && dmeasured <= 35)){ |
aftorob | 0:4f72e674f36d | 158 | pc.printf("3"); |
aftorob | 0:4f72e674f36d | 159 | } |
aftorob | 0:4f72e674f36d | 160 | else if ((dmeasured > 35 && dmeasured <= 40)){ |
aftorob | 0:4f72e674f36d | 161 | pc.printf("2"); |
laura015 | 1:27e51c864014 | 162 | }*/ |
laura015 | 1:27e51c864014 | 163 | else if ((dmeasured > 35){ |
aftorob | 0:4f72e674f36d | 164 | pc.printf("1"); |
laura015 | 1:27e51c864014 | 165 | Piso_trans=1; |
laura015 | 1:27e51c864014 | 166 | if ((dmeasured > 45)){ |
laura015 | 1:27e51c864014 | 167 | Piso_act=1; |
laura015 | 1:27e51c864014 | 168 | } |
laura015 | 1:27e51c864014 | 169 | } |
aftorob | 0:4f72e674f36d | 170 | else{ |
aftorob | 0:4f72e674f36d | 171 | pc.printf("0"); |
aftorob | 0:4f72e674f36d | 172 | } |
aftorob | 0:4f72e674f36d | 173 | } |
aftorob | 0:4f72e674f36d | 174 | |
aftorob | 0:4f72e674f36d | 175 |