Delay function is not working properly

Hi everyone,
I am new in this field. I was trying to prepare a sensor node using Arduino Mega and raspberry pi. The temperature-humidity sensor (DHT11), CO2 sensor (Gravity infrared CO2 sensor V2), digital Light sensor (SI1145), EC (Sal-BTA), and pH sensor( PH0-14 Value Detect Sensor Module). I collected and tested individual sensor codes, and later I tried to combine all the codes. I am recording the data in 10-sec intervals, but sometimes the delay function is not working. I attached the Arduino code and screenshot of the data-saving problem. I think I did some mistakes during the code combination. Can anyone show me where is the mistake?

#include <dht11.h>            //for Temp_Humi sensor (DHT11)
#include <Wire.h>
#include <SPI.h>              // for CO2 sensor
#include <Adafruit_GFX.h>     // for CO2 sensor
#include <Adafruit_SI1145.h>  // for Digital light sensor (UV_Vis_IR)
Adafruit_SI1145 uv = Adafruit_SI1145(); // for Digital light sensor (UV_Vis_IR)

#include <dht11.h>
#define DHT11PIN 13
dht11 DHT11;
int pH_Value;
float Voltage, pHvalue;


#define  sensorIn  2   // Sensor PWM interface
#define  interruptNumber  0   // interrupt number
unsigned long pwm_high_start_ticks=0, pwm_high_end_ticks=0;
float pwm_high_val=0, pwm_low_val=0, concentration;
volatile uint8_t flag=0;

void interrupt_rising()
{
  pwm_high_start_ticks = micros();    // store the current micros() value
  if(2 == flag){
    flag = 4;
    pwm_low_val = pwm_high_start_ticks - pwm_high_end_ticks;
  }
  else{
    flag = 1;
  }
  attachInterrupt(interruptNumber, interrupt_falling, FALLING);
}

void interrupt_falling(){
  pwm_high_end_ticks = micros();    // store the current micros() value
  if(1 == flag){
    flag = 2;
    pwm_high_val = pwm_high_end_ticks - pwm_high_start_ticks;
  }
  attachInterrupt(interruptNumber, interrupt_rising, RISING);
}


void setup()
{
  Serial.begin(9600);
  pinMode(sensorIn, INPUT);
  attachInterrupt(interruptNumber, interrupt_rising, RISING);
  pinMode(pH_Value, INPUT);
  if (! uv.begin()) {
    while (1);
    Serial.println("OK!");
    Serial.println("temperature and Humidity Data");
    Serial.println("UV_Vis_IR Data");
    Serial.println("Didn't find Si1145");
    Serial.println("CO2(ppm), Temp(C), Humi(%), Vis, IR, UV, pH");
    }
  }

  void loop()
  {
//Temp-Humi sensor
      Serial.println();
      int chk = DHT11.read(DHT11PIN);
//Temp-Humi sensor

//Digital light sensor
      float UVindex = uv.readUV();
      UVindex /= 100.0;
//Digital light sensor

//CO2 sensor
     if(flag == 4){
       flag = 1;
       float pwm_high_val_ms = (pwm_high_val * 1000) / (pwm_low_val + pwm_high_val);
       if (pwm_high_val_ms < 0.01){
       Serial.print("Fault");
      }
      else if (pwm_high_val_ms < 80.00){
       Serial.print("preheat");
      }
      else if (pwm_high_val_ms < 998.00){
        float concentration = (pwm_high_val_ms - 2) * 5;
        Serial.print(concentration); Serial.print(" , ");
      }
      else{
        Serial.print("Beyond the range");
        
      }
//CO2 sensor

// pH sensor
        pH_Value = analogRead(A1);
        Voltage = pH_Value*5.0/1024/6;
        pHvalue = 3.5*Voltage;
// pH sensor


        Serial.print((float)DHT11.temperature, 2); Serial.print(" , ");
        Serial.print((float)DHT11.humidity, 2); Serial.print(" , ");
        Serial.print(uv.readVisible()); Serial.print(" , ");
        Serial.print(uv.readIR()); Serial.print(" , ");
        Serial.print(UVindex); Serial.print(" , ");
        Serial.print(pHvalue); 
        delay(10000);
        }
  }'

The screenshot is consistent with noise, or nonprinting characters being received, possibly due to faulty wiring. It does not suggest that delay() is failing.

This is a serious mistake.
float pwm_high_val=0, pwm_low_val=0, concentration;

Variables shared with interrupts MUST be declared volatile, and access to them by the main program MUST be protected by disabling interrupts during access. In general, something like this is required

noInterrupts(); // off in main loop
int shared_variable_copy = shared_variable; //copy variable shared with interrupt routine
interrupts();
Serial.println(shared_variable_copy);

Please post a complete wiring diagram, and photos of your setup. What does the RPi do?

1 Like

Maybe those are the empty lines you print at the beginning of every loop(). If 'flag' is not equal to 4 there will be no other output.

1 Like

hi
I removed all empty lines and run the code. I am still facing the same problem. This technique is not working.

