2016-07-05 21:45:58 +00:00
|
|
|
#ifndef SCANDELAY_H
|
|
|
|
#define SCANDELAY_H
|
2016-07-04 04:25:49 +00:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2016-07-05 21:45:58 +00:00
|
|
|
/* ScanDelay() delay's scan to give switches time to debounce.
|
2016-07-04 04:25:49 +00:00
|
|
|
|
|
|
|
DELAY_MICROSECONDS explained
|
|
|
|
----------------------------
|
|
|
|
A keyboard with a faster scan rate responds faster.
|
2016-07-15 05:15:38 +00:00
|
|
|
Follow these step to tune DELAY_MICROSECONDS for maximum scan rate for a given SAMPLE_COUNT_MACRO:
|
2016-07-05 21:45:58 +00:00
|
|
|
Intantiate DELAY_MICROSECONDS in your sketch:
|
|
|
|
ScanDelay scanDelay(1000);
|
2016-07-04 04:25:49 +00:00
|
|
|
Add this to the sketch's loop() function:
|
|
|
|
debug.print_microseconds_per_scan();
|
|
|
|
Compile and load the sketch into the microcontroller; microseconds_per_scan is printed every second.
|
|
|
|
Adjust the value of DELAY_MICROSECONDS and repeat until:
|
2016-07-15 05:15:38 +00:00
|
|
|
debug.print_microseconds_per_scan() <= DEBOUNCE_TIME / SAMPLE_COUNT_MACRO
|
2016-07-04 04:25:49 +00:00
|
|
|
|
|
|
|
DEBOUNCE_TIME can be obtained from the switch's datasheet. Some switch bounce times are:
|
|
|
|
Cherry MX specifies 5msec bounce time http://www.cherrycorp.com/english/switches/key/mx.htm
|
|
|
|
hasu measured Cherry MX bounce times .3ms to 1.4ms http://geekhack.org/index.php?topic=42385.0
|
2016-07-18 02:26:00 +00:00
|
|
|
Tactile switch MJTP series bounce 10 ms http://www.apem.com/files/apem/brochures/MJTP_6MM.pdf
|
2016-07-04 04:25:49 +00:00
|
|
|
|
2016-07-05 21:45:58 +00:00
|
|
|
The largest allowable DELAY_MICROSECONDS is 65535 (65.535 ms).
|
|
|
|
|
2016-07-04 04:25:49 +00:00
|
|
|
Avoid sampling the switch input at a rate synchronous to events in the environment
|
|
|
|
that might create periodic EMI. For instance, 50 and 60 Hz.
|
|
|
|
|
2016-07-05 21:45:58 +00:00
|
|
|
Polling I2C may slow the scan rate enough so that no additional delay is needed
|
|
|
|
and scanDelay(0) can be omitted from the loop().
|
2016-07-04 04:25:49 +00:00
|
|
|
|
2016-07-05 21:45:58 +00:00
|
|
|
Could put delay directly in loop(), but then it's harder to finding this documentation.
|
|
|
|
delay(5);
|
2016-07-04 04:25:49 +00:00
|
|
|
*/
|
2016-07-05 21:45:58 +00:00
|
|
|
class ScanDelay
|
2016-07-04 04:25:49 +00:00
|
|
|
{
|
|
|
|
private:
|
2016-07-05 21:45:58 +00:00
|
|
|
const unsigned int DELAY_MICROSECONDS; //delay each loop() for debouncing
|
|
|
|
public:
|
|
|
|
ScanDelay(const unsigned int DELAY_MICROSECONDS): DELAY_MICROSECONDS(DELAY_MICROSECONDS) {}
|
2016-07-04 04:25:49 +00:00
|
|
|
void delay();
|
|
|
|
};
|
|
|
|
#endif
|