Help with encoder.h library

GeoffAndreychuk

New member
Hello everyone, I am trying to figure out how I can reattach the interrupt for Paul Stoffregen's encoder.h library, hopefully this request can be brought to his attention somehow since more than likely he will know since he wrote the library etc.

Background on the project. I am trying to add a second interrupt onto a pin in my sketch so that I am able to use the second interrupt to wake up my Arduino after being asleep when I turn the encoder that I am already using in my project to make various selections from user input. Ultimately I would like to NOT have to add an additional connection/button/switch etc so that I am able to get everything up and running again from sleep mode.

Currently the program I have does run (100% properly before I tried to add the sleep mode to it), however with the way it is currently written, I can attach the second interrupt to the pin, put everything to sleep, get it to wake up from sleep, etc. but then I detach the interrupts that are assigned to pin2 for the encoder, the second pin in use with the encoder is pin3, I end up now getting the encoder inputs going through the items backwards (counterclockwise when turning the encoder clockwise),

Hence why I want to try and figure out how I can add the interrupt back from the encoder.h library.


Thanks

Geoff
 
You should post your program so people can see what you are trying to do. But I think I understand and I think your best bet would be to connect another change interrupt capable pin to pin 2 ( pin 2 and pin x are both connected together and to the encoder ) and use that for your sleep interrupt.
 
Here is a quick sketch I have been using to try to understand what all I was doing so I didnt cause any additional issues with my actual program.

Code:
/**
 * a test to see if I can attach to interrupts to a pin.  using the encoderbutton.h and the avr/sleep.h libraries.  Encoderbutton.h lib is a wrapper for the encoder.h library by  Paul Stoffregen
 */
#include <EncoderButton.h>

#include <avr/sleep.h>//this AVR library contains the methods that controls the sleep modes


/**
 * Instatiate an EncoderButton.
 * For Arduino Uno, the hardware interrupts are pins 2 & 3
 * For Teensy, you can use any digital pin.
 * Probably better to pick a more meaningful name than 'eb1'...
 * Encoder+button:
 * EncoderButton(byte encoderPin1, byte encoderPin2, byte switchPin);
 * Encoder only:
 * EncoderButton(byte encoderPin1, byte encoderPin2);
 * Button only:
 * EncoderButton(byte switchPin);
 */
EncoderButton eb1(2, 3, 4);
#define interruptPin 2 //Pin we are going to use to wake up the Arduino

/**
 * A function to handle the 'clicked' event
 * Can be called anything but requires EncoderButton& 
 * as its only parameter.
 * I tend to prefix with 'on' and suffix with the 
 * event type.
 */
void onEb1Clicked(EncoderButton& eb) {
  Serial.print("eb1 clickCount: ");
  Serial.println(eb.clickCount());
}

/**
 * A function to handle the 'encoder' event
 */
void onEb1Encoder(EncoderButton& eb) {
  Serial.print("eb1 incremented by: ");
  Serial.println(eb.increment());
  Serial.print("eb1 position is: ");
  Serial.println(eb.position());
}

void onEb1Idle(EncoderButton& eb)
{
   Going_To_Sleep();
}

void Going_To_Sleep(){
    sleep_enable();//Enabling sleep mode
    attachInterrupt(digitalPinToInterrupt(interruptPin), wakeUp, CHANGE);//attaching a interrupt to pin d2
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting the sleep mode, in our case full sleep
    digitalWrite(LED_BUILTIN,LOW);//turning LED off
    delay(1000); //wait a second to allow the led to be turned off before going to sleep
    sleep_cpu();//activating sleep mode
    Serial.println("just woke up!");//next line of code executed after the interrupt 
    digitalWrite(LED_BUILTIN,HIGH);//turning LED on
}

void wakeUp(){
  Serial.println("Interrrupt Fired");//Print message to serial monitor
   sleep_disable();//Disable sleep mode
  detachInterrupt(digitalPinToInterrupt(interruptPin)); //Removes the interrupt from pin 2;
}


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(500);
  pinMode(LED_BUILTIN,OUTPUT);//We use the led on pin 13 to indecate when Arduino is A sleep
  digitalWrite(LED_BUILTIN,HIGH);//turning LED on
  Serial.println("multiple interrupt test");



  //Link the event(s) to your function
  eb1.setClickHandler(onEb1Clicked);
  eb1.setEncoderHandler(onEb1Encoder);  
  eb1.setIdleTimeout(45000);
  eb1.setIdleHandler(onEb1Idle);

}

void loop() {
  // put your main code here, to run repeatedly:
  // You must call update() for every defined EncoderButton.
  // This will update the state of the encoder & button and 
  // fire the appropriat events.
  eb1.update();
}
 
Back
Top