Relay tester over LCD screen

Dependencies:   mbed TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #// NOTES:
00002 // 1. mbed.h MUST BE the revision from 30 Mar 2016, otherwise it will behave strange and unexpected! Older or newer versions won't work.
00003 //    For example, with the latest version of mbed.h from 25 May 2016, the "wait" function does not work at all, it's the same as if it's not there.
00004 #include "mbed.h"
00005 #include "TextLCD.h"
00006 
00007 ////////////////////////////////
00008 // instantiations
00009 ////////////////////////////////
00010 Serial pc(SERIAL_TX, SERIAL_RX);
00011 AnalogIn button(A0); // Init button (SELECT, LEFT, UP, DOWN, RIGHT)
00012 TextLCD lcd(D8, D9, D4, D5, D6, D7);
00013 PwmOut backlight(D10); // Backlight LCD
00014 PwmOut blueLed(D15);
00015 Timer t;
00016 DigitalOut led(LED1);
00017 DigitalOut load(D3);
00018 DigitalIn measure(D2);
00019 
00020 ////////////////////////////////
00021 // variables and constants
00022 ////////////////////////////////
00023 
00024 // keyboard keys
00025 #define COMMAND_NONE 0
00026 #define COMMAND_UP 1
00027 #define COMMAND_DOWN 2
00028 #define COMMAND_LEFT 3
00029 #define COMMAND_RIGHT 4
00030 #define COMMAND_SELECT 5
00031 
00032 // config menu options
00033 #define MENU_NONE 0
00034 #define MENU_LED_STATE 1
00035 #define MENU_LED_VALUE 2
00036 
00037 int menuOptions[2] = {
00038     MENU_LED_STATE,
00039     MENU_LED_VALUE
00040 };
00041 
00042 // config
00043 struct config_t {
00044     bool ledLit;
00045     bool ledChanged;
00046     float ledValue;
00047 
00048     config_t():ledLit(true), ledChanged(true), ledValue(0.5){}
00049 
00050 } config;
00051 
00052 // variables
00053 int meas;
00054 int check=0;
00055 int millard=0;
00056 long counter=0;
00057 int counterfailure=0;
00058 int command = COMMAND_NONE;
00059 int lastCommand = COMMAND_NONE;
00060 bool isInConfig = false;
00061 
00062 int currentOption = MENU_NONE;
00063 int lastOption = currentOption;
00064 
00065 int lastDebounceTime; // the last time the output pin was toggled
00066 int debounceDelay = 100; // the debounce time; increase if the output flickers
00067 int debounceReading = COMMAND_NONE; // last read command from keyboard
00068 int debounceCommand = COMMAND_NONE; // current command
00069 int debounceLastCommand = COMMAND_NONE; // last command
00070 
00071 void printFirstScreen()
00072 {
00073     lcd.cls(); // Clear LCD
00074     lcd.locate(0, 0); // Set the pointer to the first column, first row
00075     lcd.printf("Press SELECT to enter setup"); // first time initialization
00076 }
00077 
00078 void initialize()
00079 {
00080     // Start serial USB
00081     pc.baud(9600);
00082 
00083     // Set the LCD backlight
00084     backlight.period(0.002);
00085     backlight = 0.7;
00086 
00087     // Print on LCD
00088     printFirstScreen();
00089 
00090      // Set the LED backlight
00091     blueLed.period(0.001); // blueLed.period: 0.01 = 100Hz; 0.02 = 50Hz; 0.001 = 1kHz; 0.0001 = 10kHz;
00092 
00093     // Start the timer
00094     t.start();
00095 
00096     // Serial debug
00097     pc.printf("Initialization complete\n");
00098 }
00099 
00100 // the debounce was taken from https://www.arduino.cc/en/Tutorial/Debounce
00101 void readKeyboard()
00102 {
00103     meas = button.read() * 1000; // Read the analog input value (value from 0.0 to 1.0) and convert to int value (from 0 to 1000)
00104 
00105     // read the buttons
00106     // right < 40
00107     // up: 130 - 170
00108     // down: 360 - 400
00109     // left: 580 - 620
00110     // select: 920 - 960
00111     // no button pressed: 1000
00112 
00113     // set a temporary command for the button that has been pushed
00114     if (meas < 40) { // right button
00115         debounceReading = COMMAND_RIGHT;
00116     }
00117     else if (meas > 130 && meas < 170) { // up button
00118         debounceReading = COMMAND_UP;
00119     }
00120     else if (meas > 360 && meas < 400) { // down button
00121         debounceReading = COMMAND_DOWN;
00122     }
00123     else if (meas > 580 && meas < 620) { // left button
00124         debounceReading = COMMAND_LEFT;
00125     }
00126     else if (meas > 920 && meas < 960) { // select button
00127         debounceReading = COMMAND_SELECT;
00128     }
00129     else {
00130         debounceReading = COMMAND_NONE;
00131     }
00132 
00133     int milliseconds = t.read_ms();
00134     int lastPushedTime = milliseconds - lastDebounceTime;
00135 
00136     // a button has been pushed or released
00137     if (debounceReading != debounceLastCommand) {
00138         // reset the debouncing timer
00139         lastDebounceTime = t.read_ms();
00140     }
00141 
00142     if (lastPushedTime > debounceDelay && debounceReading != debounceCommand) {
00143         debounceCommand = debounceReading;
00144         command = debounceCommand;
00145     }
00146 
00147     debounceLastCommand = debounceReading;
00148 
00149     // reset the timer if keyboard is idle for a specific amount of time
00150     if (debounceReading == COMMAND_NONE && lastPushedTime > 1200e3) { // 1200e3 = 20min
00151         t.reset();
00152     }
00153 }
00154 
00155 /*
00156 void printKeyboardCommandOnLCD()
00157 {
00158     if (command != lastCommand)
00159     {
00160         if (command != COMMAND_NONE) {
00161             lcd.cls();
00162             lcd.locate(0, 0);
00163         }
00164 
00165         if (command == COMMAND_RIGHT) {
00166             lcd.printf("BUTTON: Right");
00167         }
00168         else if (command == COMMAND_UP) {
00169             lcd.printf("BUTTON: Up");
00170         }
00171         else if (command == COMMAND_DOWN) {
00172             lcd.printf("BUTTON: Down");
00173         }
00174         else if (command == COMMAND_LEFT) {
00175             lcd.printf("BUTTON: Left");
00176         }
00177         else if (command == COMMAND_SELECT) {
00178             lcd.printf("BUTTON: Select");
00179         }
00180     }
00181 }
00182 */
00183 
00184 void resetKeyboardCommand()
00185 {
00186     lastCommand = command;
00187 }
00188 
00189 void resetMenuOption()
00190 {
00191     lastOption = currentOption;
00192 }
00193 
00194 void clearLCDSecondRow()
00195 {
00196     lcd.locate(0, 1);
00197     lcd.printf("                ");
00198     //lcd.printf("%*.0i", 16, 0);
00199 }
00200 
00201 void printLedStateConfig()
00202 {
00203     lcd.locate(0, 0);
00204     lcd.printf("1 Million test");
00205     lcd.locate(0, 1);
00206     lcd.printf("Press UP/DOWN"); 
00207     
00208 }
00209 
00210 void printLedValueConfig()
00211 {
00212     lcd.locate(0, 0);
00213     lcd.printf("5 Million test");
00214     lcd.locate(0, 1);
00215     lcd.printf("Press UP/DOWN"); 
00216 }
00217 
00218 void program1()
00219 {
00220 start:        
00221     resetKeyboardCommand();
00222     resetMenuOption();
00223     while(millard<1) 
00224     {
00225         load=1; //change status
00226         led=0;
00227         wait_ms(100);
00228         check=DigitalIn(measure);
00229         if ((check==1)||(counterfailure>3))
00230         {
00231             wait_ms(100);
00232             
00233                         
00234         }
00235         if ((check==0)&& (counterfailure<4))
00236         {
00237             lcd.cls();
00238             lcd.locate(0, 0);
00239             lcd.printf(" FAILURE CLOSING \n");
00240             lcd.locate(0, 1);
00241             lcd.printf("Cycles:%d\n\r", counter); //Contact didn't close. NC contact 
00242             goto failure;
00243             
00244             
00245             
00246         }
00247         load=0;
00248         wait_ms(100);
00249         check=DigitalIn(measure);
00250         if ((check==0)||(counterfailure>3))
00251         {
00252             wait_ms(100);
00253             
00254         }
00255         if ((check==1)&&(counterfailure<4))
00256         {
00257             lcd.cls();
00258             lcd.locate(0, 0);
00259             lcd.printf("FAILURE OPENING \n");
00260             lcd.locate(0, 1);
00261             lcd.printf("Cycles:%d\n\r", counter);
00262             goto failure;
00263         }
00264         counter++;
00265         if (counter>999999)
00266         {
00267             millard=millard+1;
00268             counter=0;
00269         }
00270         led=1;
00271         lcd.cls();
00272         lcd.locate(0, 0);
00273         lcd.printf("Cycles:%d\n\r", counter);
00274         lcd.locate(0, 1);
00275         lcd.printf("Million:%d\n\r", millard);
00276         
00277 
00278     }
00279     
00280 failure:      
00281     counterfailure++;
00282 
00283         while(counterfailure<4)
00284         {  
00285             readKeyboard();
00286             if (command != COMMAND_NONE)
00287             {
00288                 goto start; 
00289             }
00290         }
00291         if (counterfailure>3)
00292         {
00293             goto start;
00294         }
00295 }  
00296 
00297 void program2()
00298 {   
00299     millard=1;
00300     resetKeyboardCommand();
00301     resetMenuOption();
00302     while(millard<5) 
00303     {
00304         load=1; //change status
00305         led=0;
00306         wait_ms(200);
00307         load=0;
00308         wait_ms(200);
00309         counter++;
00310         if (counter>999999)
00311         {
00312             millard=millard+1;
00313             counter=0;
00314         }
00315         lcd.cls();
00316         lcd.locate(0, 0);
00317         lcd.printf("Cycles:%d\n\r", counter);
00318         lcd.locate(0, 1);
00319         lcd.printf("Million:%d\n\r", millard);    
00320 
00321     }
00322 }
00323     
00324 
00325 // handle keyboard input
00326 void handleConfigMenu()
00327 {
00328     // a new command has been triggered
00329     if (command != lastCommand)
00330     {
00331         if (isInConfig)
00332         {
00333              // exit the config menu
00334             if (command == COMMAND_SELECT)
00335             {
00336                 printFirstScreen();
00337                 isInConfig = false;
00338 
00339                 // revert/reset everything to the first state
00340                 currentOption = MENU_NONE;
00341                 resetMenuOption();
00342                 command = COMMAND_NONE;
00343                 resetKeyboardCommand();
00344             }
00345             // scroll through menu options
00346             else if (command == COMMAND_LEFT || command == COMMAND_RIGHT)
00347             {
00348                 // search for current element position
00349                 int menuOptionsSize = sizeof(menuOptions) / sizeof(int);
00350                 int pos = 0;
00351                 while (pos < menuOptionsSize && currentOption != menuOptions[pos]) {
00352                     pos++;
00353                 }
00354 
00355                 if (command == COMMAND_LEFT)
00356                 {
00357                     if (pos == 0) {
00358                         currentOption = menuOptions[menuOptionsSize - 1];
00359                     } else {
00360                         currentOption = menuOptions[--pos];
00361                     }
00362                 }
00363                 else if (command == COMMAND_RIGHT)
00364                 {
00365                     if (pos == (menuOptionsSize - 1)) {
00366                         currentOption = menuOptions[0];
00367                     } else {
00368                         currentOption = menuOptions[++pos];
00369                     }
00370                 }
00371             }
00372             // scroll through menu settings
00373             else if (command == COMMAND_UP || command == COMMAND_DOWN)
00374             {
00375                 // the LED state has changed
00376                 if (currentOption == MENU_LED_STATE) {
00377                     config.ledLit = !config.ledLit;
00378                     config.ledChanged = true;
00379 
00380                     program1();
00381                 }
00382                 // the LED value has changed
00383                 else if (currentOption == MENU_LED_VALUE) {
00384                     if (command == COMMAND_UP || command == COMMAND_DOWN) 
00385                     {
00386                         program2();
00387         
00388                     } 
00389 
00390                 }
00391             }
00392         }
00393         else {
00394              // enter in config menu
00395             if (command == COMMAND_SELECT)
00396             {
00397                 lcd.cls();
00398                 lcd.locate(0, 0);
00399                 lcd.printf("MENU CONFIG");
00400 
00401                 isInConfig = true;
00402                 currentOption = MENU_LED_STATE;
00403             }
00404         }
00405 
00406         // a new command has been triggered
00407         // browse through options
00408         if (currentOption != lastOption)
00409         {
00410             // clear second row
00411             clearLCDSecondRow();
00412 
00413             // LED state
00414             if (currentOption == MENU_LED_STATE)
00415             {
00416                 printLedStateConfig();
00417             }
00418             // LED value
00419             else if (currentOption == MENU_LED_VALUE)
00420             {
00421                 printLedValueConfig();
00422             }
00423         }
00424     }
00425 }
00426 
00427 
00428 int main()
00429 {
00430     initialize();
00431 
00432     while(1)
00433     {
00434         // keyboard routines
00435         readKeyboard();
00436         //printKeyboardCommandOnLCD();
00437 
00438         // config menu routines
00439         handleConfigMenu();
00440 
00441 
00442         // reset everything
00443         resetKeyboardCommand();
00444         resetMenuOption();
00445     }
00446 }