setRange()

Description

Set a upper bound value of encoder counter. When the counter value increase up to the upper bound, then back to zero, will trigger one interrupt event. Otherwise, When the counter value decrease to the lower bound, then back to max value, will also trigger one interrupt event.

Note: this function is available after the “Coding 105” and just works under Pulse/DIR, CW/CCW and A/B Pulse mode.

Syntax


Enc0.setRange(val)
Enc0.setRange(val, condition)

Enc1.setRange(val)
Enc1.setRange(val, condition)

Enc2.setRange(val)
Enc2.setRange(val, condition)

Enc3.setRange(val)
Enc3.setRange(val, condition)

Parameters

Enc0、Enc1、Enc2、Enc3:Corresponding to ENC0, ENC1, ENC2 and ENC3 encoder interfaces.

val:upper bound of encoder counter. The range is 1 ~ 4,294,967,295 (2^32 – 1). If the value is 199, and the counter will be back to 0 when counter value increase over 199, or back to 199 after counter value decrease to 0.

condition:true or false。Set true to enable interrupt event, and false to disable interrupt event. (Default is true)

Returns

None

Example

#include "Encoder.h"

volatile int ovnumber = 0;
volatile int uvnumber = 0;
void encoder_callback(int flag) {
  if(flag == INTR_OVERFLOW) // overflow event
    ovnumber++;
  else if(flag == INTR_UNDERFLOW) // underflow event
    uvnumber++;
}

void setup() {
  Serial.begin(9600);
  Enc0.begin(MODE_AB_PHASE);
  Enc0.setRange(199); // Set upper bound value is 199
  Enc0.attachInterrupt(encoder_callback); // set callback function and enable interrupt
}

void loop() {
  Serial.print("count = ");
  Serial.print(Enc0.read()); // read the counter value of encoder
  Serial.print(" ");
  Serial.print("overflow = ");
  Serial.print(ovnumber); // print the times of overflow event
  Serial.print(" ");
  Serial.print("underflow = ");
  Serial.println(uvnumber); // print the times of underflow event
  
  delay(100);
}

See also

setComparator()
setIndexReset()


Libraries Reference Home

The text of the 86Duino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.