Error with attachInterrupt on a Wemos D1 mini

Hi all, First I'll admit I a huge noob with coding. But I'm trying to program a Wemos D1 mini to act as a flow meter. The program I've written complies fine, but when it gets uploaded the light on the D1 flashes constantly and in the Serial I get the following error:

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

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

I've narrowed the problem down to the attachInterrupt line, but can't figure out whats wrong with it. Every example online I've found has something similar.

Here's my code:

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char AUTH[] = "*****";
char WIFI_SSID[] = "*****";
char WIFI_PASS[] = "*****";

int flowPin;
int flowRate; 
int count=1; 

void setup() {
  flowPin = 4;
  pinMode(flowPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(4), Flow, RISING);
  Serial.begin(115200);
  delay(1000);
  Serial.print("Connecting to: ");
  Serial.print(WIFI_SSID);

  WiFi.begin(WIFI_SSID, WIFI_PASS);
  int wifi_ctr = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(5000);
    Serial.print("-");
  }

  Serial.println("WiFi Connected");

  Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS);
  
}

void loop() {
  Blynk.run();

  count = 0;
  interrupts();
  delay(1000);
  noInterrupts();

  flowRate = (count * 2.25);
  flowRate = (flowRate * 60);
  flowRate = (flowRate / 1000);

  Serial.println(flowRate);
  Serial.print("L/Min");
  Blynk.virtualWrite(V1, flowRate);

}

void Flow() {
  count++;
}

Hello,

Declare count as volatile :
volatile int count=1;

And add the attribute ICACHE_RAM_ATTR to the ISR :
void ICACHE_RAM_ATTR Flow() {

You can add [code] before your code and [/code] after your code.

You're a life saver! Thanks a lot, that worked.

It did error out on me though when I added the ICACHE_RAM_ATTR to the ISR. Instead it worked when I declared:
void ICACHE_RAM_ATTR Flow();
before void setup()

The ISR MUST be defined before the attachInterrupt call. Instead of adding the ISR prototype, you can simply move the ISR above setup().

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