Code: Select all
/*
Button auto repeat by John Pearson
This test requires that the button use an external debounce circuit using two resistors, one diode, and one capacitor.
The circuit used for this test was designed to work as an input to a 74LS132 gate. For this reason, the resistor
values are low. R is 1K0 and C is 4u7. The diode is a 1N914/4148. The circuit can be found at:
http://www.labbookpages.co.uk/electronics/debounce.html
*/
// Define constants
#define BUTTONPIN 6 // Use pin D6 for this test
#define AUTOREPEATDELAY 600 // How long to wait before button is in autorepeat
#define AUTOREPEATRATE 75 // How long to wait before repeating button action
// Define global variables
long buttontime = 0; // Button auto repeat timer
int counter = 0; // Something to show action
void setup()
{
Serial.begin(9600); // Start the monitor
Serial.println( "Start Auto Repeat Test" );
pinMode( BUTTONPIN, INPUT ); // Initialize input pin
}
void loop()
{
// Loop until button activity
while( digitalRead( BUTTONPIN ) == LOW ) // Button is down
{
buttontime = millis(); // Start timer
counter++; // Do something first time
Serial.print( "Counter = " );
Serial.println( counter );
while( digitalRead( BUTTONPIN ) == LOW ) // Button is still down
{
if( millis() - buttontime > AUTOREPEATDELAY ) // Check timer
{
counter++; // Do something again rapidly
Serial.print( "Counter = " );
Serial.println( counter );
delay( AUTOREPEATRATE ); // Auto repeat at this rate via delay
} // End if
} // End while
} // End while
} // End loop