marko resetar / Mbed 2 deprecated TipkovnicaZadnje

Dependencies:   TextLCD key_pad mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 BusInOut stupci(p23, p22, p21);
00031 
00032 InterruptIn red1(p27);
00033 InterruptIn red2(p26);
00034 InterruptIn red3(p25);
00035 InterruptIn red4(p24);
00036 
00037 BusOut izlaz(LED1, LED2, LED3, LED4);
00038 
00039 volatile extern int row=0;
00040 volatile extern int col=0;
00041 volatile extern int key=0;
00042 volatile extern int step=0;
00043 volatile extern int code=0;
00044 volatile extern int pass_code=0;
00045 
00046 
00047 void enable_keys(){
00048     red1.fall(&get_row1);
00049     red2.fall(&get_row2);
00050     red3.fall(&get_row3);
00051     red4.fall(&get_row4);
00052     
00053     debounce.detach();
00054 }
00055 
00056 int main() {
00057     
00058     /** configure the rising edge to start the timer */
00059     echo.rise(&startTimer);
00060 
00061     /** configure the falling edge to stop the timer */
00062     echo.fall(&stopTimer);
00063     timer2.start();
00064     led1=0;
00065     led2=0;
00066     led3=1;
00067     
00068     stupci.mode(OpenDrain);
00069     stupci.mode(PullUp);
00070     
00071     red1.mode(PullUp);
00072     red2.mode(PullUp);
00073     red3.mode(PullUp);
00074     red4.mode(PullUp);
00075     
00076     red1.fall(&get_row1);
00077     red2.fall(&get_row2);
00078     red3.fall(&get_row3);
00079     red4.fall(&get_row4);
00080     
00081     while(1) {
00082             if(red1 == 1 && red2 == 1 && red3 == 1 && red4 == 1){
00083                 if(key>0){
00084                     izlaz=key;
00085                     //pc.printf("\n\r %d", code);
00086                     __enable_irq();
00087                     debounce.attach(&enable_keys, 0.5); ////////////////////////////////
00088                 }
00089                 key=0;
00090                 wait_ms(100);
00091             }
00092             
00093             switch(pass_code){
00094                 case(0):
00095                     lcd.locate(0,3);
00096                     lcd.printf("Unesite mod rada");
00097                 break;
00098                 
00099                 case(123):
00100                     /** Start the measurement by sending the 10us trigger pulse */
00101                     trigger = 1;
00102                     wait_us(10);
00103                     trigger = 0;
00104                     /** Wait for the sensor to finish measurement (generate rise and fall interrupts).
00105                     *  Minimum wait time is determined by maximum measurement distance of 400 cm.
00106                     *  t_min = 400 * 58 = 23200 us = 23.2 ms */
00107                     wait(0.2);
00108                     predzadnji=distance;
00109                     /** calculate the distance in cm */
00110                     distance = timer.read() * 1e6 / 58;
00111                     timer.reset(); // reset the timer to 0
00112                     brzina=(distance-predzadnji)/(timer2.read()-vrijeme);
00113                     timer2.reset();
00114                     timer2.start();
00115                     vrijeme=timer2.read();
00116                     /** Print the result in cm to the terminal with 1 decimal place
00117                     *  (number 5 after % means that total of 5 digits will be reserved
00118                     *  for printing the number, including the dot and one decimal place). */
00119      
00120                     if (brzina>=20) {
00121                         lcd.cls();
00122                         lcd.locate(0,0);
00123                         lcd.printf("Daljina:%5.1f cm", distance);
00124                         lcd.locate(0,1);
00125                         lcd.printf("Brzina:%5.1f cm/s",brzina);
00126                         lcd.locate(0,2);
00127                         lcd.printf("Palim crveno!!!");
00128                         led3=0;
00129                         led2=1;
00130                         wait(3);
00131                         led2=0;
00132                         led1=1;
00133                         wait(10);
00134                         led2=1;
00135                         wait(4);
00136                         led3=1;
00137                         led1=0;
00138                         led2=0;
00139                     } else if (brzina < 20 && brzina >5) {
00140                         lcd.cls();
00141                         lcd.locate(0,0);
00142                         lcd.printf("Daljina:%5.1f cm", distance);
00143                         lcd.locate(0,1);
00144                         lcd.printf("Brzina:%5.1f cm/s",brzina);
00145                         lcd.locate(0,2);
00146                         lcd.printf("Usporite!");
00147                         wait(2);
00148                     } else {
00149                         lcd.cls();
00150                         lcd.locate(0,0);
00151                         lcd.printf("Daljina:%5.1f cm", distance);
00152                         lcd.locate(0,1);
00153                         lcd.printf("Brzina:%5.1f cm/s",brzina);
00154                         lcd.locate(0,2);
00155                         lcd.printf("OK");
00156                     }
00157                 break;
00158                 
00159                 case(456):
00160                     lcd.cls();
00161                     lcd.locate(0,3);
00162                     lcd.printf(" %d", pass_code);
00163                 break;
00164 
00165                 case(789):
00166                     lcd.cls();
00167                     lcd.locate(0,3);
00168                     lcd.printf(" %d", pass_code);
00169                 break;
00170             }
00171         
00172     }
00173 }