![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
.
Dependencies: mbed Servo MFRC522_Updated HC-SR04 ssd1306_library
main.cpp@0:366b3e64c83c, 2020-06-06 (annotated)
- Committer:
- 20172573073
- Date:
- Sat Jun 06 21:46:44 2020 +0000
- Revision:
- 0:366b3e64c83c
- Child:
- 1:849d12c9b2be
escaner rtos; ;
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 | 0:366b3e64c83c | 7 | SSD1306 lcd (D7,D6); |
20172573073 | 0:366b3e64c83c | 8 | BusOut display7Seg(PTC10, PTC7, PTC6, PTC4, PTC0, PTB19, PTB18); |
20172573073 | 0:366b3e64c83c | 9 | DigitalOut display1(PTC11); |
20172573073 | 0:366b3e64c83c | 10 | DigitalOut display2(PTC13); |
20172573073 | 0:366b3e64c83c | 11 | Servo myservo(D2); |
20172573073 | 0:366b3e64c83c | 12 | Thread thread; |
20172573073 | 0:366b3e64c83c | 13 | Thread t; |
20172573073 | 0:366b3e64c83c | 14 | Thread t1; |
20172573073 | 0:366b3e64c83c | 15 | Thread t2; |
20172573073 | 0:366b3e64c83c | 16 | Thread t3; |
20172573073 | 0:366b3e64c83c | 17 | Serial pc(USBTX, USBRX); |
20172573073 | 0:366b3e64c83c | 18 | HCSR04 sonar(D3,D4); |
20172573073 | 0:366b3e64c83c | 19 | int a,q; |
20172573073 | 0:366b3e64c83c | 20 | float menor; |
20172573073 | 0:366b3e64c83c | 21 | float da; |
20172573073 | 0:366b3e64c83c | 22 | float dis = 0.0; |
20172573073 | 0:366b3e64c83c | 23 | |
20172573073 | 0:366b3e64c83c | 24 | |
20172573073 | 0:366b3e64c83c | 25 | int anodeComun[10] = { |
20172573073 | 0:366b3e64c83c | 26 | 0b0000001, // 0 |
20172573073 | 0:366b3e64c83c | 27 | 0b1001111, // 1 |
20172573073 | 0:366b3e64c83c | 28 | 0b0010010, // 2 |
20172573073 | 0:366b3e64c83c | 29 | 0b0000110, // 3 |
20172573073 | 0:366b3e64c83c | 30 | 0b1001100, // 4 |
20172573073 | 0:366b3e64c83c | 31 | 0b0100100, // 5 |
20172573073 | 0:366b3e64c83c | 32 | 0b0100000, // 6 |
20172573073 | 0:366b3e64c83c | 33 | 0b0001111, // 7 |
20172573073 | 0:366b3e64c83c | 34 | 0b0000000, // 8 |
20172573073 | 0:366b3e64c83c | 35 | 0b0001100, // 9 |
20172573073 | 0:366b3e64c83c | 36 | }; |
20172573073 | 0:366b3e64c83c | 37 | |
20172573073 | 0:366b3e64c83c | 38 | void servo() |
20172573073 | 0:366b3e64c83c | 39 | { |
20172573073 | 0:366b3e64c83c | 40 | while(true) { |
20172573073 | 0:366b3e64c83c | 41 | menor = sonar.getCm(); |
20172573073 | 0:366b3e64c83c | 42 | a=0; |
20172573073 | 0:366b3e64c83c | 43 | for(int i=0; i<40; i++) { |
20172573073 | 0:366b3e64c83c | 44 | myservo = i/40.0; |
20172573073 | 0:366b3e64c83c | 45 | da = sonar.getCm(); |
20172573073 | 0:366b3e64c83c | 46 | pc.printf("Distancia %.2f cm \n\r",da); |
20172573073 | 0:366b3e64c83c | 47 | if(menor > da) { |
20172573073 | 0:366b3e64c83c | 48 | menor = da; |
20172573073 | 0:366b3e64c83c | 49 | a=(i*180)/40; |
20172573073 | 0:366b3e64c83c | 50 | } |
20172573073 | 0:366b3e64c83c | 51 | //ThisThread::sleep_for(500); |
20172573073 | 0:366b3e64c83c | 52 | wait(0.5); |
20172573073 | 0:366b3e64c83c | 53 | } |
20172573073 | 0:366b3e64c83c | 54 | pc.printf("menor final %.2f cm \n\r",menor); |
20172573073 | 0:366b3e64c83c | 55 | menor=sonar.getCm(); |
20172573073 | 0:366b3e64c83c | 56 | a=0; |
20172573073 | 0:366b3e64c83c | 57 | for(int i=40; i>0; i--) { |
20172573073 | 0:366b3e64c83c | 58 | myservo = i/40.0; |
20172573073 | 0:366b3e64c83c | 59 | da = sonar.getCm(); |
20172573073 | 0:366b3e64c83c | 60 | pc.printf("Distancia %.2f cm \n\r",da); |
20172573073 | 0:366b3e64c83c | 61 | if(menor > da) { |
20172573073 | 0:366b3e64c83c | 62 | menor = da; |
20172573073 | 0:366b3e64c83c | 63 | a=(i*180)/40; |
20172573073 | 0:366b3e64c83c | 64 | } |
20172573073 | 0:366b3e64c83c | 65 | //ThisThread::sleep_for(500); |
20172573073 | 0:366b3e64c83c | 66 | wait(0.5); |
20172573073 | 0:366b3e64c83c | 67 | } |
20172573073 | 0:366b3e64c83c | 68 | pc.printf("menor %.2f cm \n\r",menor); |
20172573073 | 0:366b3e64c83c | 69 | } |
20172573073 | 0:366b3e64c83c | 70 | } |
20172573073 | 0:366b3e64c83c | 71 | void seg7() |
20172573073 | 0:366b3e64c83c | 72 | { |
20172573073 | 0:366b3e64c83c | 73 | while(1) { |
20172573073 | 0:366b3e64c83c | 74 | int d,u,n; |
20172573073 | 0:366b3e64c83c | 75 | n=int(menor); |
20172573073 | 0:366b3e64c83c | 76 | |
20172573073 | 0:366b3e64c83c | 77 | d = n /10; |
20172573073 | 0:366b3e64c83c | 78 | u = n % 10; |
20172573073 | 0:366b3e64c83c | 79 | |
20172573073 | 0:366b3e64c83c | 80 | display1=1; |
20172573073 | 0:366b3e64c83c | 81 | display2=0; |
20172573073 | 0:366b3e64c83c | 82 | display7Seg=anodeComun[d]; |
20172573073 | 0:366b3e64c83c | 83 | //ThisThread::sleep_for(25); |
20172573073 | 0:366b3e64c83c | 84 | wait(0.025); |
20172573073 | 0:366b3e64c83c | 85 | display1=0; |
20172573073 | 0:366b3e64c83c | 86 | display2=1; |
20172573073 | 0:366b3e64c83c | 87 | display7Seg=anodeComun[u]; |
20172573073 | 0:366b3e64c83c | 88 | //ThisThread::sleep_for(25); |
20172573073 | 0:366b3e64c83c | 89 | wait(0.025); |
20172573073 | 0:366b3e64c83c | 90 | } |
20172573073 | 0:366b3e64c83c | 91 | } |
20172573073 | 0:366b3e64c83c | 92 | void oled() |
20172573073 | 0:366b3e64c83c | 93 | { |
20172573073 | 0:366b3e64c83c | 94 | while(1) { |
20172573073 | 0:366b3e64c83c | 95 | lcd.init(); |
20172573073 | 0:366b3e64c83c | 96 | lcd.speed (SSD1306::Fast); |
20172573073 | 0:366b3e64c83c | 97 | lcd.cls(); |
20172573073 | 0:366b3e64c83c | 98 | lcd.locate (3,1); |
20172573073 | 0:366b3e64c83c | 99 | lcd.printf ("angulo :%d ",a); |
20172573073 | 0:366b3e64c83c | 100 | lcd.locate (5,1); |
20172573073 | 0:366b3e64c83c | 101 | lcd.printf ("dis menor :%2.f ",menor); |
20172573073 | 0:366b3e64c83c | 102 | set_time(q); |
20172573073 | 0:366b3e64c83c | 103 | lcd.locate (1,0); |
20172573073 | 0:366b3e64c83c | 104 | time_t seconds = time(NULL); |
20172573073 | 0:366b3e64c83c | 105 | lcd.printf("fecha %s",ctime(&seconds)); |
20172573073 | 0:366b3e64c83c | 106 | lcd.redraw(); |
20172573073 | 0:366b3e64c83c | 107 | wait(0.5); |
20172573073 | 0:366b3e64c83c | 108 | } |
20172573073 | 0:366b3e64c83c | 109 | } |
20172573073 | 0:366b3e64c83c | 110 | void tiempo() |
20172573073 | 0:366b3e64c83c | 111 | { |
20172573073 | 0:366b3e64c83c | 112 | while(1) { |
20172573073 | 0:366b3e64c83c | 113 | q=q+1; |
20172573073 | 0:366b3e64c83c | 114 | //ThisThread::sleep_for(1000); |
20172573073 | 0:366b3e64c83c | 115 | wait(1); |
20172573073 | 0:366b3e64c83c | 116 | } |
20172573073 | 0:366b3e64c83c | 117 | } |
20172573073 | 0:366b3e64c83c | 118 | int main() |
20172573073 | 0:366b3e64c83c | 119 | { |
20172573073 | 0:366b3e64c83c | 120 | myservo.calibrate(0.0015,360); |
20172573073 | 0:366b3e64c83c | 121 | t.start((servo)); |
20172573073 | 0:366b3e64c83c | 122 | t1.start(seg7); |
20172573073 | 0:366b3e64c83c | 123 | t2.start(oled); |
20172573073 | 0:366b3e64c83c | 124 | t3.start(tiempo); |
20172573073 | 0:366b3e64c83c | 125 | q=1590029272; |
20172573073 | 0:366b3e64c83c | 126 | q=q-((5*60)*60); |
20172573073 | 0:366b3e64c83c | 127 | //thread.flags_set(STOP_FLAG); |
20172573073 | 0:366b3e64c83c | 128 | //thread.join(); |
20172573073 | 0:366b3e64c83c | 129 | } |