![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Klasa ne radi
Fork of TipkovnicaZadnje by
main.cpp@1:290c6681a06a, 2018-01-17 (annotated)
- Committer:
- resetar
- Date:
- Wed Jan 17 13:23:22 2018 +0000
- Revision:
- 1:290c6681a06a
- Parent:
- 0:23662f0a15af
Ne radi
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
resetar | 0:23662f0a15af | 1 | #include "mbed.h" |
resetar | 0:23662f0a15af | 2 | /** Initial test of the ultrasonic sensor HC-SR04 */ |
resetar | 0:23662f0a15af | 3 | #include "TextLCD.h" |
resetar | 0:23662f0a15af | 4 | #include "KEY_PAD.h" |
resetar | 0:23662f0a15af | 5 | TextLCD lcd(p7, p8, p9, p10, p11, p12); |
resetar | 0:23662f0a15af | 6 | InterruptIn echo(p5); // echo pin |
resetar | 0:23662f0a15af | 7 | DigitalOut trigger(p6); // trigger pin |
resetar | 0:23662f0a15af | 8 | Timer timer; // echo pulsewidth measurement |
resetar | 0:23662f0a15af | 9 | Timer timer2; |
resetar | 0:23662f0a15af | 10 | float brzina=0; |
resetar | 0:23662f0a15af | 11 | float predzadnji; |
resetar | 0:23662f0a15af | 12 | float vrijeme=0; |
resetar | 0:23662f0a15af | 13 | float distance = 0; |
resetar | 0:23662f0a15af | 14 | DigitalOut led1(p28);//crveno |
resetar | 0:23662f0a15af | 15 | DigitalOut led2(p29); |
resetar | 0:23662f0a15af | 16 | DigitalOut led3(p30); |
resetar | 0:23662f0a15af | 17 | /** Start the echo pulsewidth measurement */ |
resetar | 0:23662f0a15af | 18 | void startTimer() |
resetar | 0:23662f0a15af | 19 | { |
resetar | 0:23662f0a15af | 20 | timer.start(); // start the timer |
resetar | 0:23662f0a15af | 21 | } |
resetar | 0:23662f0a15af | 22 | /** Stop the echo pulsewidth measurement */ |
resetar | 0:23662f0a15af | 23 | void stopTimer() |
resetar | 0:23662f0a15af | 24 | { |
resetar | 0:23662f0a15af | 25 | timer.stop(); // stop the timer |
resetar | 0:23662f0a15af | 26 | } |
resetar | 0:23662f0a15af | 27 | |
resetar | 0:23662f0a15af | 28 | Ticker debounce; |
resetar | 0:23662f0a15af | 29 | |
resetar | 0:23662f0a15af | 30 | |
resetar | 0:23662f0a15af | 31 | |
resetar | 0:23662f0a15af | 32 | |
resetar | 0:23662f0a15af | 33 | |
resetar | 0:23662f0a15af | 34 | |
resetar | 0:23662f0a15af | 35 | void enable_keys(){ |
resetar | 0:23662f0a15af | 36 | red1.fall(&get_row1); |
resetar | 0:23662f0a15af | 37 | red2.fall(&get_row2); |
resetar | 0:23662f0a15af | 38 | red3.fall(&get_row3); |
resetar | 0:23662f0a15af | 39 | red4.fall(&get_row4); |
resetar | 0:23662f0a15af | 40 | |
resetar | 0:23662f0a15af | 41 | debounce.detach(); |
resetar | 0:23662f0a15af | 42 | } |
resetar | 0:23662f0a15af | 43 | |
resetar | 0:23662f0a15af | 44 | int main() { |
resetar | 0:23662f0a15af | 45 | |
resetar | 0:23662f0a15af | 46 | /** configure the rising edge to start the timer */ |
resetar | 0:23662f0a15af | 47 | echo.rise(&startTimer); |
resetar | 0:23662f0a15af | 48 | |
resetar | 0:23662f0a15af | 49 | /** configure the falling edge to stop the timer */ |
resetar | 0:23662f0a15af | 50 | echo.fall(&stopTimer); |
resetar | 0:23662f0a15af | 51 | timer2.start(); |
resetar | 0:23662f0a15af | 52 | led1=0; |
resetar | 0:23662f0a15af | 53 | led2=0; |
resetar | 0:23662f0a15af | 54 | led3=1; |
resetar | 0:23662f0a15af | 55 | |
resetar | 0:23662f0a15af | 56 | stupci.mode(OpenDrain); |
resetar | 0:23662f0a15af | 57 | stupci.mode(PullUp); |
resetar | 0:23662f0a15af | 58 | |
resetar | 0:23662f0a15af | 59 | red1.mode(PullUp); |
resetar | 0:23662f0a15af | 60 | red2.mode(PullUp); |
resetar | 0:23662f0a15af | 61 | red3.mode(PullUp); |
resetar | 0:23662f0a15af | 62 | red4.mode(PullUp); |
resetar | 0:23662f0a15af | 63 | |
resetar | 0:23662f0a15af | 64 | red1.fall(&get_row1); |
resetar | 0:23662f0a15af | 65 | red2.fall(&get_row2); |
resetar | 0:23662f0a15af | 66 | red3.fall(&get_row3); |
resetar | 0:23662f0a15af | 67 | red4.fall(&get_row4); |
resetar | 0:23662f0a15af | 68 | |
resetar | 0:23662f0a15af | 69 | while(1) { |
resetar | 0:23662f0a15af | 70 | if(red1 == 1 && red2 == 1 && red3 == 1 && red4 == 1){ |
resetar | 0:23662f0a15af | 71 | if(key>0){ |
resetar | 0:23662f0a15af | 72 | izlaz=key; |
resetar | 0:23662f0a15af | 73 | //pc.printf("\n\r %d", code); |
resetar | 0:23662f0a15af | 74 | __enable_irq(); |
resetar | 0:23662f0a15af | 75 | debounce.attach(&enable_keys, 0.5); //////////////////////////////// |
resetar | 0:23662f0a15af | 76 | } |
resetar | 0:23662f0a15af | 77 | key=0; |
resetar | 0:23662f0a15af | 78 | wait_ms(100); |
resetar | 0:23662f0a15af | 79 | } |
resetar | 0:23662f0a15af | 80 | |
resetar | 0:23662f0a15af | 81 | switch(pass_code){ |
resetar | 0:23662f0a15af | 82 | case(0): |
resetar | 0:23662f0a15af | 83 | lcd.locate(0,3); |
resetar | 0:23662f0a15af | 84 | lcd.printf("Unesite mod rada"); |
resetar | 0:23662f0a15af | 85 | break; |
resetar | 0:23662f0a15af | 86 | |
resetar | 0:23662f0a15af | 87 | case(123): |
resetar | 0:23662f0a15af | 88 | /** Start the measurement by sending the 10us trigger pulse */ |
resetar | 0:23662f0a15af | 89 | trigger = 1; |
resetar | 0:23662f0a15af | 90 | wait_us(10); |
resetar | 0:23662f0a15af | 91 | trigger = 0; |
resetar | 0:23662f0a15af | 92 | /** Wait for the sensor to finish measurement (generate rise and fall interrupts). |
resetar | 0:23662f0a15af | 93 | * Minimum wait time is determined by maximum measurement distance of 400 cm. |
resetar | 0:23662f0a15af | 94 | * t_min = 400 * 58 = 23200 us = 23.2 ms */ |
resetar | 0:23662f0a15af | 95 | wait(0.2); |
resetar | 0:23662f0a15af | 96 | predzadnji=distance; |
resetar | 0:23662f0a15af | 97 | /** calculate the distance in cm */ |
resetar | 0:23662f0a15af | 98 | distance = timer.read() * 1e6 / 58; |
resetar | 0:23662f0a15af | 99 | timer.reset(); // reset the timer to 0 |
resetar | 0:23662f0a15af | 100 | brzina=(distance-predzadnji)/(timer2.read()-vrijeme); |
resetar | 0:23662f0a15af | 101 | timer2.reset(); |
resetar | 0:23662f0a15af | 102 | timer2.start(); |
resetar | 0:23662f0a15af | 103 | vrijeme=timer2.read(); |
resetar | 0:23662f0a15af | 104 | /** Print the result in cm to the terminal with 1 decimal place |
resetar | 0:23662f0a15af | 105 | * (number 5 after % means that total of 5 digits will be reserved |
resetar | 0:23662f0a15af | 106 | * for printing the number, including the dot and one decimal place). */ |
resetar | 0:23662f0a15af | 107 | |
resetar | 0:23662f0a15af | 108 | if (brzina>=20) { |
resetar | 0:23662f0a15af | 109 | lcd.cls(); |
resetar | 0:23662f0a15af | 110 | lcd.locate(0,0); |
resetar | 0:23662f0a15af | 111 | lcd.printf("Daljina:%5.1f cm", distance); |
resetar | 0:23662f0a15af | 112 | lcd.locate(0,1); |
resetar | 0:23662f0a15af | 113 | lcd.printf("Brzina:%5.1f cm/s",brzina); |
resetar | 0:23662f0a15af | 114 | lcd.locate(0,2); |
resetar | 0:23662f0a15af | 115 | lcd.printf("Palim crveno!!!"); |
resetar | 0:23662f0a15af | 116 | led3=0; |
resetar | 0:23662f0a15af | 117 | led2=1; |
resetar | 0:23662f0a15af | 118 | wait(3); |
resetar | 0:23662f0a15af | 119 | led2=0; |
resetar | 0:23662f0a15af | 120 | led1=1; |
resetar | 0:23662f0a15af | 121 | wait(10); |
resetar | 0:23662f0a15af | 122 | led2=1; |
resetar | 0:23662f0a15af | 123 | wait(4); |
resetar | 0:23662f0a15af | 124 | led3=1; |
resetar | 0:23662f0a15af | 125 | led1=0; |
resetar | 0:23662f0a15af | 126 | led2=0; |
resetar | 0:23662f0a15af | 127 | } else if (brzina < 20 && brzina >5) { |
resetar | 0:23662f0a15af | 128 | lcd.cls(); |
resetar | 0:23662f0a15af | 129 | lcd.locate(0,0); |
resetar | 0:23662f0a15af | 130 | lcd.printf("Daljina:%5.1f cm", distance); |
resetar | 0:23662f0a15af | 131 | lcd.locate(0,1); |
resetar | 0:23662f0a15af | 132 | lcd.printf("Brzina:%5.1f cm/s",brzina); |
resetar | 0:23662f0a15af | 133 | lcd.locate(0,2); |
resetar | 0:23662f0a15af | 134 | lcd.printf("Usporite!"); |
resetar | 0:23662f0a15af | 135 | wait(2); |
resetar | 0:23662f0a15af | 136 | } else { |
resetar | 0:23662f0a15af | 137 | lcd.cls(); |
resetar | 0:23662f0a15af | 138 | lcd.locate(0,0); |
resetar | 0:23662f0a15af | 139 | lcd.printf("Daljina:%5.1f cm", distance); |
resetar | 0:23662f0a15af | 140 | lcd.locate(0,1); |
resetar | 0:23662f0a15af | 141 | lcd.printf("Brzina:%5.1f cm/s",brzina); |
resetar | 0:23662f0a15af | 142 | lcd.locate(0,2); |
resetar | 0:23662f0a15af | 143 | lcd.printf("OK"); |
resetar | 0:23662f0a15af | 144 | } |
resetar | 0:23662f0a15af | 145 | break; |
resetar | 0:23662f0a15af | 146 | |
resetar | 0:23662f0a15af | 147 | case(456): |
resetar | 0:23662f0a15af | 148 | lcd.cls(); |
resetar | 0:23662f0a15af | 149 | lcd.locate(0,3); |
resetar | 0:23662f0a15af | 150 | lcd.printf(" %d", pass_code); |
resetar | 0:23662f0a15af | 151 | break; |
resetar | 0:23662f0a15af | 152 | |
resetar | 0:23662f0a15af | 153 | case(789): |
resetar | 0:23662f0a15af | 154 | lcd.cls(); |
resetar | 0:23662f0a15af | 155 | lcd.locate(0,3); |
resetar | 0:23662f0a15af | 156 | lcd.printf(" %d", pass_code); |
resetar | 0:23662f0a15af | 157 | break; |
resetar | 0:23662f0a15af | 158 | } |
resetar | 0:23662f0a15af | 159 | |
resetar | 0:23662f0a15af | 160 | } |
resetar | 0:23662f0a15af | 161 | } |