Temp. Sensor with fan

Hi, so I have this project that I need to use a temp. sensor which is reading the ambient temprature all the time, and I need some help if possible.
What I need to do is, when the sensor reads 25 degrees celsius, the fan turns on for 30 mins and then turns of even if the temp does not get down. If it doesn't get down, after the fan turns off, it waits 30 mins to turn on again for other 30. The process is always like this.

And your attempt at coding this is.... ? (or do you expect someone to do your homework?)

@Filipe-Moleiro

Can you check that you have the answers of the following questions:

1. Temperature sensor is (put Tick Mark): LM35, DS18B20, BMP180, DS3231
2. Arduino Kit is (put Tick Mark) : UNO, MEGA, NANO, DUE
3. Display type is (put Tick Mark): 7-segment, PLCD, I2CLCD
4. Control Structure for your program (put Tick Mark):
(a) if-else
(b) while-do
(c) do-while
(d) case-switch

Following suggestion of Post#1, try to write some codes for the control program of your fan based on the control structures shown above.

Sorry for long waiting, the code that I did just reads the temperature, nothing else.

  1. Temperature sensor is :DS18B20
  2. Arduino Kit is : Leonardo
  3. Display type is : PLCD
  4. Control Structure for your program :
    (a) if-else

Temp._sensor_read_code.ino (1.01 KB)

Consider the following 2 pseudo code:

const unsigned long ENABLE_INTERVAL = 30minutes;
const unsigned long COOLING_INTERVAL = 30minutes;
const int TEMP_THRESHOLD = 25;
unsigned long enable_timestamp;
unsigned long cooling_timestamp;

void setup()
{
	...
	
	enable_timestamp = 0;
	cooling_timestamp = 0;
	
	...
	
}


void loop()
{
	temp_val = read_temperature();
	if (temp_val >= TEMP_THRESHOLD) {
		turn_on_fan();
		delay(ENABLE_INTERVAL);
		turn_off_fan();
		delay(COOLING_INTERVAL);
	}
}

and

const unsigned long ENABLE_INTERVAL = 30minutes;
const unsigned long COOLING_INTERVAL = 30minutes;
const int TEMP_THRESHOLD = 25;
unsigned long enable_timestamp;
unsigned long cooling_timestamp;

void setup()
{
	...
	
	enable_timestamp = 0;
	cooling_timestamp = 0;
	
	...
	
}


void loop()
{
	temp_val = read_temperature();
	if (temp_val >= TEMP_THRESHOLD) {
		if (enable_timestamp == 0) {
			if (cooling_timestamp == 0) {
				//fan has not activated and cooling time is over
				turn_on_fan();
				//set timestamps
				enable_timestamp = millis();
			} else {
				//fan has not activated but it's still in cooling
				if (millis() - cooling_timestamp >= COOLING_INTERVAL) {
					//cooling period is over
					cooling_timestamp = 0;
				} else {
					//cooling period is still going on
				}
			}
		} else {
			//fan is running
			if (millis() - enable_timestamp >= 0) {
				//fan finishes
				//start cooling period here
				turn_off_fan();
				enable_timestamp = 0;
				cooling_timestamp = millis();
			}
		}
	}
}

The first one is simple and straightforward if you just want to do what you are describing and nothing else.

The second one will let you do everything else.

Thank you, my biggest problem was that I didn’t know how to go get the temp. value from the monitor so that I could do the rest.