Rotary Encoder with a button assistance (inputpullup)

I have a Rotary Encoder code. It complies on my Arduino IDE with a Raspberry Pi Pico and Arduino Micro board. I need to add one button to this and I am having problems adding one button function. I am new to Arduino code. l am more familiar with G-Code. But, I have successfully unloaded an adapter code and got it to work. Below is the Rotary Encoder code I have for my project. If you could help me that would be great. I need input pullups added to my code as well. I added some button function code to the sketch from the forums, but I was unable to get it going properly.
I need to know what to add and where in the void set-up and void loop a button needs to be placed in order to work. If you could add the parts to the code, I would greatly appreciate it. And then instruct me on where to use this. It is simple and probably very easy to solve. I would just like to learn how. And what libraries I may need. I plan on getting a Arduino Micro for this project due to me using a Rotary Encoder because I will use the interrupt pins. Thank you for having such awesome boards, IDE and support.

Below is the rotary encoder code I have without any button function added.

// Define rotary encoder pins
#define ENC_A 2
#define ENC_B 3

unsigned long _lastIncReadTime = micros();
unsigned long _lastDecReadTime = micros();
int _pauseLength = 25000;
int _fastIncrement = 10;

volatile int counter = 0;

void setup() {

// Set encoder pins and attach interrupts
pinMode(ENC_A, INPUT_PULLUP);
pinMode(ENC_B, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ENC_A), read_encoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(ENC_B), read_encoder, CHANGE);

// Start the serial monitor to show output
Serial.begin(115200);
}

void loop() {
static int lastCounter = 0;

// If count has changed print the new value to serial
if(counter != lastCounter){
Serial.println(counter);
lastCounter = counter;
}
}

void read_encoder() {
// Encoder interrupt routine for both pins. Updates counter
// if they are valid and have rotated a full indent

static uint8_t old_AB = 3; // Lookup table index
static int8_t encval = 0; // Encoder value
static const int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0}; // Lookup table

old_AB <<=2; // Remember previous state

if (digitalRead(ENC_A)) old_AB |= 0x02; // Add current state of pin A
if (digitalRead(ENC_B)) old_AB |= 0x01; // Add current state of pin B

encval += enc_states[( old_AB & 0x0f )];

// Update counter if encoder has rotated a full indent, that is at least 4 steps
if( encval > 3 ) { // Four steps forward
int changevalue = 1;
if((micros() - _lastIncReadTime) < _pauseLength) {
changevalue = _fastIncrement * changevalue;
}
_lastIncReadTime = micros();
counter = counter + changevalue; // Update counter
encval = 0;
}
else if( encval < -3 ) { // Four steps backward
int changevalue = -1;
if((micros() - _lastDecReadTime) < _pauseLength) {
changevalue = _fastIncrement * changevalue;
}
_lastDecReadTime = micros();
counter = counter + changevalue; // Update counter
encval = 0;
}
}

Welcome to the forum

Please post your sketch, using code tags when you do. This prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Thanks for the information. I am new to these forums. I appreciate any help.

Your topic does not indicate a problem with IDE 1.x and therefore has been moved to a more suitable location on the forum.

Here you go:

// Define rotary encoder pins
#define ENC_A 2
#define ENC_B 3
#define BUTTON_PIN 4

unsigned long _lastIncReadTime = micros();
unsigned long _lastDecReadTime = micros();
int _pauseLength = 25000;
int _fastIncrement = 10;

volatile int counter = 0;

void setup() {
  // Button is connected between BUTTON_PIN and Gnd
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  // Set encoder pins and attach interrupts
  pinMode(ENC_A, INPUT_PULLUP);
  pinMode(ENC_B, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(ENC_A), read_encoder, CHANGE);
  attachInterrupt(digitalPinToInterrupt(ENC_B), read_encoder, CHANGE);

  // Start the serial monitor to show output
  Serial.begin(115200);
}

void loop() {
  static int lastCounter = 0;

  // If count has changed print the new value to serial
  if (counter != lastCounter) {
    Serial.println(counter);
    lastCounter = counter;
  }

  // Read the button, not pressed = HIGH, pressed = LOW
  int buttonState = digitalRead(BUTTON_PIN);
  // You may want to consider button debounce (Google it)
  if (buttonState == HIGH)
  {
    // code for button not pressed goes here
  }
  else
  {
    // code for button pressed goes here
  }
}

void read_encoder() {
  // Encoder interrupt routine for both pins. Updates counter
  // if they are valid and have rotated a full indent

  static uint8_t old_AB = 3; // Lookup table index
  static int8_t encval = 0; // Encoder value
  static const int8_t enc_states[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0}; // Lookup table

  old_AB <<= 2; // Remember previous state

  if (digitalRead(ENC_A)) old_AB |= 0x02; // Add current state of pin A
  if (digitalRead(ENC_B)) old_AB |= 0x01; // Add current state of pin B

  encval += enc_states[( old_AB & 0x0f )];

  // Update counter if encoder has rotated a full indent, that is at least 4 steps
  if ( encval > 3 ) { // Four steps forward
    int changevalue = 1;
    if ((micros() - _lastIncReadTime) < _pauseLength) {
      changevalue = _fastIncrement * changevalue;
    }
    _lastIncReadTime = micros();
    counter = counter + changevalue; // Update counter
    encval = 0;
  }
  else if ( encval < -3 ) { // Four steps backward
    int changevalue = -1;
    if ((micros() - _lastDecReadTime) < _pauseLength) {
      changevalue = _fastIncrement * changevalue;
    }
    _lastDecReadTime = micros();
    counter = counter + changevalue; // Update counter
    encval = 0;
  }
}

You should also fix these warnings I see when compiling your code. I've edited the warnings to remove irrelevant long paths.

deleteme2.ino:66:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if ((micros() - _lastIncReadTime) < _pauseLength) {
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
deleteme2.ino:75:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if ((micros() - _lastDecReadTime) < _pauseLength) {
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~

Thank you so much for taking the time to help. I greatly appreciate it.

You are trying to change a static variable. I am wrong. see post #8

Nothing wrong with changing a static variable. Are you confusing static with const ?

Yes. Sorry.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.