Input Button Auto Repeat

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
arctic_eddie
 
Posts: 233
Joined: Tue Feb 28, 2012 6:01 pm

Input Button Auto Repeat

Post by arctic_eddie »

I've been working on a stable platform for aerial photography. This system needs button inputs to trim roll and pitch angles. An auto repeat function on the trim up and down buttons was desired. After numerous attempts, this one worked.

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

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”