@peekay123, no, I didn’t come to the conclusion of Log.trace calls are causing crash. They’ve been added after the fact and I think they are only pointing to where I think the problem is. I’m not sure what the evidence for Log calls causing crash? I do not have these calls in my normal firmware.
The code below and above is the exact code I am running on my test devices, so there are not other sensors or anything else being read from. In my normal firmware, there are other sensors, like a ds18b20, but nothing that requires pulse counting like this.
Here is a revision with change to Serial.print’s:
#include "application.h"
SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
#define SENSOR_FAIL 2
#define SENSOR_SUCCESS 3
int dataPin = D3;
int powerPin = C0;
int status;
uint32_t timerStart;
uint32_t timerStop;
uint16_t rpm;
uint32_t delayTimer = 3000;
unsigned long rpmCountFrozen = 0;
volatile unsigned long rpmCount;
void getRpm();
void interrupt();
void setup() {
Serial.begin(9600);
pinMode(dataPin, INPUT);
pinMode(powerPin, OUTPUT);
}
void loop() {
getRpm();
Serial.print("RPM: ");
Serial.print(rpm);
Serial.print(", millis: ");
Serial.println(millis());
delay(1000);
}
void getRpm() {
digitalWrite(powerPin, LOW);
delay(50);
Serial.println("TACH0");
rpm = 0;
rpmCount = 0;
rpmCountFrozen = 0;
attachInterrupt(dataPin, interrupt, RISING);
timerStart = millis();
Serial.println("TACH1");
while(millis() - timerStart < delayTimer) {
Particle.process();
}
Serial.println("TACH2");
timerStop = millis();
rpmCountFrozen = rpmCount;
Serial.println("TACH3");
//Disable interrupt when calculating
noInterrupts();
delay(1);
detachInterrupt(dataPin);
interrupts();
Serial.println("TACHDET");
Serial.flush();
Serial.println("TACH4");
Serial.flush();
rpm = (uint16_t)(rpmCountFrozen / ((timerStop - timerStart) / 1000));
Serial.println("TACH5");
Serial.flush();
rpmCount = 0;
Serial.println("TACH6");
Serial.flush();
if(rpm >= 0 && rpm < 5000) {
status = SENSOR_SUCCESS;
} else {
status = SENSOR_FAIL;
rpm = 0;
}
digitalWrite(powerPin, HIGH);
return;
}
void interrupt() {
rpmCount++;
}