Having an issue with multiple timer interrupts.

debelapantagana
Posts: 1
Joined: Tue May 18, 2021 7:24 am

Having an issue with multiple timer interrupts.

Postby debelapantagana » Wed May 19, 2021 5:12 pm

Hello,
trying to learn more about timers and interrupts i made a small example with DHT22 sensor and LDR resistor. My goal is to measure temperature and light in different intervals. First i made a sketch with one timer and everything worked correctly. After I added a second timer and interrupt for LDR readings my temperature interrupt stopped working.

Code: Select all

#include <Arduino.h>
#include <DHT.h>

#define LDR 33
#define DHTPIN 4
#define DHTTYPE DHT22


DHT dht(DHTPIN, DHTTYPE);
volatile bool getTemp = false;
volatile bool getLight = false;

hw_timer_t *tempTimer = NULL;
hw_timer_t *ldrTimer = NULL;

portMUX_TYPE muxTemp = portMUX_INITIALIZER_UNLOCKED;
portMUX_TYPE muxLdr = portMUX_INITIALIZER_UNLOCKED;

void IRAM_ATTR tempInterrupt() {
  portENTER_CRITICAL_ISR(&muxTemp);
  getTemp = true;
  portEXIT_CRITICAL_ISR(&muxTemp);
}

void IRAM_ATTR ldrInterrupt() {
  portENTER_CRITICAL_ISR(&muxLdr);
  getLight = true;
  portEXIT_CRITICAL_ISR(&muxLdr);
}

void setup() {
  Serial.begin(115200);
  dht.begin();

  tempTimer = timerBegin(0, 80, true);
  timerAttachInterrupt(tempTimer, &tempInterrupt, true);
  timerAlarmWrite(tempTimer, 5000000, true);

  ldrTimer = timerBegin(0, 80, true);
  timerAttachInterrupt(ldrTimer, &ldrInterrupt, true);
  timerAlarmWrite(ldrTimer, 1000000, true);

  timerAlarmEnable(ldrTimer);
  timerAlarmEnable(tempTimer);
}

void loop() {
  if (getTemp) {
    portENTER_CRITICAL_ISR(&muxTemp);
    getTemp = false;
    portEXIT_CRITICAL_ISR(&muxTemp);

    Serial.print("Temperature: ");
    Serial.println(dht.readTemperature());
  }

  if (getLight) {
    portENTER_CRITICAL_ISR(&muxLdr);
    getLight = false;
    portEXIT_CRITICAL_ISR(&muxLdr);

    Serial.print("LDR adc reading: ");
    Serial.println(analogRead(LDR));
  }
}
When I run this code it doesn't print temperature readings but only LDR values. If i comment out all lines related to LDR readings, temperature readings work correctly. I also tried to increase LDR interval so it triggers after temperature interrupt but it still only prints out LDR values.
I'm using VS Code with platformio and DOIT esp32 devkit1 board.
Im very new to the esp platform so I feel like I'm missing something obvious here.
Thanks in advance for your help.

Who is online

Users browsing this forum: No registered users and 71 guests