| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Rotary Encoder Code

Page history last edited by David S 10 years, 9 months ago

/* Rotary encoder test code

 * Modified from http://www.circuitsathome.com/mcu/programming/reading-rotary-encoder-on-arduino by Oleg

 * Latest mod by Jessica Faruque 7/17/2013

 */

 

 

#define ENC_A 9 //these need to be digital input pins

#define ENC_B 10

 

void setup()

{

  /* Setup encoder pins as inputs */

  pinMode(ENC_A, INPUT_PULLUP);

  pinMode(ENC_B, INPUT_PULLUP);

 

  Serial.begin (9600);

  Serial.println("Start");

}

 

void loop()

{

  static unsigned int counter4x = 0;      //the SparkFun encoders jump by 4 states from detent to detent

  static unsigned int counter = 0;

  static unsigned int prevCounter = 0;

  int tmpdata;

  tmpdata = read_encoder();

  if( tmpdata) {

    counter4x += tmpdata;

    counter = counter4x/4;

    if (prevCounter != counter){

      Serial.print("Counter value: ");

      Serial.println(counter);

    }

    prevCounter = counter;

  }

}

 

/* returns change in encoder state (-1,0,1) */

int read_encoder()

{

  static int enc_states[] = {

    0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0  };

  static byte ABab = 0;

  ABab *= 4;                   //shift the old values over 2 bits

  ABab = ABab%16;      //keeps only bits 0-3

  ABab += 2*digitalRead(ENC_A)+digitalRead(ENC_B); //adds enc_a and enc_b values to bits 1 and 0

  return ( enc_states[ABab]);

 

 

}

 

Comments (0)

You don't have permission to comment on this page.