Blinker

Dependencies:   TextLCD mbed MMA8451Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Blinker.cpp Source File

Blinker.cpp

00001 //Multifunktionsblinker
00002 //Hardware: Freescale FRDM KL25Z & SaintSmart LCD Keypad Shield
00003 //Copyright: Andre Ehwein, Marcel Berrang, Daniel Knopp
00004 
00005 #include "mbed.h"                                                       //Allgemeine Bib. für mbed
00006 #include "TextLCD.h"                                                    //Bib. für den LCD Shield
00007 
00008 DigitalOut Blinker_L_Led(PTD2);                                         // Digitaler Ausgang für den linken Blinker
00009 DigitalOut Blinker_R_Led(PTD3);                                         // Digitaler Ausgang für den rechten Blinker
00010 DigitalOut Licht(PTA12);                                                // Digitaler Ausgang für das Licht  
00011 
00012 AnalogIn KEYS(PTB0);                                                    //AnalogIn um die Spannung zu bestimmen und die Buttons zu erkennen
00013 TextLCD lcd(PTA13, PTD5, PTA4, PTA5, PTC8, PTC9, TextLCD::LCD16x2);     //Konfiguration des LCD-Keypad-Shields mit Pins: rs, e, d4, d5, d6, d7
00014 
00015 //Definition der Tasten des Keypad Shields
00016 #define RIGHT_KEY   0
00017 #define UP_KEY      1
00018 #define DOWN_KEY    2
00019 #define LEFT_KEY    3
00020 #define SELECT_KEY  4
00021 #define NO_KEY      5                                                 
00022 
00023 //Variablendefinition
00024 int read_KEY();                                                         //Eingang Keypad
00025 int buttonState;  
00026 float z = 0.35;                                                         //Blinkrythmus
00027 float wt=0.4;                                                           //Wartezeit vor Tastenerfassung (wichtig)
00028                                                 
00029 
00030 //Definition der Unterprogramme
00031 void BlinkerL();
00032 void BlinkerR();
00033 void BlinkerW();
00034 void Programmwahl();
00035 
00036 //Hauptprogramm  
00037 int main()
00038 {                           
00039     lcd.cls();                                                          //LCD rücksetzen
00040     lcd.locate(3,0);                                                    //Posiiton der Anzeige festlegen
00041     lcd.printf("Welcome To");                                           //Textausgabe auf LCD 
00042     lcd.locate(4,1);
00043     lcd.printf("smartBIG");
00044     char h;                                                             //Schleife für Willkomenslicht
00045         for (h=2; h>=1; h--)
00046             {
00047                 Licht=0;
00048                 wait(0.2);
00049                 Licht=1;
00050                 wait(0.2);
00051             }
00052     wait(1);
00053       
00054     Programmwahl();                                                     //Aufruf der Programmauswahl
00055 }
00056 
00057 //Unterprogramm Programmwahl
00058 void Programmwahl()
00059 {
00060     Blinker_L_Led = 0;                                                  //Blinker zurücksetzen
00061     Blinker_R_Led = 0;
00062     lcd.cls();
00063     lcd.locate(4,1);
00064     lcd.printf("smartBIG");       
00065     while(1)                                                            //Schleife zur Programmwahlabfrage 
00066         {
00067         wait(wt);                                                       
00068         buttonState = read_KEY();                                       //Einlesen des betätigten Buttons
00069         if (buttonState == LEFT_KEY)                                    //Linker Button
00070         {
00071             BlinkerL();
00072         }
00073         if (buttonState == RIGHT_KEY)                                   //Rechter Button
00074         {
00075             BlinkerR();
00076         }
00077         if (buttonState == UP_KEY)                                      //Oben Button
00078         {
00079             BlinkerW();
00080         }
00081     }
00082 }
00083 
00084 // Programm für die Ansteuerung der linken Blinker
00085 void BlinkerL()
00086 {
00087     buttonState = NO_KEY;                                               //Zurücksetzen des Button-Status aus Programmauswahl
00088     lcd.cls();
00089     lcd.locate(1,1);
00090     lcd.printf("Blinker  Links");
00091     wait(z);
00092     lcd.locate(0,0);
00093     lcd.printf("<--");
00094     Blinker_L_Led = 1;
00095     wait(wt);
00096     buttonState = read_KEY();
00097     
00098         if (buttonState == LEFT_KEY)                                    //Einstieg Dauerblinker links
00099         {   while(1)
00100             {
00101                 Blinker_L_Led = 0;
00102                 buttonState = NO_KEY;
00103                 lcd.cls();
00104                 lcd.locate(2,1);
00105                 lcd.printf("Dauerblinker");
00106                 wait(z);
00107                 lcd.locate(0,0);
00108                 lcd.printf("<--");
00109                 Blinker_L_Led = 1;
00110                 wait(wt);
00111                 buttonState = read_KEY();
00112                 
00113                 if (buttonState == DOWN_KEY)                            //Abbruchbedingung Dauerblinker links
00114                 {
00115                 Programmwahl();                                         //Zurück zur Programmwahl
00116                 }
00117                 if (buttonState == UP_KEY)                              //Einstieg Warnblinker
00118                 { BlinkerW();}
00119                 if (buttonState == RIGHT_KEY)                           //Einstieg Blinker Rechts
00120                 { 
00121                 Blinker_L_Led = 0;
00122                 BlinkerR();}
00123             }   
00124         }
00125         else                                                            //Einstieg Komfortblinker links
00126             {
00127             char x = 3;                                                 //x Zählvariable
00128             for (x=3; x>=1; x--)
00129             {
00130                 Blinker_L_Led = 0;
00131                 lcd.cls();
00132                 lcd.locate(1,1);
00133                 lcd.printf("Komfortblinker");
00134                 wait(z);
00135                 lcd.locate(15,0);
00136                 lcd.printf("%d",x);                                     //Ausgabe der Var. x auf dem LCD
00137                 lcd.locate(0,0);
00138                 lcd.printf("<--");
00139                 Blinker_L_Led = 1;
00140                 wait(z);
00141             }
00142             }
00143             Programmwahl();                                        
00144         }
00145 
00146 
00147 // Programm für die Ansteuerung der rechten Blinker
00148 void BlinkerR()
00149 {
00150     buttonState = NO_KEY;
00151     lcd.cls();
00152     lcd.locate(1,1);
00153     lcd.printf("Blinker  Rechts");
00154     wait(z);
00155     lcd.locate(13,0);
00156     lcd.printf("-->");
00157     Blinker_R_Led = 1;
00158     wait(wt);
00159     buttonState = read_KEY();
00160     
00161         if (buttonState == RIGHT_KEY)                                   //Einstieg Dauerblinker rechts
00162         {   while(1)
00163             {
00164                 Blinker_R_Led = 0;
00165                 buttonState = NO_KEY;
00166                 lcd.cls();
00167                 lcd.locate(2,1);
00168                 lcd.printf("Dauerblinker");
00169                 wait(z);
00170                 lcd.locate(13,0);
00171                 lcd.printf("-->");
00172                 Blinker_R_Led = 1;
00173                 wait(wt);
00174                 buttonState = read_KEY();
00175                 
00176                 if (buttonState == DOWN_KEY)                            //Abbruchbedingung Dauerblinker rechts
00177                 {
00178                 Programmwahl();
00179                 }
00180                 if (buttonState == UP_KEY)                              //Einstieg Warnblinker
00181                 { BlinkerW();}
00182                 if (buttonState == LEFT_KEY)                           //Einstieg Blinker Links
00183                 { 
00184                 Blinker_R_Led = 0;
00185                 BlinkerL();}
00186             }   
00187         }
00188         else                                                            //Einstieg Komfortblinker rechts
00189             {
00190             char x = 3;
00191             for (x=3; x>=1; x--)
00192             {
00193                 Blinker_R_Led = 0;
00194                 lcd.cls();
00195                 lcd.locate(1,1);
00196                 lcd.printf("Komfortblinker");
00197                 wait(z);
00198                 lcd.locate(0,0);
00199                 lcd.printf("%d",x);
00200                 lcd.locate(13,0);
00201                 lcd.printf("-->");
00202                 Blinker_R_Led = 1;
00203                 wait(z);
00204             }
00205             }
00206             Programmwahl();                                             //Zurück zur Programmwahl
00207             }
00208 
00209 // Programm für die Ansteuerung der Warnblinkroutine
00210 void BlinkerW()
00211 {
00212      while(1)
00213      {
00214              buttonState = NO_KEY;
00215              lcd.cls();
00216              lcd.locate(2,1);
00217              lcd.printf("Warnblinker!");
00218              Blinker_L_Led = 0;
00219              Blinker_R_Led = 0;
00220              wait(z);
00221              lcd.locate(13,0);
00222              lcd.printf("-->");
00223              lcd.locate(0,0);
00224              lcd.printf("<--");
00225              Blinker_L_Led = 1;
00226              Blinker_R_Led = 1;
00227              
00228              wait(wt);
00229              buttonState = read_KEY();
00230         if (buttonState==DOWN_KEY || buttonState==UP_KEY)               //Doppelte Abschaltbedingung
00231         {
00232         Programmwahl();
00233         }
00234      }
00235 }
00236 
00237 //Routine zum Auslesem der betätigten Taste
00238 int read_KEY()                                                     
00239 {   
00240     int adc_key_in = 0;
00241     adc_key_in = KEYS.read_u16 ();                                      //Einlesen der aktuell anliegenden Spannung 
00242                                                                                                                                                 
00243     if (adc_key_in > 65000)    {return NO_KEY;}                         //Zuordnung der ermittelten Spannungswerte in Intervallen (aufgrund Schwankungen) 
00244     if (adc_key_in < 300)      {return RIGHT_KEY;}
00245     if (adc_key_in < 15000)    {return UP_KEY;}
00246     if (adc_key_in < 35000)    {return DOWN_KEY;}
00247     if (adc_key_in < 53000)    {return LEFT_KEY;}
00248       
00249     return NO_KEY;                                                      //Wenn kein Wert eingelesen werden kann
00250 }
00251 
00252 //Ende