Day4 01-debounce
2024. 2. 4. 15:41ㆍarduino
- debounce를 이용한 LED제어
const int ledPin = 3;
const int buttonPin = 2;
int ledState = LOW;
int buttonState = HIGH;
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(ledPin, ledState);
}
void loop() {
// put your main code here, to run repeatedly:
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis()-lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
ledState = !ledState;
}
}
}
digitalWrite(ledPin, ledState);
lastButtonState = reading;
}
'arduino' 카테고리의 다른 글
Day4 03-DCmotor (0) | 2024.02.04 |
---|---|
Day4 02-encoder (0) | 2024.02.04 |
Day3 01-dht11 (0) | 2024.02.04 |
Day2 06_Debounce (0) | 2024.01.18 |
Day2 05_IR (0) | 2024.01.18 |