Blinker

Dependencies:   TextLCD mbed MMA8451Q

Revision:
17:c96c9af96dbb
Parent:
16:3775d1fa074b
Child:
18:8876780cb268
--- a/Blinker.cpp	Fri Jun 27 11:22:07 2014 +0000
+++ b/Blinker.cpp	Fri Jun 27 13:04:38 2014 +0000
@@ -2,50 +2,46 @@
 //Hardware: Freescale FRDM KL25Z & SaintSmart LCD Keypad Shield
 //Copyright: Andre Ehwein, Marcel Berrang, Daniel Knopp
 
-#include "mbed.h"                                                      //common library für mbed
-#include "TextLCD.h"                                                   //library für den LCD Shield
-#include "TSISensor.h"                                                 //library für den TSi Sensor Idee: Helligkeit des Lichts einstellen
-
+#include "mbed.h"                                                       //Allgemeine Bib. für mbed
+#include "TextLCD.h"                                                    //Bib. für den LCD Shield
 
-DigitalOut Blinker_L_Led(PTD2);                                         // Digitaler Ausgang für die Blinker links
-DigitalOut Blinker_R_Led(PTD3);                                         // Digitaler Ausgang für die Blinker rechts
-DigitalOut Licht(PTA12);                                                 // Digitaler Ausgang für Licht
-//PwmOut Licht(PTD0);  
+DigitalOut Blinker_L_Led(PTD2);                                         // Digitaler Ausgang für den linken Blinker
+DigitalOut Blinker_R_Led(PTD3);                                         // Digitaler Ausgang für den rechten Blinker
+DigitalOut Licht(PTA12);                                                // Digitaler Ausgang für das Licht  
 
-AnalogIn KEYS(PTB0);                                                    //Analog In um die Spannung an PTB0 zu bestimmen und die Buttons zu erkennen
-TextLCD lcd(PTA13, PTD5, PTA4, PTA5, PTC8, PTC9, TextLCD::LCD16x2);     //Konfiguration des LCD-Keypad-Schields mit Pins: rs, e, d4, d5, d6, d7
+AnalogIn KEYS(PTB0);                                                    //AnalogIn um die Spannung zu bestimmen und die Buttons zu erkennen
+TextLCD lcd(PTA13, PTD5, PTA4, PTA5, PTC8, PTC9, TextLCD::LCD16x2);     //Konfiguration des LCD-Keypad-Shields mit Pins: rs, e, d4, d5, d6, d7
 
-//TSISensor touchpad;
-
+//Definition der Tasten des Keypad Shields
 #define RIGHT_KEY   0
 #define UP_KEY      1
 #define DOWN_KEY    2
 #define LEFT_KEY    3
 #define SELECT_KEY  4
-#define NO_KEY      5                                                  //definiert die Keys des LCD Shields
+#define NO_KEY      5                                                 
 
-//Funktions Prototypen
-void Programmwahl();
-int read_KEY();
-float z = 0.4;                                                         //Blinkrythmus
-float wt=0.4;                                                         
-int buttonState = 0;
+//Variablendefinition
+int read_KEY();                                                         //Eingang Keypad
+int buttonState;  
+float z = 0.35;                                                         //Blinkrythmus
+float wt=0.4;                                                           //Wartezeit vor Tastenerfassung (wichtig)
+                                                
 
+//Definition der Unterprogramme
 void BlinkerL();
 void BlinkerR();
 void BlinkerW();
-//void Slider();
+void Programmwahl();
 
+//Hauptprogramm  
 int main()
