//No interruptions,no use switch pin
const int CLKpin = 4;
const int DTpin = 5;
//const int SWpin = 6;
int clk=0;
int dt=0;
//int sw=0;
int prevclk=0;
//int prevdt=0;
//int prevsw=0;
void readCLK(){
prevclk=clk;
clk=digitalRead(CLKpin);
}
void readDT(){
//prevdt=dt;
dt=digitalRead(DTpin);
}
//void readSW(){prevsw=sw;sw=digitalRead(SWpin);}
int rotaryPosition = 0;
//KY-040
int rotaryType=30;
long rotaryBaudMinSuccesfullTested=57600;
void initRotaryEncoder(){
pinMode (CLKpin,INPUT);
pinMode (DTpin,INPUT);
}
void resetRotaryPosition(){
rotaryPosition = 0;
readCLK();
Serial.println("Position Reseted to 0");
}
void detectAndRefreshRotaryPosition(){
readCLK();
if (clk != prevclk){
readDT();
if (dt != clk){
rotaryPosition ++;
Serial.print ("Right to :");
}
else {
rotaryPosition--;
Serial.print("Left to: ");
}
rotaryPosition%=rotaryType;
Serial.println(rotaryPosition);
}
}
void setup() {
initRotaryEncoder();
Serial.begin (rotaryBaudMinSuccesfullTested);
resetRotaryPosition();
}
void loop() {
detectAndRefreshRotaryPosition();
}
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου