Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- byte cha = 18;
- byte chb = 19;
- long encoder_pos = 0;
- byte last_value = B00000000;
- byte counter = 0;
- byte cumulator = B00000000;
- void setup() {
- pinMode(cha, INPUT_PULLUP);
- pinMode(chb, INPUT_PULLUP);
- attachInterrupt(4, doEncoder, CHANGE);
- Serial.begin(2000000);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- Serial.println(encoder_pos);
- }
- void doEncoder()
- {
- // pp B11001001
- byte current_value = PIND & B00001100; // excludem din portu D biti nefolositi => B00001000
- if (last_value != current_value ) // B00001100 -> B00001000
- {
- cumulator <<= 2; // B00001100 => B00110000
- cumulator |= current_value >> 2; // 1. B00001000 => B00000010 , 2.B00110010
- if (cumulator == 75)
- {
- encoder_pos --;
- }
- if (cumulator == 30)
- {
- encoder_pos ++;
- }
- }
- last_value = current_value;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement