Permalink
Browse files

added noise filter to switch checks to deal with noise on lines

  • Loading branch information...
1 parent dbdad57 commit 63d3fbc2458f6c2736edd425c51daabee2d87741 @phooky committed Mar 2, 2014
Showing with 15 additions and 2 deletions.
  1. +15 −2 teensytype/teensytype.ino
@@ -95,7 +95,7 @@ enum {
SW_4 = 0x08
};
-uint8_t getSwitchState() {
+uint8_t getSwitchStateRaw() {
uint8_t state = 0;
if (digitalRead(pin_switch1) == HIGH) state |= SW_1;
if (digitalRead(pin_switch2) == HIGH) state |= SW_2;
@@ -104,6 +104,19 @@ uint8_t getSwitchState() {
return state;
}
+uint8_t getSwitchState() {
+ // Debounce switch state
+ uint8_t state = getSwitchStateRaw();
+ uint8_t agrees = 0;
+ while (agrees < 5) {
+ uint8_t candidate = getSwitchStateRaw();
+ if (candidate == state) { agrees++; }
+ else {
+ state = candidate; agrees = 0;
+ }
+ }
+}
+
/*
* Bank -> key mapping
* This chart gives the function of each bank/solenoid pair.
@@ -354,7 +367,7 @@ void delayBetween(uint8_t last, uint8_t next) {
boolean doCarriageReturn() {
const static uint8_t ARM_SW = 1<<2;
setMotor(MOTOR_ON);
- delay(50);
+ delay(150);
uint32_t i;
for (i = 0; i < 160000UL; i++) {
if ((getSwitchState() & ARM_SW) == 0) {

0 comments on commit 63d3fbc

Please sign in to comment.