Trying to get jurs code from: Rain Gauge topic to work with an ESP32.
jurs code:
#define TENMINUTES (600*1000L) // ten minutes are 600000 milliseconds
#define REEDPIN 32 //changed for RSP32
#define REEDINTERRUPT 0
volatile int pulseCount_ISR;
void reedSwitch_ISR()
{
static unsigned long lastReedSwitchTime;
// debounce for a quarter second = max. 4 counts per second
if (labs(millis()-lastReedSwitchTime)>250)
{
pulseCount_ISR++;
lastReedSwitchTime=millis();
}
}
void setup() {
Serial.begin(9600);
Serial.println();
Serial.println("Total\tCurrent");
pinMode(REEDPIN,INPUT_PULLUP);
attachInterrupt(REEDINTERRUPT,reedSwitch_ISR, FALLING);
}
unsigned long lastSecond,last10Minutes;
long lastPulseCount;
int currentPulseCount;
void loop()
{
// each second read and reset pulseCount_ISR
if (millis()-lastSecond>=1000)
{
lastSecond+=1000;
noInterrupts();
currentPulseCount+=pulseCount_ISR; // add to current counter
pulseCount_ISR=0; // reset ISR counter
interrupts();
Serial.print(lastPulseCount);Serial.print('\t');Serial.print(currentPulseCount);
Serial.println();
}
// each 10 minutes save data to another counter
if (millis()-last10Minutes>=TENMINUTES)
{
last10Minutes+=TENMINUTES; // remember the time
lastPulseCount+=currentPulseCount; // add to last period Counter
currentPulseCount=0;; // reset counter for current period
}
}
Thank you jurs for providing your code in the referenced topic.
Just "0 0" in Serial Monitor; when executing jurs code on ESP32.
What needs to be done for jurs code to work with ESP32?
Tried this:
#define FIVEMINUTES (300*1000L) // ten minutes are 600000 milliseconds
#define REEDPIN 32
#define REEDINTERRUPT 0
volatile int pulseCount_ISR = 0;
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR reedSwitch_ISR()
{
static unsigned long lastReedSwitchTime;
// debounce for a quarter second = max. 4 counts per second
if (labs(millis()-lastReedSwitchTime)>250)
{
portENTER_CRITICAL_ISR(&mux);
pulseCount_ISR++;
lastReedSwitchTime=millis();
portEXIT_CRITICAL_ISR(&mux);
}
}
void setup() {
Serial.begin(9600);
Serial.println();
Serial.println("Total\tCurrent");
pinMode(REEDPIN,INPUT_PULLUP);
attachInterrupt(REEDINTERRUPT,reedSwitch_ISR, FALLING);
}
unsigned long lastSecond,last5Minutes;
long lastPulseCount;
int currentPulseCount;
void loop()
{
// each second read and reset pulseCount_ISR
if (millis()-lastSecond>=1000)
{
lastSecond+=1000;
portENTER_CRITICAL(&mux);
//currentPulseCount+=reedSwitch_ISR(); // add to current counter
pulseCount_ISR=0; // reset ISR counter
portEXIT_CRITICAL(&mux);
Serial.print(lastPulseCount);Serial.print('\t');Serial.print(currentPulseCount);
Serial.println();
}
// each 5 minutes save data to another counter
if (millis()-last5Minutes>=FIVEMINUTES)
{
last5Minutes+=FIVEMINUTES; // remember the time
lastPulseCount+=currentPulseCount; // add to last period Counter
currentPulseCount=0;; // reset counter for current period
}
}
Still only "0 0" in Serial Monitor.
Just starting to use ESP32 with interrupts.
William