//Senha Pré-definida como 12-13-12-11
#include "ansi.h"
#include <SoftwareSerial.h>
#include <Servo.h>
const int ENCODER_CLK = 3;
const int ENCODER_DT = 4;
int encoderPos = 0;
int encoderPos1 = 0;
int encoderPos2 = 0;
int encoderPos3 = 0;
int count = 0;
Servo servo1;
void setup() {
Serial.begin(9600);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
servo1.attach(6);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
}
int lastClk = HIGH;
void loop() {
if (count >= 3){
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
// Houve uma mudança no pino CLK
lastClk = newClk;
count++;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
encoderPos3 = encoderPos2;
encoderPos2 = encoderPos1;
encoderPos1 = encoderPos;
encoderPos++;
if (encoderPos == 21){
encoderPos = 1;
}
Serial.println("Girando no sentido horário ⏩");
Serial.print("Posicao: ");
Serial.println(encoderPos);
Serial.print("Sequencia: ");
Serial.print(encoderPos);
Serial.print(" ");
Serial.print(encoderPos1);
Serial.print(" ");
Serial.print(encoderPos2);
Serial.print(" ");
Serial.println(encoderPos3);
}
if (newClk == LOW && dtValue == LOW) {
encoderPos3 = encoderPos2;
encoderPos2 = encoderPos1;
encoderPos1 = encoderPos;
encoderPos--;
if (encoderPos == 0){
encoderPos = 20;
}
Serial.println("Girando no sentido anti-horário ⏪");
Serial.print("Posicao: ");
Serial.println(encoderPos);
Serial.print("Sequencia: ");
Serial.print(encoderPos);
Serial.print(" ");
Serial.print(encoderPos1);
Serial.print(" ");
Serial.print(encoderPos2);
Serial.print(" ");
Serial.println(encoderPos3);
}
}
}
else if (count == 2){
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
// Houve uma mudança no pino CLK
lastClk = newClk;
count++;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
encoderPos2 = encoderPos1;
encoderPos1 = encoderPos;
encoderPos++;
if (encoderPos == 21){
encoderPos = 1;
}
Serial.println("Girando no sentido horário ⏩");
Serial.print("Posicao: ");
Serial.println(encoderPos);
}
if (newClk == LOW && dtValue == LOW) {
encoderPos2 = encoderPos1;
encoderPos1 = encoderPos;
encoderPos--;
if (encoderPos == 0){
encoderPos = 20;
}
Serial.println("Girando no sentido anti-horário ⏪");
Serial.print("Posicao: ");
Serial.println(encoderPos);
}
}
}
else if (count == 1) {
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
// Houve uma mudança no pino CLK
lastClk = newClk;
count++;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
encoderPos1 = encoderPos;
encoderPos++;
if (encoderPos == 21){
encoderPos = 1;
}
Serial.println("Girando no sentido horário ⏩");
Serial.print("Posicao: ");
Serial.println(encoderPos);
}
if (newClk == LOW && dtValue == LOW) {
encoderPos1 = encoderPos;
encoderPos--;
if (encoderPos == 0){
encoderPos = 20;
}
Serial.println("Girando no sentido anti-horário ⏪");
Serial.print("Posicao: ");
Serial.println(encoderPos);
}
}
}
else if (count == 0){
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
lastClk = newClk;
count++;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
encoderPos++;
if (encoderPos == 21){
encoderPos = 1;
}
Serial.println("Girando no sentido horário ⏩");
Serial.print("Posicao: ");
Serial.println(encoderPos);
}
if (newClk == LOW && dtValue == LOW) {
encoderPos = 20;
Serial.println("Girando no sentido anti-horário ⏪");
Serial.print("Posicao: ");
Serial.println(encoderPos);
}
}
}
if (encoderPos == 12 && encoderPos1 == 13 && encoderPos2 == 12 && encoderPos3 && 11){
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
servo1.write(180);
}
else {
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
servo1.write(90);
}
}