-{   
-    //Licht = 1; 
-    //wait(0.1);                               
-    lcd.cls();
-    lcd.locate(3,0);
-    lcd.printf("Welcome To");
+{                           
+    lcd.cls();                                                          //LCD rücksetzen
+    lcd.locate(3,0);                                                    //Posiiton der Anzeige festlegen
+    lcd.printf("Welcome To");                                           //Textausgabe auf LCD 
     lcd.locate(4,1);
     lcd.printf("smartBIG");
-    char h;
+    char h;                                                             //Schleife für Willkomenslicht
         for (h=2; h>=1; h--)
             {
                 Licht=0;
@@ -55,31 +51,30 @@
             }
     wait(1);
       
-    Programmwahl();
+    Programmwahl();                                                     //Aufruf der Programmauswahl
 }
 
-
-
+//Unterprogramm Programmwahl
 void Programmwahl()
 {
-    Blinker_L_Led = 0;  
+    Blinker_L_Led = 0;                                                  //Blinker zurücksetzen
     Blinker_R_Led = 0;
     lcd.cls();
     lcd.locate(4,1);
     lcd.printf("smartBIG");       
-    while(1)
+    while(1)                                                            //Schleife zur Programmwahlabfrage 
         {
-        wait(wt);                                     // Wichtig!!! Richtige Zuordnung der Buttons
-        buttonState = read_KEY();
-        if (buttonState == LEFT_KEY)
+        wait(wt);                                                       
+        buttonState = read_KEY();                                       //Einlesen des betätigten Buttons
+        if (buttonState == LEFT_KEY)                                    //Linker Button
         {
             BlinkerL();
         }
-        if (buttonState == RIGHT_KEY)
+        if (buttonState == RIGHT_KEY)                                   //Rechter Button
         {
             BlinkerR();
         }
-        if (buttonState == UP_KEY)
+        if (buttonState == UP_KEY)                                      //Oben Button
         {
             BlinkerW();
         }
@@ -87,10 +82,9 @@
 }
 
 // Programm für die Ansteuerung der linken Blinker
-
 void BlinkerL()
 {
-    buttonState = NO_KEY;
+    buttonState = NO_KEY;                                               //Zurücksetzen des Button-Status aus Programmauswahl
     lcd.cls();
     lcd.locate(1,1);
     lcd.printf("Blinker  Links");
@@ -101,14 +95,9 @@
     wait(wt);
     buttonState = read_KEY();
     
-        if (buttonState == LEFT_KEY)                // Einstieg Dauerblinker
+        if (buttonState == LEFT_KEY)                                    //Einstieg Dauerblinker links
         {   while(1)
             {
-                
-                /*buttonState = read_KEY();
-                wait(0.05);
-                if (buttonState == DOWN_KEY)        // Abbruchbedingung Dauerblinker
-                { Programmwahl();}*/
                 Blinker_L_Led = 0;
                 buttonState = NO_KEY;
                 lcd.cls();
@@ -121,17 +110,17 @@
                 wait(wt);
                 buttonState = read_KEY();
                 
-                if (buttonState == DOWN_KEY)        // Abbruchbedingung Dauerblinker
+                if (buttonState == DOWN_KEY)                            //Abbruchbedingung Dauerblinker links
                 {
-                Programmwahl();
+                Programmwahl();                                         //Zurück zur Programmwahl
                 }
-                if (buttonState == UP_KEY)        // Einstieg Warnblinker
+                if (buttonState == UP_KEY)                              //Einstieg Warnblinker
                 { BlinkerW();}
             }   
         }
-        else                                        //Kompfortblinker
+        else                                                            //Einstieg Komfortblinker links
             {
-            char x = 3;
+            char x = 3;                                                 //x Zählvariable
             for (x=3; x>=1; x--)
             {
                 Blinker_L_Led = 0;
@@ -140,19 +129,18 @@
                 lcd.printf("Komfortblinker");
                 wait(z);
                 lcd.locate(15,0);
-                lcd.printf("%d",x);
+                lcd.printf("%d",x);                                     //Ausgabe der Var. x auf dem LCD
                 lcd.locate(0,0);
                 lcd.printf("<--");
                 Blinker_L_Led = 1;
                 wait(z);
             }
             }
-            Programmwahl();                                         // Zurück zur Programmwahl
-            }
+            Programmwahl();                                        
+        }
 
 
-// Programm für die Ansteuerung des rechten Blinker
-
+// Programm für die Ansteuerung der rechten Blinker
 void BlinkerR()
 {
     buttonState = NO_KEY;
@@ -166,14 +154,9 @@
     wait(wt);
     buttonState = read_KEY();
     
-        if (buttonState == RIGHT_KEY)                // Einstieg Dauerblinker
+        if (buttonState == RIGHT_KEY)                                   //Einstieg Dauerblinker rechts
         {   while(1)
             {
-                
-                /*buttonState = read_KEY();
-                wait(0.05);
-                if (buttonState == DOWN_KEY)        // Abbruchbedingung Dauerblinker
-                { Programmwahl();}*/
                 Blinker_R_Led = 0;
                 buttonState = NO_KEY;
                 lcd.cls();
@@ -186,15 +169,15 @@
                 wait(wt);
                 buttonState = read_KEY();
                 
-                if (buttonState == DOWN_KEY)        // Abbruchbedingung Dauerblinker
+                if (buttonState == DOWN_KEY)                            //Abbruchbedingung Dauerblinker rechts
                 {
                 Programmwahl();
                 }
-                if (buttonState == UP_KEY)        // Einstieg Warnblinker
+                if (buttonState == UP_KEY)                              //Einstieg Warnblinker
                 { BlinkerW();}
             }   
         }
-        else                                        //Kompfortblinker
+        else                                                            //Einstieg Komfortblinker rechts
             {
             char x = 3;
             for (x=3; x>=1; x--)
@@ -212,14 +195,14 @@
                 wait(z);
             }
             }
-            Programmwahl();                                         // Zurück zur Programmwahl
+            Programmwahl();                                             //Zurück zur Programmwahl
             }
 
 // Programm für die Ansteuerung der Warnblinkroutine
