![](/media/cache/group/tvz-logo.png.50x50_q85.png)
AUTO_RAMPA_SAMBOLEK
Dependencies: mbed HCSR04 TextLCD
main.cpp@1:d4075ab93f44, 2021-09-21 (annotated)
- Committer:
- msambolek
- Date:
- Tue Sep 21 15:48:06 2021 +0000
- Revision:
- 1:d4075ab93f44
- Parent:
- 0:83ddf858b8ae
Finalna verzija
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
msambolek | 0:83ddf858b8ae | 1 | #include "mbed.h" |
msambolek | 0:83ddf858b8ae | 2 | #include "TextLCD.h" |
msambolek | 0:83ddf858b8ae | 3 | #include "HCSR04.h" |
msambolek | 0:83ddf858b8ae | 4 | |
msambolek | 0:83ddf858b8ae | 5 | TextLCD lcd(PB_10, PA_8, PA_10, PB_3, PB_5, PB_4); //inicijacija 20x4 LCD-a |
msambolek | 1:d4075ab93f44 | 6 | HCSR04 hcsr(PA_6, PA_7); //inicijalizacija senzora udaljenosti |
msambolek | 1:d4075ab93f44 | 7 | DigitalOut Stop(PB_9); |
msambolek | 0:83ddf858b8ae | 8 | DigitalOut Kreni(PB_8); |
msambolek | 0:83ddf858b8ae | 9 | |
msambolek | 1:d4075ab93f44 | 10 | InterruptIn Tipka(PA_5); |
msambolek | 1:d4075ab93f44 | 11 | Timer debounce; //deklaracija timera za debounce |
msambolek | 1:d4075ab93f44 | 12 | void proces(void); //deklaracija funkcije |
msambolek | 0:83ddf858b8ae | 13 | |
msambolek | 1:d4075ab93f44 | 14 | PwmOut SM(PB_0); //deklaracija izlaznog PWM pina |
msambolek | 0:83ddf858b8ae | 15 | |
msambolek | 1:d4075ab93f44 | 16 | Ticker provjera; //deklaracija tickera |
msambolek | 0:83ddf858b8ae | 17 | |
msambolek | 1:d4075ab93f44 | 18 | Timer ZatRamp; //deklaracija timera za zadržavanje rampe |
msambolek | 0:83ddf858b8ae | 19 | |
msambolek | 1:d4075ab93f44 | 20 | void mjerenje() //funkcija za detekciju predmeta ispred senzora udaljenosti |
msambolek | 0:83ddf858b8ae | 21 | { |
msambolek | 0:83ddf858b8ae | 22 | hcsr.startMeasurement(); |
msambolek | 1:d4075ab93f44 | 23 | if( hcsr.getDistance_mm()<90 && Kreni==1) //predmet ispred senzora udaljenosti |
msambolek | 0:83ddf858b8ae | 24 | { |
msambolek | 0:83ddf858b8ae | 25 | SM=0.105; |
msambolek | 0:83ddf858b8ae | 26 | } |
msambolek | 1:d4075ab93f44 | 27 | if( hcsr.getDistance_mm()>90 && Kreni == 1) |
msambolek | 0:83ddf858b8ae | 28 | { |
msambolek | 1:d4075ab93f44 | 29 | ZatRamp.start(); // timer za spuštanje rampe |
msambolek | 0:83ddf858b8ae | 30 | Stop=1; |
msambolek | 0:83ddf858b8ae | 31 | Kreni=0; |
msambolek | 0:83ddf858b8ae | 32 | lcd.cls(); |
msambolek | 0:83ddf858b8ae | 33 | lcd.locate(4,1); |
msambolek | 0:83ddf858b8ae | 34 | lcd.printf("Dobro dosli!\n"); |
msambolek | 0:83ddf858b8ae | 35 | lcd.locate(1,2); |
msambolek | 0:83ddf858b8ae | 36 | lcd.printf("Pritisnite tipku !"); |
msambolek | 0:83ddf858b8ae | 37 | } |
msambolek | 0:83ddf858b8ae | 38 | } |
msambolek | 0:83ddf858b8ae | 39 | |
msambolek | 0:83ddf858b8ae | 40 | int main() |
msambolek | 0:83ddf858b8ae | 41 | { |
msambolek | 0:83ddf858b8ae | 42 | |
msambolek | 1:d4075ab93f44 | 43 | provjera.attach(&mjerenje,0.1); //tickeru se dodijeljuje funkcija mjerenje |
msambolek | 1:d4075ab93f44 | 44 | SM=0.0534; //pocetni otklon rampe |
msambolek | 1:d4075ab93f44 | 45 | Stop = 1; //aktivan crvena LEDica |
msambolek | 0:83ddf858b8ae | 46 | Kreni=0; |
msambolek | 1:d4075ab93f44 | 47 | Tipka.mode(PullUp); //digitalni ulaz za tipku pritegnut sa pull up otpornikom |
msambolek | 1:d4075ab93f44 | 48 | SM.period(0.020); //period PWM signala za motor |
msambolek | 0:83ddf858b8ae | 49 | lcd.cls(); |
msambolek | 0:83ddf858b8ae | 50 | lcd.locate(4,1); |
msambolek | 0:83ddf858b8ae | 51 | lcd.printf("Dobro dosli!\n"); |
msambolek | 0:83ddf858b8ae | 52 | lcd.locate(1,2); |
msambolek | 0:83ddf858b8ae | 53 | lcd.printf("Pritisnite tipku !"); |
msambolek | 0:83ddf858b8ae | 54 | |
msambolek | 0:83ddf858b8ae | 55 | |
msambolek | 0:83ddf858b8ae | 56 | while(1) |
msambolek | 0:83ddf858b8ae | 57 | { |
msambolek | 1:d4075ab93f44 | 58 | debounce.start(); //start tajmera za debounce tipke |
msambolek | 1:d4075ab93f44 | 59 | Tipka.fall(&proces); //na padajući brid tipke za podizanje rampe poziva se funkcija proces |
msambolek | 1:d4075ab93f44 | 60 | if(ZatRamp.read_ms()>5000) //provjera vrijednosti timera za spuštanje rampe |
msambolek | 0:83ddf858b8ae | 61 | { |
msambolek | 0:83ddf858b8ae | 62 | SM=0.0534; |
msambolek | 0:83ddf858b8ae | 63 | ZatRamp.reset(); |
msambolek | 1:d4075ab93f44 | 64 | } |
msambolek | 0:83ddf858b8ae | 65 | } |
msambolek | 0:83ddf858b8ae | 66 | } |
msambolek | 0:83ddf858b8ae | 67 | |
msambolek | 0:83ddf858b8ae | 68 | void proces(){ |
msambolek | 0:83ddf858b8ae | 69 | if (debounce.read_ms()>200) |
msambolek | 0:83ddf858b8ae | 70 | { |
msambolek | 0:83ddf858b8ae | 71 | hcsr.startMeasurement(); |
msambolek | 0:83ddf858b8ae | 72 | if(hcsr.getDistance_mm()<90 && Kreni==0){ |
msambolek | 0:83ddf858b8ae | 73 | lcd.cls(); |
msambolek | 0:83ddf858b8ae | 74 | lcd.locate(4,1); |
msambolek | 0:83ddf858b8ae | 75 | lcd.printf("Dovidjenja!\n"); |
msambolek | 0:83ddf858b8ae | 76 | lcd.locate(4,2); |
msambolek | 0:83ddf858b8ae | 77 | lcd.printf("Sretan put!"); |
msambolek | 0:83ddf858b8ae | 78 | Stop=!Stop; |
msambolek | 0:83ddf858b8ae | 79 | Kreni=!Kreni; |
msambolek | 0:83ddf858b8ae | 80 | SM=0.105; |
msambolek | 0:83ddf858b8ae | 81 | } |
msambolek | 1:d4075ab93f44 | 82 | } |
msambolek | 1:d4075ab93f44 | 83 | debounce.reset(); |
msambolek | 1:d4075ab93f44 | 84 | } |