It might be, but I run several codes using the same technique. I didn't face any data receiving problems. This is the first time, I am facing this type of error. The problem is somewhere else...

delay(); is not in the main loop(). It's in the else from the last if statement. The finished code should look like this:

#include <dht11.h>            //for Temp_Humi sensor (DHT11)
#include <Wire.h>
#include <SPI.h>              // for CO2 sensor
#include <Adafruit_GFX.h>     // for CO2 sensor
#include <Adafruit_SI1145.h>  // for Digital light sensor (UV_Vis_IR)
Adafruit_SI1145 uv = Adafruit_SI1145(); // for Digital light sensor (UV_Vis_IR)

#include <dht11.h>
#define DHT11PIN 13
dht11 DHT11;
int pH_Value;
float Voltage, pHvalue;


#define  sensorIn  2   // Sensor PWM interface
#define  interruptNumber  0   // interrupt number
unsigned long pwm_high_start_ticks=0, pwm_high_end_ticks=0;
float pwm_high_val=0, pwm_low_val=0, concentration;
volatile uint8_t flag=0;

void interrupt_rising()
{
  pwm_high_start_ticks = micros();    // store the current micros() value
  if(2 == flag){
    flag = 4;
    pwm_low_val = pwm_high_start_ticks - pwm_high_end_ticks;
  }
  else{
    flag = 1;
  }
  attachInterrupt(interruptNumber, interrupt_falling, FALLING);
}

void interrupt_falling(){
  pwm_high_end_ticks = micros();    // store the current micros() value
  if(1 == flag){
    flag = 2;
    pwm_high_val = pwm_high_end_ticks - pwm_high_start_ticks;
  }
  attachInterrupt(interruptNumber, interrupt_rising, RISING);
}


void setup()
{
  Serial.begin(9600);
  pinMode(sensorIn, INPUT);
  attachInterrupt(interruptNumber, interrupt_rising, RISING);
  pinMode(pH_Value, INPUT);
  if (! uv.begin()) {
    while (1);
    Serial.println("OK!");
    Serial.println("temperature and Humidity Data");
    Serial.println("UV_Vis_IR Data");
    Serial.println("Didn't find Si1145");
    Serial.println("CO2(ppm), Temp(C), Humi(%), Vis, IR, UV, pH");
    }
  }

  void loop()
  {
//Temp-Humi sensor
      Serial.println();
      int chk = DHT11.read(DHT11PIN);
//Temp-Humi sensor

//Digital light sensor
      float UVindex = uv.readUV();
      UVindex /= 100.0;
//Digital light sensor

//CO2 sensor
     if(flag == 4){
       flag = 1;
       float pwm_high_val_ms = (pwm_high_val * 1000) / (pwm_low_val + pwm_high_val);
       if (pwm_high_val_ms < 0.01){
       Serial.print("Fault");
      }
      else if (pwm_high_val_ms < 80.00){
       Serial.print("preheat");
      }
      else if (pwm_high_val_ms < 998.00){
        float concentration = (pwm_high_val_ms - 2) * 5;
        Serial.print(concentration); Serial.print(" , ");
      }
      else{
        Serial.print("Beyond the range");
        
      }
//CO2 sensor

// pH sensor
        pH_Value = analogRead(A1);
        Voltage = pH_Value*5.0/1024/6;
        pHvalue = 3.5*Voltage;
// pH sensor


        Serial.print((float)DHT11.temperature, 2); Serial.print(" , ");
        Serial.print((float)DHT11.humidity, 2); Serial.print(" , ");
        Serial.print(uv.readVisible()); Serial.print(" , ");
        Serial.print(uv.readIR()); Serial.print(" , ");
        Serial.print(UVindex); Serial.print(" , ");
        Serial.print(pHvalue); 
        // delay() used to be here
        }
        delay(10000);
  }

tinkerer9

1 Like

There will be other problems, like nonsensical data. Guaranteed.

1 Like

hi
This technique works but one empty line is adding automatically (picture attached)... Would you please suggest me how can I solve this issue?
Data

Not sure, the issue may be that Serial.println(); at the beginning of the loop() has no quotes (" ") in the parameter. Try replacing in with Serial.println(""); (it has quotes). Not sure if this will work, but give it a try.

tinkerer9

Hi Tinkerer9
I put the Serial.println() at the end (before the delay function) and it works. Thanks for your help...

I'm confused by @Chowdhury_milon accepting @anon44338819 's solution. Tinkerer9 suggested adding quotes inside the (), but Chowdhury_milon said the fix was due to moving the Serial.print() in the code.

You absolutely do not need the quotes as the following code and output shows:

void setup() {
 Serial.begin(9600);
 Serial.println("foo");
 Serial.println();
 Serial.println();
 Serial.println();
 Serial.println();
 Serial.println();
 Serial.println("bar");
}

void loop() {}

image

Accepting that solution might indicate to future forum readers that the quotes are required, when they are definitely not.

1 Like

I did not know that. I'm a beginner too, and I do not know much about things like that.

tinkerer9

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