![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
.
Dependencies: mbed Servo MFRC522_Updated HC-SR04 ssd1306_library
main.cpp@2:ad8f0cdf7015, 2020-06-10 (annotated)
- Committer:
- 20172573073
- Date:
- Wed Jun 10 15:41:43 2020 +0000
- Revision:
- 2:ad8f0cdf7015
- Parent:
- 1:849d12c9b2be
- Child:
- 3:3eb3185401f9
es
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
20172573073 | 0:366b3e64c83c | 1 | #include "mbed.h" |
20172573073 | 0:366b3e64c83c | 2 | #include "rtos.h" |
20172573073 | 0:366b3e64c83c | 3 | #include "Servo.h" |
20172573073 | 0:366b3e64c83c | 4 | #include "HCSR04.h" |
20172573073 | 0:366b3e64c83c | 5 | #include "MFRC522.h" |
20172573073 | 0:366b3e64c83c | 6 | #include "ssd1306.h" |
20172573073 | 1:849d12c9b2be | 7 | |
20172573073 | 1:849d12c9b2be | 8 | MFRC522 RfChip (D11, D12, D13, PTE0, D10); |
20172573073 | 0:366b3e64c83c | 9 | SSD1306 lcd (D7,D6); |
20172573073 | 0:366b3e64c83c | 10 | BusOut display7Seg(PTC10, PTC7, PTC6, PTC4, PTC0, PTB19, PTB18); |
20172573073 | 0:366b3e64c83c | 11 | DigitalOut display1(PTC11); |
20172573073 | 0:366b3e64c83c | 12 | DigitalOut display2(PTC13); |
20172573073 | 1:849d12c9b2be | 13 | DigitalOut display3(PTC16); |
20172573073 | 0:366b3e64c83c | 14 | Servo myservo(D2); |
20172573073 | 1:849d12c9b2be | 15 | |
20172573073 | 0:366b3e64c83c | 16 | Thread t; |
20172573073 | 0:366b3e64c83c | 17 | Thread t1; |
20172573073 | 0:366b3e64c83c | 18 | Thread t2; |
20172573073 | 0:366b3e64c83c | 19 | Thread t3; |
20172573073 | 1:849d12c9b2be | 20 | Thread t4; |
20172573073 | 1:849d12c9b2be | 21 | |
20172573073 | 0:366b3e64c83c | 22 | Serial pc(USBTX, USBRX); |
20172573073 | 0:366b3e64c83c | 23 | HCSR04 sonar(D3,D4); |
20172573073 | 2:ad8f0cdf7015 | 24 | |
20172573073 | 0:366b3e64c83c | 25 | float menor; |
20172573073 | 0:366b3e64c83c | 26 | float da; |
20172573073 | 0:366b3e64c83c | 27 | float dis = 0.0; |
20172573073 | 2:ad8f0cdf7015 | 28 | static const char tar[]={0xD7,0x4F,0x88,0x19}; |
20172573073 | 2:ad8f0cdf7015 | 29 | static const char llav[]={0x27,0x57,0xC1,0x7B}; |
20172573073 | 2:ad8f0cdf7015 | 30 | int q,a,al,b,w; |
20172573073 | 0:366b3e64c83c | 31 | |
20172573073 | 0:366b3e64c83c | 32 | |
20172573073 | 0:366b3e64c83c | 33 | int anodeComun[10] = { |
20172573073 | 0:366b3e64c83c | 34 | 0b0000001, // 0 |
20172573073 | 0:366b3e64c83c | 35 | 0b1001111, // 1 |
20172573073 | 0:366b3e64c83c | 36 | 0b0010010, // 2 |
20172573073 | 0:366b3e64c83c | 37 | 0b0000110, // 3 |
20172573073 | 0:366b3e64c83c | 38 | 0b1001100, // 4 |
20172573073 | 0:366b3e64c83c | 39 | 0b0100100, // 5 |
20172573073 | 0:366b3e64c83c | 40 | 0b0100000, // 6 |
20172573073 | 0:366b3e64c83c | 41 | 0b0001111, // 7 |
20172573073 | 0:366b3e64c83c | 42 | 0b0000000, // 8 |
20172573073 | 0:366b3e64c83c | 43 | 0b0001100, // 9 |
20172573073 | 0:366b3e64c83c | 44 | }; |
20172573073 | 0:366b3e64c83c | 45 | |
20172573073 | 0:366b3e64c83c | 46 | void servo() |
20172573073 | 0:366b3e64c83c | 47 | { |
20172573073 | 1:849d12c9b2be | 48 | |
20172573073 | 0:366b3e64c83c | 49 | while(true) { |
20172573073 | 0:366b3e64c83c | 50 | menor = sonar.getCm(); |
20172573073 | 0:366b3e64c83c | 51 | for(int i=0; i<40; i++) { |
20172573073 | 0:366b3e64c83c | 52 | myservo = i/40.0; |
20172573073 | 0:366b3e64c83c | 53 | da = sonar.getCm(); |
20172573073 | 2:ad8f0cdf7015 | 54 | w=i; |
20172573073 | 2:ad8f0cdf7015 | 55 | //pc.printf("%1.f , %.2f \n\r",float(i*4.5),da); |
20172573073 | 0:366b3e64c83c | 56 | if(menor > da) { |
20172573073 | 0:366b3e64c83c | 57 | menor = da; |
20172573073 | 0:366b3e64c83c | 58 | a=(i*180)/40; |
20172573073 | 0:366b3e64c83c | 59 | } |
20172573073 | 0:366b3e64c83c | 60 | //ThisThread::sleep_for(500); |
20172573073 | 1:849d12c9b2be | 61 | wait(0.05); |
20172573073 | 0:366b3e64c83c | 62 | } |
20172573073 | 0:366b3e64c83c | 63 | menor=sonar.getCm(); |
20172573073 | 0:366b3e64c83c | 64 | a=0; |
20172573073 | 0:366b3e64c83c | 65 | for(int i=40; i>0; i--) { |
20172573073 | 0:366b3e64c83c | 66 | myservo = i/40.0; |
20172573073 | 0:366b3e64c83c | 67 | da = sonar.getCm(); |
20172573073 | 2:ad8f0cdf7015 | 68 | w=i; |
20172573073 | 2:ad8f0cdf7015 | 69 | // pc.printf("%1.f , %.2f \n\r",float(i*4.5),da); |
20172573073 | 0:366b3e64c83c | 70 | if(menor > da) { |
20172573073 | 0:366b3e64c83c | 71 | menor = da; |
20172573073 | 0:366b3e64c83c | 72 | a=(i*180)/40; |
20172573073 | 0:366b3e64c83c | 73 | } |
20172573073 | 0:366b3e64c83c | 74 | //ThisThread::sleep_for(500); |
20172573073 | 1:849d12c9b2be | 75 | wait(0.05); |
20172573073 | 0:366b3e64c83c | 76 | } |
20172573073 | 0:366b3e64c83c | 77 | } |
20172573073 | 0:366b3e64c83c | 78 | } |
20172573073 | 0:366b3e64c83c | 79 | void seg7() |
20172573073 | 0:366b3e64c83c | 80 | { |
20172573073 | 0:366b3e64c83c | 81 | while(1) { |
20172573073 | 1:849d12c9b2be | 82 | int d,u,n,c; |
20172573073 | 0:366b3e64c83c | 83 | n=int(menor); |
20172573073 | 2:ad8f0cdf7015 | 84 | |
20172573073 | 1:849d12c9b2be | 85 | c= n/100; |
20172573073 | 1:849d12c9b2be | 86 | d = (n - (c*100))/10; |
20172573073 | 0:366b3e64c83c | 87 | u = n % 10; |
20172573073 | 0:366b3e64c83c | 88 | |
20172573073 | 0:366b3e64c83c | 89 | display1=1; |
20172573073 | 0:366b3e64c83c | 90 | display2=0; |
20172573073 | 1:849d12c9b2be | 91 | display3=0; |
20172573073 | 0:366b3e64c83c | 92 | display7Seg=anodeComun[d]; |
20172573073 | 1:849d12c9b2be | 93 | |
20172573073 | 0:366b3e64c83c | 94 | //ThisThread::sleep_for(25); |
20172573073 | 1:849d12c9b2be | 95 | wait(0.003); |
20172573073 | 1:849d12c9b2be | 96 | |
20172573073 | 0:366b3e64c83c | 97 | display1=0; |
20172573073 | 0:366b3e64c83c | 98 | display2=1; |
20172573073 | 1:849d12c9b2be | 99 | display3=0; |
20172573073 | 0:366b3e64c83c | 100 | display7Seg=anodeComun[u]; |
20172573073 | 1:849d12c9b2be | 101 | |
20172573073 | 0:366b3e64c83c | 102 | //ThisThread::sleep_for(25); |
20172573073 | 1:849d12c9b2be | 103 | wait(0.003); |
20172573073 | 2:ad8f0cdf7015 | 104 | |
20172573073 | 2:ad8f0cdf7015 | 105 | display1=0; |
20172573073 | 1:849d12c9b2be | 106 | display2=0; |
20172573073 | 1:849d12c9b2be | 107 | display3=1; |
20172573073 | 1:849d12c9b2be | 108 | display7Seg=anodeComun[c]; |
20172573073 | 1:849d12c9b2be | 109 | |
20172573073 | 1:849d12c9b2be | 110 | //ThisThread::sleep_for(25); |
20172573073 | 1:849d12c9b2be | 111 | wait(0.003); |
20172573073 | 2:ad8f0cdf7015 | 112 | |
20172573073 | 2:ad8f0cdf7015 | 113 | |
20172573073 | 0:366b3e64c83c | 114 | } |
20172573073 | 0:366b3e64c83c | 115 | } |
20172573073 | 0:366b3e64c83c | 116 | void oled() |
20172573073 | 0:366b3e64c83c | 117 | { |
20172573073 | 0:366b3e64c83c | 118 | while(1) { |
20172573073 | 0:366b3e64c83c | 119 | lcd.init(); |
20172573073 | 0:366b3e64c83c | 120 | lcd.speed (SSD1306::Fast); |
20172573073 | 0:366b3e64c83c | 121 | lcd.cls(); |
20172573073 | 1:849d12c9b2be | 122 | lcd.locate (4,1); |
20172573073 | 1:849d12c9b2be | 123 | lcd.printf ("dis menor :%2.f ",menor); |
20172573073 | 0:366b3e64c83c | 124 | lcd.locate (5,1); |
20172573073 | 1:849d12c9b2be | 125 | lcd.printf ("angulo :%d ",a); |
20172573073 | 0:366b3e64c83c | 126 | lcd.locate (1,0); |
20172573073 | 0:366b3e64c83c | 127 | time_t seconds = time(NULL); |
20172573073 | 1:849d12c9b2be | 128 | lcd.printf("Fecha= %s", ctime(&seconds)); |
20172573073 | 0:366b3e64c83c | 129 | lcd.redraw(); |
20172573073 | 1:849d12c9b2be | 130 | //ThisThread::sleep_for(500); |
20172573073 | 0:366b3e64c83c | 131 | wait(0.5); |
20172573073 | 0:366b3e64c83c | 132 | } |
20172573073 | 0:366b3e64c83c | 133 | } |
20172573073 | 1:849d12c9b2be | 134 | void rf() |
20172573073 | 0:366b3e64c83c | 135 | { |
20172573073 | 0:366b3e64c83c | 136 | while(1) { |
20172573073 | 2:ad8f0cdf7015 | 137 | al=0; |
20172573073 | 2:ad8f0cdf7015 | 138 | b=0; |
20172573073 | 1:849d12c9b2be | 139 | lcd.init(); |
20172573073 | 1:849d12c9b2be | 140 | lcd.speed (SSD1306::Fast); |
20172573073 | 1:849d12c9b2be | 141 | if ( ! RfChip.PICC_IsNewCardPresent()) { |
20172573073 | 1:849d12c9b2be | 142 | //ThisThread::sleep_for(500); |
20172573073 | 1:849d12c9b2be | 143 | wait_ms(500); |
20172573073 | 1:849d12c9b2be | 144 | continue; |
20172573073 | 1:849d12c9b2be | 145 | } |
20172573073 | 1:849d12c9b2be | 146 | if ( ! RfChip.PICC_ReadCardSerial()) { |
20172573073 | 1:849d12c9b2be | 147 | //ThisThread::sleep_for(500); |
20172573073 | 1:849d12c9b2be | 148 | wait_ms(500); |
20172573073 | 1:849d12c9b2be | 149 | continue; |
20172573073 | 1:849d12c9b2be | 150 | } |
20172573073 | 1:849d12c9b2be | 151 | uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); |
20172573073 | 1:849d12c9b2be | 152 | lcd.locate (6,3); |
20172573073 | 1:849d12c9b2be | 153 | lcd.printf(" %s ", RfChip.PICC_GetTypeName(piccType)); |
20172573073 | 1:849d12c9b2be | 154 | lcd.redraw(); |
20172573073 | 2:ad8f0cdf7015 | 155 | for (uint8_t i = 0; i < RfChip.uid.size; i++) { |
20172573073 | 2:ad8f0cdf7015 | 156 | |
20172573073 | 2:ad8f0cdf7015 | 157 | if(uint8_t (tar[i])==RfChip.uid.uidByte[i]){ |
20172573073 | 2:ad8f0cdf7015 | 158 | al=al+1; |
20172573073 | 2:ad8f0cdf7015 | 159 | }else{ |
20172573073 | 2:ad8f0cdf7015 | 160 | al=0; |
20172573073 | 2:ad8f0cdf7015 | 161 | } |
20172573073 | 2:ad8f0cdf7015 | 162 | } |
20172573073 | 2:ad8f0cdf7015 | 163 | for (uint8_t i = 0; i < RfChip.uid.size; i++) { |
20172573073 | 2:ad8f0cdf7015 | 164 | |
20172573073 | 2:ad8f0cdf7015 | 165 | if(uint8_t (llav[i])==RfChip.uid.uidByte[i]){ |
20172573073 | 2:ad8f0cdf7015 | 166 | b=b+1; |
20172573073 | 2:ad8f0cdf7015 | 167 | }else{ |
20172573073 | 2:ad8f0cdf7015 | 168 | b=0; |
20172573073 | 2:ad8f0cdf7015 | 169 | } |
20172573073 | 2:ad8f0cdf7015 | 170 | } |
20172573073 | 2:ad8f0cdf7015 | 171 | |
20172573073 | 0:366b3e64c83c | 172 | //ThisThread::sleep_for(1000); |
20172573073 | 1:849d12c9b2be | 173 | wait_ms(1000); |
20172573073 | 0:366b3e64c83c | 174 | } |
20172573073 | 0:366b3e64c83c | 175 | } |
20172573073 | 2:ad8f0cdf7015 | 176 | void serial() |
20172573073 | 2:ad8f0cdf7015 | 177 | { |
20172573073 | 2:ad8f0cdf7015 | 178 | while(1) { |
20172573073 | 1:849d12c9b2be | 179 | |
20172573073 | 2:ad8f0cdf7015 | 180 | time_t seconds = time(NULL); |
20172573073 | 1:849d12c9b2be | 181 | |
20172573073 | 2:ad8f0cdf7015 | 182 | char buffer2[32]; |
20172573073 | 2:ad8f0cdf7015 | 183 | strftime(buffer2, 32, "%m/%d/%y", localtime(&seconds)); |
20172573073 | 2:ad8f0cdf7015 | 184 | pc.printf("%s", buffer2); |
20172573073 | 2:ad8f0cdf7015 | 185 | |
20172573073 | 2:ad8f0cdf7015 | 186 | char buffer1[32]; |
20172573073 | 2:ad8f0cdf7015 | 187 | strftime(buffer1, 32, "%H:%M:%S", localtime(&seconds)); |
20172573073 | 2:ad8f0cdf7015 | 188 | pc.printf(" , %s", buffer1); |
20172573073 | 2:ad8f0cdf7015 | 189 | pc.printf(" , %1.f , %.2f , ",float(w*4.5),da); |
20172573073 | 2:ad8f0cdf7015 | 190 | if(al==4){ |
20172573073 | 2:ad8f0cdf7015 | 191 | pc.printf("tarjeta"); |
20172573073 | 2:ad8f0cdf7015 | 192 | }else if(b==4){ |
20172573073 | 2:ad8f0cdf7015 | 193 | pc.printf("llavero"); |
20172573073 | 2:ad8f0cdf7015 | 194 | }else{ |
20172573073 | 2:ad8f0cdf7015 | 195 | pc.printf("sin lectura"); |
20172573073 | 2:ad8f0cdf7015 | 196 | } |
20172573073 | 2:ad8f0cdf7015 | 197 | pc.printf("\n\r"); |
20172573073 | 2:ad8f0cdf7015 | 198 | wait(0.04); |
20172573073 | 2:ad8f0cdf7015 | 199 | |
20172573073 | 2:ad8f0cdf7015 | 200 | } |
20172573073 | 2:ad8f0cdf7015 | 201 | } |
20172573073 | 0:366b3e64c83c | 202 | int main() |
20172573073 | 0:366b3e64c83c | 203 | { |
20172573073 | 0:366b3e64c83c | 204 | myservo.calibrate(0.0015,360); |
20172573073 | 1:849d12c9b2be | 205 | set_time(1591673863); |
20172573073 | 1:849d12c9b2be | 206 | RfChip.PCD_Init(); |
20172573073 | 0:366b3e64c83c | 207 | t.start((servo)); |
20172573073 | 1:849d12c9b2be | 208 | t.set_priority (osPriorityNormal3); |
20172573073 | 0:366b3e64c83c | 209 | t1.start(seg7); |
20172573073 | 2:ad8f0cdf7015 | 210 | t1.set_priority (osPriorityNormal); |
20172573073 | 0:366b3e64c83c | 211 | t2.start(oled); |
20172573073 | 1:849d12c9b2be | 212 | t2.set_priority (osPriorityNormal1); |
20172573073 | 2:ad8f0cdf7015 | 213 | t3.start(serial); |
20172573073 | 2:ad8f0cdf7015 | 214 | t2.set_priority (osPriorityNormal2); |
20172573073 | 1:849d12c9b2be | 215 | t4.start(rf); |
20172573073 | 1:849d12c9b2be | 216 | t.join(); |
20172573073 | 0:366b3e64c83c | 217 | } |