Program within program and waiting for change ?

Dear Arduino community

I am trying to make something but I’m not really sure how to solve this, I tried to google it but I can’t really find what I need.

First:

I want to log temperature every 2 hours lets say, I can use a delay of two hours but that will make my sketch stop for 2 hours before checking the gas every 10 minutes.

I am wondering what kind of function I have to use, currently I am using the follwing:

(Code to read temperature sensors)

delay(2hours) --> (I know it must be 2 hours in MS) 

(Code to read temperature sensors

delay(10 minutes)

I basicly want to create 2 loops not affecting each other with those delays.

Second problem:

I have a setup which checks light sensitivity to conclude if a door is open or shut. Open means the analog signal of the LDR is below 900 and closed means it’s above 900. When I serialprint the data it keeps printing ‘door closed’ because I am using a regular delay and it keeps concluding that it’s closed. I want to only print something when the LDR condition is changed. Let’s say when the door is closed and stays closed for 10 minutes I don’t care and don’t want to see anything on my serialmonitor. When the door opens I want to see appear ‘Door opened’ and that’s it. When the door stays open I don’t want to see any messages.

Third:

A MQ-2 gas sensor has as analog output a bunch of numbers (doh) but how can I know what these numbers mean like % of gas in the air,… ?

I’m new to arduino so these problems are probably peanuts to most of you.
Thank you for your help!

Tom

Problem 1: Please see the Blink Without Delay example or the Several things at once demo thread for some inspiration on how to handle this. Instead of going to sleep for two hours, just note the last time you did it and on each pass of the loop check the current time to see if 2 hours have passed. If so do your thing. If not, do whatever else.

Problem 2:

Look at the State Change example for some inspiration. You need to remember the last state of the door in some variable and only do your printing when the new state of the door is not the same as the old state of the door.

Problem 3: Does the datasheet for the sensor not give some information as to what the value it spits out means? I'm sure it gives some sort of equation for turning the analog value back into a gas level. Sometimes you have to be willing to read a little to figure things out.

Clue #1 it’s called millis https://www.arduino.cc/en/Reference/Millis
It lets you call a function without using a blocking delay.

Clue #2 called a if then statement

if (LDRLess < 900)
{
  // Tell me here 
  Serial.print("DOORS CLOSED.")
}

Clue #3 http://playground.arduino.cc/Main/MQGasSensors

Delta_G:
Problem 2:

Look at the State Change example for some inspiration. You need to remember the last state of the door in some variable and only do your printing when the new state of the door is not the same as the old state of the door.

How do I add this to the following code?
The example is done with a LED and I don’t have the experience on how to do it with variables.

int sensorReading;
int PinLDR = A1

void setup()
{
  Serial.begin(9600);
}
void loop()
{
  sensorReading=analogRead(PinLDR);  
 if (sensorReading<900) 
  {
    Serial.println("door open");  
    
  } 
  else Serial.println("door closed");


 }

Thanks
Jinx

How do I add this to the following code?

You need to add a variable to keep track of the state of the door.

bool doorOpen = false;

When the condition under which the door should be open changes (the door should be open but is not), open the door and change the recorded state:

if(sensorReading<900)
{
   // Door should be open
   if(!doorOpen)
   {
      OpenDoor();
      doorOpen = true;
   }
}

You also need to deal with the state where the door is open but should be closed, as well as where the door is open and should be open, and where the door is closed and should be closed (the last two don’t require actually doing anything but thinking about them).