Demonstrates the effect of debounce time
Dependencies: Hotboards_keypad mbed
Fork of LoopCounter by
main.cpp@0:4b3d27b81086, 2016-03-08 (annotated)
- Committer:
- RomanValenciaP
- Date:
- Tue Mar 08 20:43:11 2016 +0000
- Revision:
- 0:4b3d27b81086
- Child:
- 1:0f32d584b715
first release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RomanValenciaP | 0:4b3d27b81086 | 1 | |
RomanValenciaP | 0:4b3d27b81086 | 2 | #include "mbed.h" |
RomanValenciaP | 0:4b3d27b81086 | 3 | #include "Hotboards_keypad.h" |
RomanValenciaP | 0:4b3d27b81086 | 4 | |
RomanValenciaP | 0:4b3d27b81086 | 5 | // Defines the keys array with it's respective number of rows & cols, |
RomanValenciaP | 0:4b3d27b81086 | 6 | // and with the value of each key |
RomanValenciaP | 0:4b3d27b81086 | 7 | char keys[ 4 ][ 4 ] = |
RomanValenciaP | 0:4b3d27b81086 | 8 | { |
RomanValenciaP | 0:4b3d27b81086 | 9 | { '1' , '2' , '3' , 'A' }, |
RomanValenciaP | 0:4b3d27b81086 | 10 | { '4' , '5' , '6' , 'B' }, |
RomanValenciaP | 0:4b3d27b81086 | 11 | { '7' , '8' , '9' , 'C' }, |
RomanValenciaP | 0:4b3d27b81086 | 12 | { '*' , '0' , '#' , 'D' } |
RomanValenciaP | 0:4b3d27b81086 | 13 | }; |
RomanValenciaP | 0:4b3d27b81086 | 14 | |
RomanValenciaP | 0:4b3d27b81086 | 15 | // Defines the pins connected to the rows |
RomanValenciaP | 0:4b3d27b81086 | 16 | DigitalInOut rowPins[ 4 ] = { PA_6 , PA_7 , PB_6 , PC_7 }; |
RomanValenciaP | 0:4b3d27b81086 | 17 | // Defines the pins connected to the cols |
RomanValenciaP | 0:4b3d27b81086 | 18 | DigitalInOut colPins[ 4 ] = { PA_8 , PB_10 , PB_4 , PB_5 }; |
RomanValenciaP | 0:4b3d27b81086 | 19 | |
RomanValenciaP | 0:4b3d27b81086 | 20 | // Creates a new keyboard with the values entered before |
RomanValenciaP | 0:4b3d27b81086 | 21 | Keypad kpd( makeKeymap( keys ) , rowPins , colPins , 4 , 4 ); |
RomanValenciaP | 0:4b3d27b81086 | 22 | |
RomanValenciaP | 0:4b3d27b81086 | 23 | // Configures the serial port |
RomanValenciaP | 0:4b3d27b81086 | 24 | Serial pc( USBTX , USBRX ); |
RomanValenciaP | 0:4b3d27b81086 | 25 | |
RomanValenciaP | 0:4b3d27b81086 | 26 | // |
RomanValenciaP | 0:4b3d27b81086 | 27 | int loopCount = 0; |
RomanValenciaP | 0:4b3d27b81086 | 28 | int timer_t = 0; |
RomanValenciaP | 0:4b3d27b81086 | 29 | |
RomanValenciaP | 0:4b3d27b81086 | 30 | // Configures a timer |
RomanValenciaP | 0:4b3d27b81086 | 31 | Timer t; |
RomanValenciaP | 0:4b3d27b81086 | 32 | |
RomanValenciaP | 0:4b3d27b81086 | 33 | int main() |
RomanValenciaP | 0:4b3d27b81086 | 34 | { |
RomanValenciaP | 0:4b3d27b81086 | 35 | // Starts the timer |
RomanValenciaP | 0:4b3d27b81086 | 36 | t.start( ); |
RomanValenciaP | 0:4b3d27b81086 | 37 | pc.printf( "Press any key: "); |
RomanValenciaP | 0:4b3d27b81086 | 38 | |
RomanValenciaP | 0:4b3d27b81086 | 39 | |
RomanValenciaP | 0:4b3d27b81086 | 40 | // Try playing with different debounceTime settings to see how it affects |
RomanValenciaP | 0:4b3d27b81086 | 41 | // the number of times per second your loop will run. The library prevents |
RomanValenciaP | 0:4b3d27b81086 | 42 | // setting it to anything below 1 millisecond. |
RomanValenciaP | 0:4b3d27b81086 | 43 | kpd.setDebounceTime ( 100 ); // setDebouncetime( ms ); |
RomanValenciaP | 0:4b3d27b81086 | 44 | while(1) |
RomanValenciaP | 0:4b3d27b81086 | 45 | { |
RomanValenciaP | 0:4b3d27b81086 | 46 | // Poll the keypad to look for any activation |
RomanValenciaP | 0:4b3d27b81086 | 47 | char key = kpd.getKey( ); |
RomanValenciaP | 0:4b3d27b81086 | 48 | |
RomanValenciaP | 0:4b3d27b81086 | 49 | // Reports the number of times through the loop in 1 second. This will give |
RomanValenciaP | 0:4b3d27b81086 | 50 | // you a relative idea of just how much the debounceTime has changed the |
RomanValenciaP | 0:4b3d27b81086 | 51 | // speed of your code. If you set a high debounceTime your loopCount will |
RomanValenciaP | 0:4b3d27b81086 | 52 | // look good but your keypresses will start to feel sluggish. |
RomanValenciaP | 0:4b3d27b81086 | 53 | if(( t.read_ms( ) - timer_t ) > 1000 ) |
RomanValenciaP | 0:4b3d27b81086 | 54 | { |
RomanValenciaP | 0:4b3d27b81086 | 55 | pc.printf( "Your loop code ran " ); |
RomanValenciaP | 0:4b3d27b81086 | 56 | pc.printf( "%d" , loopCount ); |
RomanValenciaP | 0:4b3d27b81086 | 57 | pc.printf( " times over the last second" ); |
RomanValenciaP | 0:4b3d27b81086 | 58 | loopCount = 0; |
RomanValenciaP | 0:4b3d27b81086 | 59 | pc.printf( "\n\r" ); |
RomanValenciaP | 0:4b3d27b81086 | 60 | timer_t = t.read_ms( ); |
RomanValenciaP | 0:4b3d27b81086 | 61 | } |
RomanValenciaP | 0:4b3d27b81086 | 62 | loopCount ++; |
RomanValenciaP | 0:4b3d27b81086 | 63 | |
RomanValenciaP | 0:4b3d27b81086 | 64 | // If any key was pressed |
RomanValenciaP | 0:4b3d27b81086 | 65 | if( key ) |
RomanValenciaP | 0:4b3d27b81086 | 66 | { |
RomanValenciaP | 0:4b3d27b81086 | 67 | // Display the key pressed on serial port |
RomanValenciaP | 0:4b3d27b81086 | 68 | pc.printf( "%c" , key ); |
RomanValenciaP | 0:4b3d27b81086 | 69 | } |
RomanValenciaP | 0:4b3d27b81086 | 70 | } |
RomanValenciaP | 0:4b3d27b81086 | 71 | } |