Answers for "Sketch for rotatory encoder"

0

Sketch for rotatory encoder

#define encoderOutA 2 // CLK pin of Rotary Enocoder
 #define encoderOutB 1 // DT pin of Rotary Enocoder
 
 int counter = 0; 
 int presentState;
 int previousState;  
 void setup() { 
   pinMode (encoderOutA,INPUT);
   pinMode (encoderOutB,INPUT);
   
   Serial.begin (9600);
      previousState = digitalRead(encoderOutA);   // Get current state of the encoderOutA
 } 
 void loop() { 
   presentState = digitalRead(encoderOutA); 
      if (presentState != previousState)
      {     
          if (digitalRead(encoderOutB) != presentState) 
          { 
       counter ++;
     } 
     else {
       counter --;
     }
     Serial.print("Position: ");
     Serial.println(counter);
   } 
   previousState = presentState; // Replace previous state of the encoderOutA with the current state
 }
Posted by: Guest on May-15-2021

Browse Popular Code Answers by Language