Have questions about FETS, transistors, measurement, power supplies, or anything else electrical?

User avatar
By Zakaria
#91093 I am trying to expand the number of IO ports of an ESP8266 (wemos mini D1) using an MCP23017.
I am using INTA of the MCP23017 to trigger an interrupt on ESP8266's pin D5 (as shown in the attached figure).
https://ibb.co/SdqNYLb
The code below works fine. When I press the button switch, the pin number attached to the pressed button is shown in the serial monitor.

#include <Wire.h>
#include "Adafruit_MCP23017.h"
Adafruit_MCP23017 mcp;

volatile unsigned long last_interrupt; // Used to store the time in millis of the last interrupt, for debouncing
int debounce_delay = 100; // The minimum delay in milliseconds between button presses, for debouncing
volatile uint8_t pin_num; // when an interrupt is detected and decoded the pin which generated the interrupt is stored here
volatile uint8_t int_pin_val ;
int esp_interrupt_pin=14; // GPIO14 (D5)
bool send_to_me;
byte mcp_PA0=0;
void cleanInterrupt(){
while(!(mcp.digitalRead(0))) ;
// now MCP is ready to receive another interrupt
}
void ICACHE_RAM_ATTR intCallBack()
{
pin_num = mcp.getLastInterruptPin();
int_pin_val = mcp.getLastInterruptPinValue();
if(millis()-last_interrupt>=debounce_delay) // Debounce routine
{
send_to_me=true;
}
last_interrupt=millis();
cleanInterrupt(); // Call the function that clears all of the interrupt registers
}
void setup() {
Serial.begin(115200);
pinMode(esp_interrupt_pin,INPUT);
mcp.begin();
mcp.setupInterrupts(false,false,LOW);
mcp.pinMode(mcp_PA0, INPUT);
mcp.pullUp(mcp_PA0, HIGH); // turn on a 100K pullup internally
mcp.setupInterruptPin(mcp_PA0,CHANGE);
cleanInterrupt(); // Call the function that clears all of the interrupt registers
// Here we attach an interrupt to the MCU pin that will be used to listen for interrupts from the mcp...
attachInterrupt(digitalPinToInterrupt(esp_interrupt_pin),intCallBack,FALLING);
}
void loop() {
if (send_to_me==true){
Serial.print("Pin = ");
Serial.println(pin_num);
send_to_me=false;
}
}

However, when I keep pressing the button without releasing it, after few seconds, I get watchdog timer reset.

ets Jan 8 2013,rst cause:4, boot mode:(3,6)

wdt reset
load 0x4010f000, len 3584, room 16
tail 0
chksum 0xb0
csum 0xb0
v2843a5ac
~ld

I tried to put yield() in the loop but didn't solve the issue.
Any advice or help is appreciated.
User avatar
By Zakaria
#91113 I have solved this issue. The code is below:
Code: Select all#include <Wire.h>
#include "Adafruit_MCP23017.h"

Adafruit_MCP23017 mcp;

volatile unsigned long last_interrupt; // Used to store the time in millis of the last interrupt, for debouncing
int debounce_delay = 100;              // The minimum delay in milliseconds between button presses, for debouncing


const int   watchdog2 = 50;
unsigned long previousMillis2 = millis();

volatile uint8_t pin_num;   // when an interrupt is detected and decoded the pin which generated the interrupt is stored here
volatile uint8_t int_pin_val ;
int esp_interrupt_pin=14; // GPIO14 (D5)
bool send_to_me;
byte mcp_PA0=0;

void ICACHE_RAM_ATTR intCallBack()
{
  pin_num = mcp.getLastInterruptPin();
  int_pin_val = mcp.getLastInterruptPinValue();

  if(millis()-last_interrupt>=debounce_delay)  // Debounce routine
  {
    send_to_me=true;
  }
  last_interrupt=millis();
}
void setup() {
  Serial.begin(115200);
  pinMode(esp_interrupt_pin,INPUT); // Initialise the MCU pin used for the Interrupt line (INTA or INTB on the Waveshare board)
  mcp.begin(); // default address, no solder pads bridged
  mcp.setupInterrupts(false,false,LOW);

  mcp.pinMode(mcp_PA0, INPUT);
  mcp.pullUp(mcp_PA0, HIGH);  // turn on a 100K pullup internally
  mcp.setupInterruptPin(mcp_PA0,CHANGE);                         
  // Here we attach an interrupt to the MCU pin that will be used to listen for interrupts from the mcp...
  attachInterrupt(digitalPinToInterrupt(esp_interrupt_pin),intCallBack,FALLING);       
}

void loop() {

if(send_to_me==true){
    int val0 = mcp.digitalRead(0);
    unsigned long currentMillis2 = millis();
  if ( currentMillis2 - previousMillis2 > watchdog2 ) {
    previousMillis2 = currentMillis2;
       Serial.print("Pin = ");
       Serial.println(pin_num);
    send_to_me=false;
  }
  }

}