#define ENCODER_CLK 2
#define ENCODER_DT 3
#define ENCODER_BTN 4
#define pinBuzzer 11
#define pinRelay 5
int pulsa = 0;
void setup() {
Serial.begin(115200);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_BTN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(pinRelay, OUTPUT);
}
int lastClk = HIGH;
boolean btnPressed = false;
void loop() {
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
// There was a change on the CLK pin
lastClk = newClk;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
Serial.println(pulsa);
if (pulsa >= 20 && btnPressed == false){
tone(pinBuzzer, 1000);
digitalWrite(pinRelay, HIGH);
}
pulsa+=2;
}
if (newClk == LOW && dtValue == LOW) {
Serial.println(pulsa);
btnPressed = false;
pulsa-=2;
}
}
if (digitalRead(ENCODER_BTN) == LOW) {
digitalWrite(LED_BUILTIN, HIGH);
btnPressed = true;
noTone(pinBuzzer);
digitalWrite(pinRelay, LOW);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
}