![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
.
Dependencies: mbed Servo MFRC522_Updated HC-SR04 ssd1306_library
main.cpp@1:849d12c9b2be, 2020-06-09 (annotated)
- Committer:
- 20172573073
- Date:
- Tue Jun 09 05:15:28 2020 +0000
- Revision:
- 1:849d12c9b2be
- Parent:
- 0:366b3e64c83c
- Child:
- 2:ad8f0cdf7015
rtos : escaner_rf
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 | 0:366b3e64c83c | 24 | int a,q; |
20172573073 | 0:366b3e64c83c | 25 | float menor; |
20172573073 | 0:366b3e64c83c | 26 | float da; |
20172573073 | 0:366b3e64c83c | 27 | float dis = 0.0; |
20172573073 | 0:366b3e64c83c | 28 | |
20172573073 | 0:366b3e64c83c | 29 | |
20172573073 | 0:366b3e64c83c | 30 | int anodeComun[10] = { |
20172573073 | 0:366b3e64c83c | 31 | 0b0000001, // 0 |
20172573073 | 0:366b3e64c83c | 32 | 0b1001111, // 1 |
20172573073 | 0:366b3e64c83c | 33 | 0b0010010, // 2 |
20172573073 | 0:366b3e64c83c | 34 | 0b0000110, // 3 |
20172573073 | 0:366b3e64c83c | 35 | 0b1001100, // 4 |
20172573073 | 0:366b3e64c83c | 36 | 0b0100100, // 5 |
20172573073 | 0:366b3e64c83c | 37 | 0b0100000, // 6 |
20172573073 | 0:366b3e64c83c | 38 | 0b0001111, // 7 |
20172573073 | 0:366b3e64c83c | 39 | 0b0000000, // 8 |
20172573073 | 0:366b3e64c83c | 40 | 0b0001100, // 9 |
20172573073 | 0:366b3e64c83c | 41 | }; |
20172573073 | 0:366b3e64c83c | 42 | |
20172573073 | 0:366b3e64c83c | 43 | void servo() |
20172573073 | 0:366b3e64c83c | 44 | { |
20172573073 | 1:849d12c9b2be | 45 | |
20172573073 | 0:366b3e64c83c | 46 | while(true) { |
20172573073 | 0:366b3e64c83c | 47 | menor = sonar.getCm(); |
20172573073 | 0:366b3e64c83c | 48 | for(int i=0; i<40; i++) { |
20172573073 | 0:366b3e64c83c | 49 | myservo = i/40.0; |
20172573073 | 0:366b3e64c83c | 50 | da = sonar.getCm(); |
20172573073 | 1:849d12c9b2be | 51 | pc.printf("%1.f , %.2f \n\r",float(i*4.5),da); |
20172573073 | 0:366b3e64c83c | 52 | if(menor > da) { |
20172573073 | 0:366b3e64c83c | 53 | menor = da; |
20172573073 | 0:366b3e64c83c | 54 | a=(i*180)/40; |
20172573073 | 0:366b3e64c83c | 55 | } |
20172573073 | 0:366b3e64c83c | 56 | //ThisThread::sleep_for(500); |
20172573073 | 1:849d12c9b2be | 57 | wait(0.05); |
20172573073 | 0:366b3e64c83c | 58 | } |
20172573073 | 0:366b3e64c83c | 59 | menor=sonar.getCm(); |
20172573073 | 0:366b3e64c83c | 60 | a=0; |
20172573073 | 0:366b3e64c83c | 61 | for(int i=40; i>0; i--) { |
20172573073 | 0:366b3e64c83c | 62 | myservo = i/40.0; |
20172573073 | 0:366b3e64c83c | 63 | da = sonar.getCm(); |
20172573073 | 1:849d12c9b2be | 64 | pc.printf("%1.f , %.2f \n\r",float(i*4.5),da); |
20172573073 | 0:366b3e64c83c | 65 | if(menor > da) { |
20172573073 | 0:366b3e64c83c | 66 | menor = da; |
20172573073 | 0:366b3e64c83c | 67 | a=(i*180)/40; |
20172573073 | 0:366b3e64c83c | 68 | } |
20172573073 | 0:366b3e64c83c | 69 | //ThisThread::sleep_for(500); |
20172573073 | 1:849d12c9b2be | 70 | wait(0.05); |
20172573073 | 0:366b3e64c83c | 71 | } |
20172573073 | 0:366b3e64c83c | 72 | } |
20172573073 | 0:366b3e64c83c | 73 | } |
20172573073 | 0:366b3e64c83c | 74 | void seg7() |
20172573073 | 0:366b3e64c83c | 75 | { |
20172573073 | 0:366b3e64c83c | 76 | while(1) { |
20172573073 | 1:849d12c9b2be | 77 | int d,u,n,c; |
20172573073 | 0:366b3e64c83c | 78 | n=int(menor); |
20172573073 | 1:849d12c9b2be | 79 | |
20172573073 | 1:849d12c9b2be | 80 | c= n/100; |
20172573073 | 1:849d12c9b2be | 81 | d = (n - (c*100))/10; |
20172573073 | 0:366b3e64c83c | 82 | u = n % 10; |
20172573073 | 0:366b3e64c83c | 83 | |
20172573073 | 0:366b3e64c83c | 84 | display1=1; |
20172573073 | 0:366b3e64c83c | 85 | display2=0; |
20172573073 | 1:849d12c9b2be | 86 | display3=0; |
20172573073 | 0:366b3e64c83c | 87 | display7Seg=anodeComun[d]; |
20172573073 | 1:849d12c9b2be | 88 | |
20172573073 | 0:366b3e64c83c | 89 | //ThisThread::sleep_for(25); |
20172573073 | 1:849d12c9b2be | 90 | wait(0.003); |
20172573073 | 1:849d12c9b2be | 91 | |
20172573073 | 0:366b3e64c83c | 92 | display1=0; |
20172573073 | 0:366b3e64c83c | 93 | display2=1; |
20172573073 | 1:849d12c9b2be | 94 | display3=0; |
20172573073 | 0:366b3e64c83c | 95 | display7Seg=anodeComun[u]; |
20172573073 | 1:849d12c9b2be | 96 | |
20172573073 | 0:366b3e64c83c | 97 | //ThisThread::sleep_for(25); |
20172573073 | 1:849d12c9b2be | 98 | wait(0.003); |
20172573073 | 1:849d12c9b2be | 99 | |
20172573073 | 1:849d12c9b2be | 100 | display1=0; |
20172573073 | 1:849d12c9b2be | 101 | display2=0; |
20172573073 | 1:849d12c9b2be | 102 | display3=1; |
20172573073 | 1:849d12c9b2be | 103 | display7Seg=anodeComun[c]; |
20172573073 | 1:849d12c9b2be | 104 | |
20172573073 | 1:849d12c9b2be | 105 | //ThisThread::sleep_for(25); |
20172573073 | 1:849d12c9b2be | 106 | wait(0.003); |
20172573073 | 1:849d12c9b2be | 107 | |
20172573073 | 1:849d12c9b2be | 108 | |
20172573073 | 0:366b3e64c83c | 109 | } |
20172573073 | 0:366b3e64c83c | 110 | } |
20172573073 | 0:366b3e64c83c | 111 | void oled() |
20172573073 | 0:366b3e64c83c | 112 | { |
20172573073 | 0:366b3e64c83c | 113 | while(1) { |
20172573073 | 0:366b3e64c83c | 114 | lcd.init(); |
20172573073 | 0:366b3e64c83c | 115 | lcd.speed (SSD1306::Fast); |
20172573073 | 0:366b3e64c83c | 116 | lcd.cls(); |
20172573073 | 1:849d12c9b2be | 117 | lcd.locate (4,1); |
20172573073 | 1:849d12c9b2be | 118 | lcd.printf ("dis menor :%2.f ",menor); |
20172573073 | 0:366b3e64c83c | 119 | lcd.locate (5,1); |
20172573073 | 1:849d12c9b2be | 120 | lcd.printf ("angulo :%d ",a); |
20172573073 | 0:366b3e64c83c | 121 | lcd.locate (1,0); |
20172573073 | 0:366b3e64c83c | 122 | time_t seconds = time(NULL); |
20172573073 | 1:849d12c9b2be | 123 | lcd.printf("Fecha= %s", ctime(&seconds)); |
20172573073 | 0:366b3e64c83c | 124 | lcd.redraw(); |
20172573073 | 1:849d12c9b2be | 125 | //ThisThread::sleep_for(500); |
20172573073 | 0:366b3e64c83c | 126 | wait(0.5); |
20172573073 | 0:366b3e64c83c | 127 | } |
20172573073 | 0:366b3e64c83c | 128 | } |
20172573073 | 1:849d12c9b2be | 129 | void rf() |
20172573073 | 0:366b3e64c83c | 130 | { |
20172573073 | 0:366b3e64c83c | 131 | while(1) { |
20172573073 | 1:849d12c9b2be | 132 | lcd.init(); |
20172573073 | 1:849d12c9b2be | 133 | lcd.speed (SSD1306::Fast); |
20172573073 | 1:849d12c9b2be | 134 | if ( ! RfChip.PICC_IsNewCardPresent()) { |
20172573073 | 1:849d12c9b2be | 135 | //ThisThread::sleep_for(500); |
20172573073 | 1:849d12c9b2be | 136 | wait_ms(500); |
20172573073 | 1:849d12c9b2be | 137 | continue; |
20172573073 | 1:849d12c9b2be | 138 | } |
20172573073 | 1:849d12c9b2be | 139 | if ( ! RfChip.PICC_ReadCardSerial()) { |
20172573073 | 1:849d12c9b2be | 140 | //ThisThread::sleep_for(500); |
20172573073 | 1:849d12c9b2be | 141 | wait_ms(500); |
20172573073 | 1:849d12c9b2be | 142 | continue; |
20172573073 | 1:849d12c9b2be | 143 | } |
20172573073 | 1:849d12c9b2be | 144 | uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); |
20172573073 | 1:849d12c9b2be | 145 | lcd.locate (6,3); |
20172573073 | 1:849d12c9b2be | 146 | lcd.printf(" %s ", RfChip.PICC_GetTypeName(piccType)); |
20172573073 | 1:849d12c9b2be | 147 | lcd.redraw(); |
20172573073 | 0:366b3e64c83c | 148 | //ThisThread::sleep_for(1000); |
20172573073 | 1:849d12c9b2be | 149 | wait_ms(1000); |
20172573073 | 0:366b3e64c83c | 150 | } |
20172573073 | 0:366b3e64c83c | 151 | } |
20172573073 | 1:849d12c9b2be | 152 | |
20172573073 | 1:849d12c9b2be | 153 | |
20172573073 | 0:366b3e64c83c | 154 | int main() |
20172573073 | 0:366b3e64c83c | 155 | { |
20172573073 | 0:366b3e64c83c | 156 | myservo.calibrate(0.0015,360); |
20172573073 | 1:849d12c9b2be | 157 | set_time(1591673863); |
20172573073 | 1:849d12c9b2be | 158 | RfChip.PCD_Init(); |
20172573073 | 0:366b3e64c83c | 159 | t.start((servo)); |
20172573073 | 1:849d12c9b2be | 160 | t.set_priority (osPriorityNormal3); |
20172573073 | 0:366b3e64c83c | 161 | t1.start(seg7); |
20172573073 | 1:849d12c9b2be | 162 | t1.set_priority (osPriorityNormal2); |
20172573073 | 0:366b3e64c83c | 163 | t2.start(oled); |
20172573073 | 1:849d12c9b2be | 164 | t2.set_priority (osPriorityNormal1); |
20172573073 | 1:849d12c9b2be | 165 | t4.start(rf); |
20172573073 | 1:849d12c9b2be | 166 | t.join(); |
20172573073 | 0:366b3e64c83c | 167 | } |