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