#include <Arduino.h>
int relaisState;
unsigned long previousMillis = 0;
const long timeout = 2000;
const int signalInput = 2;
const int signalOutput = 3;
enum states
{
WATCH,
WAIT
};
unsigned int state;
void setup()
{
state = WATCH;
pinMode(signalInput, INPUT_PULLUP);
pinMode(signalOutput, OUTPUT);
relaisState = digitalRead(signalInput);
Serial.begin(9600);
Serial.println("Start Relais Timer");
}
void loop()
{
unsigned long currentMillis = millis();
switch (state)
{
case WATCH:
if (digitalRead(signalInput) != relaisState)
{
previousMillis = currentMillis;
state = WAIT;
Serial.println("signal change detect");
}
break;
case WAIT:
if (digitalRead(signalInput) == relaisState)
{
state = WATCH;
Serial.println("fall back to watch mode");
} else
if (currentMillis - previousMillis >= timeout)
{
relaisState = !relaisState;
Serial.print("set new relais state:");
Serial.println(relaisState, DEC);
}
break;
default:
break;
}
digitalWrite(signalOutput, relaisState);
}