Hotboards MX / Mbed 2 deprecated Hotboards_KeyPad_LoopCounter

Dependencies:   Hotboards_keypad mbed

Fork of LoopCounter by Roman Valencia

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* @file loopCounter
00002 || @version 1.1
00003 || @modified by Diego (http://hotboards.org)
00004 || @author Alexander Brevig
00005 || @contact alexanderbrevig@gmail.com
00006 ||
00007 || @description
00008 || | Demonstrates the effect of debounce time, just change the
00009 || | debounce time using the fucntion setDebounceTime
00010 
00011 */
00012 #include "mbed.h"
00013 #include "Hotboards_keypad.h"
00014 
00015 // Defines the keys array with it's respective number of rows & cols,
00016 // and with the value of each key
00017 char keys[ 4 ][ 4 ] =
00018 {
00019     { '1' , '2' , '3' , 'A' },
00020     { '4' , '5' , '6' , 'B' },
00021     { '7' , '8' , '9' , 'C' },
00022     { '*' , '0' , '#' , 'D' }
00023 };
00024 
00025 // Defines the pins connected to the rows
00026 DigitalInOut rowPins[ 4 ] = { PA_6 , PA_7 , PB_6 , PC_7 };
00027 // Defines the pins connected to the cols
00028 DigitalInOut colPins[ 4 ] = { PA_8 , PB_10 , PB_4 , PB_5 };
00029 
00030 // Creates a new keyboard with the values entered before
00031 Keypad kpd( makeKeymap( keys ) , rowPins , colPins , 4 , 4 );
00032 
00033 // Configures the serial port
00034 Serial pc( USBTX , USBRX );
00035 
00036 //
00037 int loopCount = 0;
00038 int timer_t = 0;
00039 
00040 // Configures a timer
00041 Timer t;
00042 
00043 int main()
00044 {
00045     // Starts the timer
00046     t.start( );
00047     pc.printf( "Press any key: ");
00048     
00049 
00050     // Try playing with different debounceTime settings to see how it affects
00051     // the number of times per second your loop will run. The library prevents
00052     // setting it to anything below 1 millisecond.
00053     kpd.setDebounceTime ( 100 ); // setDebouncetime( ms );
00054     while(1)
00055     {
00056         // Poll the keypad to look for any activation
00057         char key = kpd.getKey( );
00058         
00059         // Reports the number of times through the loop in 1 second. This will give
00060         // you a relative idea of just how much the debounceTime has changed the
00061         // speed of your code. If you set a high debounceTime your loopCount will
00062         // look good but your keypresses will start to feel sluggish.        
00063         if(( t.read_ms( ) - timer_t ) > 1000 )
00064         {
00065             pc.printf( "Your loop code ran " );
00066             pc.printf( "%d" , loopCount );
00067             pc.printf( " times over the last second" );
00068             loopCount = 0;
00069             pc.printf( "\n\r" );
00070             timer_t = t.read_ms( );
00071         }
00072         loopCount ++;
00073         
00074         // If any key was pressed
00075         if( key )
00076         {
00077             // Display the key pressed on serial port
00078             pc.printf( "%c" , key );
00079         }
00080     }
00081 }