Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 /** Initial test of the ultrasonic sensor HC-SR04 */ 00003 #include "TextLCD.h" 00004 #include "KEY_PAD.h" 00005 TextLCD lcd(p7, p8, p9, p10, p11, p12); 00006 InterruptIn echo(p5); // echo pin 00007 DigitalOut trigger(p6); // trigger pin 00008 Timer timer; // echo pulsewidth measurement 00009 Timer timer2; 00010 float brzina=0; 00011 float predzadnji; 00012 float vrijeme=0; 00013 float distance = 0; 00014 DigitalOut led1(p28);//crveno 00015 DigitalOut led2(p29); 00016 DigitalOut led3(p30); 00017 /** Start the echo pulsewidth measurement */ 00018 void startTimer() 00019 { 00020 timer.start(); // start the timer 00021 } 00022 /** Stop the echo pulsewidth measurement */ 00023 void stopTimer() 00024 { 00025 timer.stop(); // stop the timer 00026 } 00027 00028 Ticker debounce; 00029 00030 00031 00032 00033 00034 00035 void enable_keys(){ 00036 red1.fall(&get_row1); 00037 red2.fall(&get_row2); 00038 red3.fall(&get_row3); 00039 red4.fall(&get_row4); 00040 00041 debounce.detach(); 00042 } 00043 00044 int main() { 00045 00046 /** configure the rising edge to start the timer */ 00047 echo.rise(&startTimer); 00048 00049 /** configure the falling edge to stop the timer */ 00050 echo.fall(&stopTimer); 00051 timer2.start(); 00052 led1=0; 00053 led2=0; 00054 led3=1; 00055 00056 stupci.mode(OpenDrain); 00057 stupci.mode(PullUp); 00058 00059 red1.mode(PullUp); 00060 red2.mode(PullUp); 00061 red3.mode(PullUp); 00062 red4.mode(PullUp); 00063 00064 red1.fall(&get_row1); 00065 red2.fall(&get_row2); 00066 red3.fall(&get_row3); 00067 red4.fall(&get_row4); 00068 00069 while(1) { 00070 if(red1 == 1 && red2 == 1 && red3 == 1 && red4 == 1){ 00071 if(key>0){ 00072 izlaz=key; 00073 //pc.printf("\n\r %d", code); 00074 __enable_irq(); 00075 debounce.attach(&enable_keys, 0.5); //////////////////////////////// 00076 } 00077 key=0; 00078 wait_ms(100); 00079 } 00080 00081 switch(pass_code){ 00082 case(0): 00083 lcd.locate(0,3); 00084 lcd.printf("Unesite mod rada"); 00085 break; 00086 00087 case(123): 00088 /** Start the measurement by sending the 10us trigger pulse */ 00089 trigger = 1; 00090 wait_us(10); 00091 trigger = 0; 00092 /** Wait for the sensor to finish measurement (generate rise and fall interrupts). 00093 * Minimum wait time is determined by maximum measurement distance of 400 cm. 00094 * t_min = 400 * 58 = 23200 us = 23.2 ms */ 00095 wait(0.2); 00096 predzadnji=distance; 00097 /** calculate the distance in cm */ 00098 distance = timer.read() * 1e6 / 58; 00099 timer.reset(); // reset the timer to 0 00100 brzina=(distance-predzadnji)/(timer2.read()-vrijeme); 00101 timer2.reset(); 00102 timer2.start(); 00103 vrijeme=timer2.read(); 00104 /** Print the result in cm to the terminal with 1 decimal place 00105 * (number 5 after % means that total of 5 digits will be reserved 00106 * for printing the number, including the dot and one decimal place). */ 00107 00108 if (brzina>=20) { 00109 lcd.cls(); 00110 lcd.locate(0,0); 00111 lcd.printf("Daljina:%5.1f cm", distance); 00112 lcd.locate(0,1); 00113 lcd.printf("Brzina:%5.1f cm/s",brzina); 00114 lcd.locate(0,2); 00115 lcd.printf("Palim crveno!!!"); 00116 led3=0; 00117 led2=1; 00118 wait(3); 00119 led2=0; 00120 led1=1; 00121 wait(10); 00122 led2=1; 00123 wait(4); 00124 led3=1; 00125 led1=0; 00126 led2=0; 00127 } else if (brzina < 20 && brzina >5) { 00128 lcd.cls(); 00129 lcd.locate(0,0); 00130 lcd.printf("Daljina:%5.1f cm", distance); 00131 lcd.locate(0,1); 00132 lcd.printf("Brzina:%5.1f cm/s",brzina); 00133 lcd.locate(0,2); 00134 lcd.printf("Usporite!"); 00135 wait(2); 00136 } else { 00137 lcd.cls(); 00138 lcd.locate(0,0); 00139 lcd.printf("Daljina:%5.1f cm", distance); 00140 lcd.locate(0,1); 00141 lcd.printf("Brzina:%5.1f cm/s",brzina); 00142 lcd.locate(0,2); 00143 lcd.printf("OK"); 00144 } 00145 break; 00146 00147 case(456): 00148 lcd.cls(); 00149 lcd.locate(0,3); 00150 lcd.printf(" %d", pass_code); 00151 break; 00152 00153 case(789): 00154 lcd.cls(); 00155 lcd.locate(0,3); 00156 lcd.printf(" %d", pass_code); 00157 break; 00158 } 00159 00160 } 00161 }
Generated on Thu Jul 14 2022 10:48:10 by
1.7.2