Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Pin Change Interrupts on Metro Mini
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Pin Change Interrupts on Metro Mini

by cking0704 on Sun Mar 29, 2020 1:44 pm

I was asked to move this to a clean post. I’m using a Metro Mini in a datalogging light meter. The setup involves six TSL2591 breakouts multiplexed, a DS1307 RTC, an SD module, and an SSD1305 OLED 128x64 on hardware SPI. There are three buttons for interrupts.

Here’s the copied post:

I'm back. This time it's about the pin change interrupt I'm using. It keeps triggering off of the button press for ISR2. Anyone seen this behavior? Did I code it wrong?

Code: Select all | TOGGLE FULL SIZE
/*SOME LIBRARIES AND DECLARATIONS*/

const byte isr1Pin = 2;
const byte isr2Pin = 3;
const byte isr3Pin = 9;
unsigned long last_isr1_t = 0;
unsigned long last_isr2_t = 0;
unsigned long last_isr3_t = 0;
const byte SDPin = 10;
volatile bool wake = false;
volatile bool sleep = false;
volatile bool record = false;
bool wrote = false;
volatile bool make = false;
bool made = false;
volatile byte dmode = 6;
volatile bool relabel = false;
unsigned short int count = 0;


void setup() {
  cli();
  PCICR |= 0b00000001;
  PCMSK0 |= 0b00000010;
  sei();
  /*SOME MORE CODE*/
  pinMode(isr1Pin, INPUT_PULLUP);
  pinMode(isr2Pin, INPUT_PULLUP);
  pinMode(isr3Pin, INPUT);
  attachInterrupt(digitalPinToInterrupt(isr1Pin), ISR1, RISING);
  attachInterrupt(digitalPinToInterrupt(isr2Pin), ISR2, RISING);
}

/*SOME MORE CODE*/
//This ISR makes a new line of data in the CSV
void ISR1() {
  unsigned long isr1_t = millis();
  if (isr1_t - last_isr1_t > 500) {
    if (sleep == true){
      relabel = true;
      sleep = false;
      wake = true;
  }else{
      record = true;
  }
    last_isr1_t = isr1_t;
  }
}
//This ISR cycles the display mode (normally displays all six sensor readings, but you can focus on one sensor for faster reads)
void ISR2() {
  unsigned long isr2_t = millis();
  if (isr2_t - last_isr2_t > 500) {
    dmode++;
    if (sleep==true){
      dmode--;
      sleep = false;
      wake = true;
    }
    dmode = dmode % 7;
    relabel = true;
    last_isr2_t = isr2_t;
  }
}
//This ISR creates a new log file
ISR(PCINT0_vect) {
  unsigned long isr3_t = millis();
  if (isr3_t - last_isr3_t > 10000) {
    if (sleep==true){
      sleep = false;
      wake = true;
    }else{
      make = true;
    }
    last_isr3_t = isr3_t;
  }else{
    make = false;
  }
}
ISR(BADISR_vect){
}

cking0704
 
Posts: 19
Joined: Wed Mar 25, 2020 8:09 am

Re: Pin Change Interrupts on Metro Mini

by cking0704 on Sun Mar 29, 2020 8:34 pm

Here I think I'm answering my own question. Feel free to weigh in. INPUT vs INPUT_PULLUP. Without the pullup resistor, the PCINT pin is seeing small flutters and executing. Does that make sense? It seems to fix it, but I'd like to be sure that was it.

cking0704
 
Posts: 19
Joined: Wed Mar 25, 2020 8:09 am

Re: Pin Change Interrupts on Metro Mini

by cking0704 on Mon Mar 30, 2020 6:46 pm

Now that the PULLUP is activated, there’s no drifting ISR behavior. However, grounding the pin doesn’t always result in an interrupt.

cking0704
 
Posts: 19
Joined: Wed Mar 25, 2020 8:09 am

Re: Pin Change Interrupts on Metro Mini

by adafruit_support_carter on Mon Mar 30, 2020 8:26 pm

INPUT vs INPUT_PULLUP. Without the pullup resistor, the PCINT pin is seeing small flutters and executing. Does that make sense?

Very much so. Otherwise the input would be floating and responding to essentially noise.

However, grounding the pin doesn’t always result in an interrupt.

Maybe change to FALLING instead of RISING?

adafruit_support_carter
 
Posts: 17179
Joined: Tue Nov 29, 2016 2:45 pm

Re: Pin Change Interrupts on Metro Mini

by cking0704 on Tue Mar 31, 2020 1:25 am

Carter, thanks for the feedback. The intermittent interrupt failure is only occurring on the pin change interrupt (not hardware interrupt), so it *should* trigger on any change of any pin on the port specified, so long as that pin is activated (pin mask). I’m not aware of how pin change interrupts can be set on rising or falling edges.

cking0704
 
Posts: 19
Joined: Wed Mar 25, 2020 8:09 am

Please be positive and constructive with your questions and comments.