İnternette şöyle bir kod buldum encoder değeri ölçmek için ne kadar doğru bilmiyorum ama kod şöyle:
#Define LED 13
#Define sensor 12
İnt encoderPin1 = 2;
İnt encoderPin2 = 3;
Volatile int lastEncoded = 0;
Volatile long encoderValue = 0;
Long lastencoderValue = 0;
İnt lastMSB = 0;
İnt lastLSB = 0;
İnt ledvalue = 0;
Void setup() {
Serial. Begin (9600);
PinMode(encoderPin1, INPUT_PULLUP);
PinMode(encoderPin2, INPUT_PULLUP);
DigitalWrite(encoderPin1, High); //turn pullup resistor on
DigitalWrite(encoderPin2, High); //turn pullup resistor on
//Call updateEncoder() when any high/low changed seen
//On interrupt 0 (pin 2), or interrupt 1 (pin 3)
AttachInterrupt(0, updateEncoder, CHANGE);
AttachInterrupt(1, updateEncoder, CHANGE);
/////////////////////////////////
PinMode(LED, OUTPUT);
PinMode(sensor, INPUT_PULLUP);
DigitalWrite(LED, High);
// DigitalWrite(button, High);
}
Void loop(){
İf (encoderValue >= 100)
DigitalWrite(LED, High);
Else DigitalWrite(LED, Low);
Ledvalue = DigitalRead(LED);
//Do stuff here
Serial. Print("Enkoder");
Serial. Print(' ');
Serial. Print("Sensor");
Serial. Println(' ');
Serial. Print(encoderValue);
Serial. Print('\t');
Serial. Print(ledvalue);
Serial. Println('\t');
Delay(250); //just here to Slow down the output, and show it will work even during a delay
}
Void updateEncoder(){
Int MSB = DigitalRead(encoderPin1); //MSB = most significant Bit
Int LSB = DigitalRead(encoderPin2); //LSB = least significant Bit
Int encoded = (MSB << 1) |LSB; //converting the 2 pin Value to Single number
Int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded Value
If(sum == 0B1101 || sum == 0B0100 || sum == 0B0010 || sum == 0B1011) encoderValue ++;
If(sum == 0B1110 || sum == 0B0111 || sum == 0B0001 || sum == 0B1000) encoderValue --;
If (ledvalue == 1) encoderValue = 0;
Else if(ledvalue == 0)
LastEncoded = encoded; //Store this Value for Next time
//LastEncoded = encoded;
}
Bunu çalıştırdığımda sağa sola döndürdükçe encoder kısmı 0 la -1 arası değişiyor sanırım normalde çok daha fazla değer göstermesi gerekiyor. Encoder mi sorunlu sence?