Вернуться на страницу библиотеки модулей
int tiltPin = 2;
int ledPin = 13;
int lastReading = HIGH;
int stableState = HIGH;
unsigned long lastChange = 0;
void setup() {
pinMode(tiltPin, INPUT_PULLUP); // LOW = tilted (contacts closed)
pinMode(ledPin, OUTPUT);
}
void loop() {
int reading = digitalRead(tiltPin);
if (reading != lastReading) {
lastChange = millis();
lastReading = reading;
}
if (millis() - lastChange > 50) { // 50 ms debounce
if (reading != stableState) {
stableState = reading;
digitalWrite(ledPin, stableState == LOW ? HIGH : LOW);
}
}
}
Рекомендуемые ссылки:
Arduino Tilt Switch Sensor KY-020 – Wiring & Code
KY-020 Tilt switch – SensorKit
How to Use Tilt Sensors on the Arduino
Using the Tilt Switch Module KY-020 with Arduino – Phipps Electronics
KeyesKY-020Tilt_Sensor_Instructions.pdf