Everytime the interrupt function starts to count the number of incoming HIGHs from the encoder (in the void isr() ), I get a IWDT time out.
(Guru Meditation Error: Core 1 panic'ed (Interrupt wdt timeout on CPU1)
Can someone check my code to see what could cause this?
Thanks in advance!
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <EEPROM.h>
#define EEPROM_SIZE 1
// #define GPIO 2 (dit is voor de esp8266)
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
// Motor A
int motor1Pin1 = 27;
int motor1Pin2 = 26;
int ENA = 25;
int encoder = 13;
volatile int counter;
int currentValue = 0;
int previousValue = 0;
int openingRotations;
int TurnMotorBack;
long OpeningDelay = 0;
long Factor = 1000000;
long Day = 86400;
RTC_DATA_ATTR int bootCount = 0;
// Setting PWM properties
const int freq = 85000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 200;
class MyCallbacks : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
for (int i = 0; i < value.length(); i++) {
Serial.print(value[i]);
}
if (isDigit(value[1])) {
OpeningDelay = (value[0]*10L*3600L + value[1]*3600L + value[2]*10L*60L + value[3]*60L - 1932480L); // for some reason the number 1932480 is the starting value
Serial.println(OpeningDelay);
}
//Calibration of number of rotations motor
if (value.find("OpenCurtain") != -1) {
Serial.println("Open Curtain");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
} else if (value.find("StopMotor") != -1) {
Serial.println("Stop motor!");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
openingRotations = counter;
EEPROM.write(0, openingRotations);
EEPROM.commit();
counter = 0;
Serial.println(openingRotations);
delay(2000);
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
}
else if (value.find("Complete") != -1) {
Serial.println("Going to sleep for the 1 time");
bootCount = bootCount +1;
digitalWrite(15,LOW); // power OFF encoder before deepsleep
esp_sleep_enable_timer_wakeup(90000000L); //OpeningDelay*Factor
esp_deep_sleep_start();
}
}
}
};
void setup() {
EEPROM.begin(EEPROM_SIZE); // initialize EEPROM with predefined size (flashmemory)
pinMode(15, OUTPUT);
digitalWrite(15,HIGH); // power to encoder
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(encoder, INPUT);
attachInterrupt(13, isr, RISING);
//pinMode(GPIO, OUTPUT);
// digitalWrite(GPIO, HIGH); // disable power led (dit is voor de ESP8266)
// configure LED PWM functionalitites
ledcSetup(pwmChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(ENA, pwmChannel);
ledcWrite(pwmChannel, 200);
Serial.begin(115200);
BLEDevice::init("CrouzeSystem");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
pCharacteristic->setCallbacks(new MyCallbacks());
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
// Curtains opens every 24h and goes back to deepsleep
if (bootCount != 0){
delay(300);
Serial.println("Boot number: " + String(bootCount));
openingRotations = EEPROM.read(0);
Serial.println("opening rotations: " + openingRotations);
counter = 0;
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
if (counter >= openingRotations){
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
counter = 0;
delay(1000);
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
if (counter >= openingRotations){
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
counter = 0;
digitalWrite(15,LOW); // power OFF encoder before deepsleep
esp_sleep_enable_timer_wakeup(Day*Factor); // Ik kan de esp32 enkel 24h laten slapen door 24 x 1 uur te laten slapen (nog te fiksen)
esp_deep_sleep_start();
}
}
}
delay(300);
}
void isr() {
// calibration counter
currentValue = digitalRead(encoder);
if (currentValue != previousValue) {
if (currentValue == 1) {
counter++;
Serial.println(counter);
}
}
previousValue = currentValue;
}
void loop() {
TurnMotorBack = digitalRead(motor1Pin1);
if (counter >= openingRotations && TurnMotorBack == HIGH) {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
counter = 0;
}
}