-
 void BlinkerW()
 {
-     while(1){
+     while(1)
+     {
              buttonState = NO_KEY;
              lcd.cls();
              lcd.locate(2,1);
@@ -233,36 +216,29 @@
              lcd.printf("<--");
              Blinker_L_Led = 1;
              Blinker_R_Led = 1;
+             
              wait(wt);
              buttonState = read_KEY();
-        if (buttonState==DOWN_KEY || buttonState==UP_KEY)
-        {
-        Programmwahl();
-        }
-}
-    /*if (buttonState == DOWN_KEY)        // Abbruchbedingung Dauerblinker
+        if (buttonState==DOWN_KEY || buttonState==UP_KEY)               //Doppelte Abschaltbedingung
         {
         Programmwahl();
         }
-    */
+     }
 }
 
-    
-
-int read_KEY()                                                     //Bestimmt, welche Taste gerade gedrückt wird
+//Routine zum Auslesem der betätigten Taste
+int read_KEY()                                                     
 {   
     int adc_key_in = 0;
-    adc_key_in = KEYS.read_u16 ();                                      // read the value from the sensor //Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]. 
-                                                                        // watch approching values for pushbutton and set table below
-                                                                        // add approx 500 for safety
-    if (adc_key_in > 65000)    {return NO_KEY;} 
-    if (adc_key_in < 50)       {return RIGHT_KEY;}
-    //if (adc_key_in < 790)      {return SELECT_KEY;} 
+    adc_key_in = KEYS.read_u16 ();                                      //Einlesen der aktuell anliegenden Spannung 
+                                                                                                                                                
+    if (adc_key_in > 65000)    {return NO_KEY;}                         //Zuordnung der ermittelten Spannungswerte in Intervallen (aufgrund Schwankungen) 
+    if (adc_key_in < 300)      {return RIGHT_KEY;}
     if (adc_key_in < 15000)    {return UP_KEY;}
     if (adc_key_in < 35000)    {return DOWN_KEY;}
     if (adc_key_in < 53000)    {return LEFT_KEY;}
       
-    return NO_KEY;  //when no value readable, also return NO_KEY...
+    return NO_KEY;                                                      //Wenn kein Wert eingelesen werden kann
 }
 
 //Ende
\ No newline at end of file