const int LED_PIN = 8;
const int RELAY_PIN = 9;
const int interruptPin = 3;
const int CLK_PIN = 2;
const int DT_PIN = 3;
int pressStatus = 0;
void setup() {
// initialize digital pin LED_BUILTIN as an output.
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), blinkOK, CHANGE);
attachInterrupt(digitalPinToInterrupt(CLK_PIN), timeADJ, FALLING);
}
// the loop function runs over and over again forever
void loop() {
//digitalWrite(LED_PIN, HIGH); // turn the LED on (HIGH is the voltage level)
//delay(1000); // wait for a second
//digitalWrite(LED_PIN, LOW); // turn the LED off by making the voltage LOW
//delay(1000); // wait for a second
}
void blinkOK()
{
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 200)
{
//-----------------------------------------------------
if (pressStatus == 0)
{
pressStatus = 1;
digitalWrite(LED_PIN, HIGH);
digitalWrite(RELAY_PIN, HIGH);
}
else
{
pressStatus = 0;
digitalWrite(LED_PIN, LOW);
digitalWrite(RELAY_PIN, LOW);
}
//-----------------------------------------------------
}
last_interrupt_time = interrupt_time;
}
int lastClk = HIGH;
void timeADJ()
{
int newClk = digitalRead(CLK_PIN);
if (newClk != lastClk) {
// There was a change on the CLK pin
lastClk = newClk;
int dtValue = digitalRead(DT_PIN);
if (newClk == LOW && dtValue == HIGH) {
Serial.println("Rotated clockwise ⏩");
digitalWrite(LED_PIN, HIGH);
}
if (newClk == LOW && dtValue == LOW) {
Serial.println("Rotated counterclockwise ⏪");
digitalWrite(LED_PIN, LOW);
}
}
}