int relayPin = 12; // ПИН ДЛЯ УПРАВЛЕНИЯ РЕЛЕ (был ledPin)
int sensorPin = 4;
int sensorValue;
int lastTiltState = HIGH; // the previous reading from the tilt sensor
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(sensorPin, INPUT_PULLUP); // Используем встроенный подтягивающий резистор
pinMode(relayPin, OUTPUT);
}
void loop() {
sensorValue = digitalRead(sensorPin);
if ((millis() - lastDebounceTime) > debounceDelay) {
// Если прошло достаточно времени после последнего изменения состояния
if (sensorValue != lastTiltState) {
// Состояние изменилось -> обновляем его и запоминаем время
lastTiltState = sensorValue;
lastDebounceTime = millis();
}
}
digitalWrite(relayPin, lastTiltState);
}