Arduino

Hardware Debounce 하드웨어 디바운스

makeSomething 2018. 12. 26. 11:52

74HC14 칩으로 하드웨어 디바운스 테스트 해봅니다.

왼쪽에 있는 칩이 74HC14 로 Schmitt Trigger라고 합니다.

쉬미트 트리거 칩 없이 아래 코드 실행하면 가끔식 미스가 나거나 제대로 안될 때가 있습니다. 그래서 delay도 조정해 보고 하는데 이건 기계식 접점이 가지고 있는 한계라서요.

아래와 같은 셋업이면 코드도 간단해지고 편하긴 합니다만 칩도 필요하고 회로 구성이 조금은 복잡해지네요.

하드웨어적으로 해결하면 아래와 같이 간단한 코드로 버튼 문제가 해결 됩니다.

const int buttonPin = 7;     
const int ledPin =  8;
 
boolean lastButton = LOW; 
boolean ledOn = false;
 
void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}
 
void loop() {
 
  if (digitalRead(buttonPin) == HIGH && lastButton == LOW)
  {
    ledOn = !ledOn;
    lastButton = HIGH;
  }
  else
  {
    lastButton = digitalRead(buttonPin);
  }
 digitalWrite(ledPin, ledOn);
}