Logics for 4-5 independent temperature events

Hello all and sorry for bad english.

Last week i'm trying to make some "project", that can switch some relays as reaction on temperature from DHT11 sensors, i have.

Main goal is something like: "Measuring temperature, if it is lower than (trashhold level or taken value) than do ((short the relay, light some LED, or many more)). Do the same with another sensors... Then output to serial or web like "Temp1 is ..., Temp2 is ... and so on"".

Problems are:

  1. How can i switch relay with heating load on at 30 seconds, then switch em off, and do it with anoter pairs - DHT11-relay?
    If my code contains something like
if (temp < 20) {
DigitalWrite(3, HIGH);
DigitalWrite(4, HIGH);
Delay(30000);
DigitalWrite(3, LOW);
DigitalWrite(4, LOW);
}

I will have an 30 secs delay between each step of program and will have step-by-step measuring and reaction on it

So that will no way to send commands over ethernet.

  1. How can i switch number of relays in one tact like: "Measure for DHT11 #1, DHT11 #2, DHT11 #3, DHT11 #4, then switch Relays # 1,3,4 On and wait 30 sec< then OFF for all"?

Thanks.

You need to study the blinkWithoutDelay example - in order to manage several things at once you have to avoid using delay and start using timestamps.

  1. How can i switch relay with heating load on at 30 seconds, then switch em off, and do it with anoter pairs - DHT11-relay?

Look at the blink with delay example, and get rid of that delay.

Think about how YOU would switch the relay on and off if the on time were 30 days, instead of 30 seconds. You could stand next to the relay, with your finger poised over the switch, waiting for the 30 days to elapse. No sleeping, no bathroom breaks, no meal breaks.

On the other hand, you could write down when the relay was turned on, and how long it is supposed to be on for. Periodically, you'd check to see if it was time to turn it off. If so, you'd do so. If not, you'd just walk away.

Periodically, on the Arduino, means every pass through loop(). Writing down when means using a variable to store a value. Checking the time is a matter of calling millis().

  1. How can i switch number of relays in one tact like: "Measure for DHT11 #1, DHT11 #2, DHT11 #3, DHT11 #4, then switch Relays # 1,3,4 On and wait 30 sec< then OFF for all"?

I'm not sure that I understand the question. Are the relays linked to the temperature sensors? That is, if temperature sensor 2 reads high, do you activate relay 2?

If so, then the sensor and relay actions are not linked to other sensor and relay actions. Read sensor n. Make a decision regarding it's value. Implement the decision. Record when the decision involved turning the relay on.

Separately, check the on time(s) (for the relay(s) that are on (on time is not 0)) against the current time, to see if it is time to turn the relays off (and clear the on time).

PaulS:

  1. How can i switch number of relays in one tact like: "Measure for DHT11 #1, DHT11 #2, DHT11 #3, DHT11 #4, then switch Relays # 1,3,4 On and wait 30 sec< then OFF for all"?

I'm not sure that I understand the question. Are the relays linked to the temperature sensors? That is, if temperature sensor 2 reads high, do you activate relay 2?

If so, then the sensor and relay actions are not linked to other sensor and relay actions. Read sensor n. Make a decision regarding it's value. Implement the decision. Record when the decision involved turning the relay on.

Separately, check the on time(s) (for the relay(s) that are on (on time is not 0)) against the current time, to see if it is time to turn the relays off (and clear the on time).

No!
Measurement takes me state of relays.

T1=10 (too cold) - do the switching
T2=25 (too cold) - do the switching
T3=35 (normal) - do nothing
T3=28 (too cold) - do the switching

so there i have triggering rule:

DigitalWrite (3, HIGH);
DigitalWrite (4, HIGH);
DigitalWrite (5, LOW);
DigitalWrite (6, HIGH);
Delay(30000);  - or like, were told, some construction from BlinkWithoutDelay
DigitalWrite (3, LOW);
and so ...

And if rule will be ON OFF OFF OFF
I must write whole section with

DigitalWrite (3, HIGH);
DigitalWrite (4, LOW);
DigitalWrite (5, LOW);
DigitalWrite (6, LOW);
Delay(30000);  - or like, were told, some construction from BlinkWithoutDelay
DigitalWrite (3, LOW);
and so ...

Or possible do it with some function?

No!
Measurement takes me state of relays.

I don't understand. Instead of trying to define what you want to do in terms of code, try describing where the 4 sensors are, what the 4 relays control, and under what circumstances each relay should be turned on.

Okay
Here is a code for 2 DHT's (with delays yet :blush:)

#include <Ethernet.h>
#include <dht11.h>

const int kol_datchikov=2; //how many DHT11 sensors are
dht11 TEMP[kol_datchikov]; 
int TEMPPIN[kol_datchikov]={2.3}; //what ports used
int temp_critical[kol_datchikov]={50,50}; // temp trashhold
int temp_time=500;
void setup(){
for (int i=0; i<=kol_datchikov; i++) {
		pinMode(A0+i,OUTPUT);  
for (int i=0; i<=kol_datchikov; i++) 	  //set all LOW 
		digitalWrite(A0+i,LOW);
for (int i=kol_datchikov; i>=0; i--) {   	//test TEMP relays one by one  
		for (int j=kol_datchikov; j>=0; j--) {
			if (j==i) {
				digitalWrite(A0+j,HIGH);
				delay(temp_time);
				digitalWrite(A0+j,LOW);
                        }
		}
	}
for (int i=0; i<=kol_datchikov; i++)
		digitalWrite(A0+i,LOW);
	Serial.begin(9600);
	Serial.println("Ready");
}
void loop(){
for (int i=0; i<kol_datchikov; i++) {   //initialization of chk's and print temperature values  
		int chk=TEMP[i].read(TEMPPIN[i]);
		Serial.print("Sensor ");
		Serial.print(i);
		Serial.print(" check...");
		switch (chk) {
			case 0: Serial.println("OK"); break;
			case -1: Serial.println("Checksum error"); break;
			case -2: Serial.println("Time out error"); break;
			default: Serial.println("Sensor error"); break;
		}
		Serial.print("Sensor ");
		Serial.print(i);
		Serial.print(" temperature = ");
		Serial.print((int)TEMP[i].temperature);
		Serial.print(" oC");
		Serial.print("\n");
	}
int current_temp[kol_datchikov];  //read temperature from sensors and check it
	bool temp_need[kol_datchikov];
	for (int i=0; i<kol_datchikov; i++)
		temp_need[i]=false;
	bool temp_check=false;
	for (int i=0; i<kol_datchikov; i++) {
	current_temp[i]=(int)TEMP[i].temperature;
	if ((int)TEMP[i].temperature > temp_critical[i]) {
	        temp_need[i]=true;
			temp_check=true;
		}
	}
	if (temp_check==true) {    	//switch relays on if needed	
		for (int i=1; i<=kol_datchikov; i++)
			if (temp_need[i]==true)
				digitalWrite(A0+i,HIGH);
		delay(200);
		digitalWrite(A0,HIGH);
		delay(temp_time);
		for (int i=0; i<=kol_datchikov; i++)
			digitalWrite(A0+i,LOW);
	}
	else 	//switch all relays off
		for (int i=0; i<=kol_datchikov; i++)
			digitalWrite(A0+i,LOW);
}

I have 5 relays @ A0-A4 outputs
4 of them connected to load via relays
#5 is A0 and connected to AC220V, that turns on everything

int TEMPPIN[kol_datchikov]={2.3}; //what ports used

Hmmm. That doesn't look right.

				digitalWrite(A0+j,HIGH);
				delay(temp_time);
				digitalWrite(A0+j,LOW);

A0 is the analog pin used as a digital pin. Is that what you are doing?

	if (temp_check==true) {    	//switch relays on if needed

The ==true bit is not needed. temp_check is either true or false all by itself.

In English, at least, temp is an abbreviation for both temperature and temporary. I'd suggest you use something other than temp_ as a prefix. Since it appears as though what you are doing is turning heaters on, might I suggest that heat_check and heat_need make more sense.

It looks to me that your logic is basically this:

LOOP
Gather temperature data
Take action on relays based on data
Output information
GOTOLOOP

In that last piece of code, setup() is a mess. Missing braces again and some redundancy. To keep braces organised I always immediately type my closing brace when I type the open brace. Then I put my code inside them. I also use the IDE's format feature which will fail with unmatched braces and brackets.

A0 is the analog pin used as a digital pin. Is that what you are doing?

Yez. And it works. I'm doing like this because every digital port is taken
5 by DHT's, other for sd-card and Eth-card and so.
May be i must send there something else instead of (, HIGH) ?..

In English, at least, temp is an abbreviation for both temperature and temporary. I'd suggest you use something other than temp_ as a prefix. Since it appears as though what you are doing is turning heaters on, might I suggest that heat_check and heat_need make more sense.

I know. Yes, my mistake.
This code was written in russian and names there was like (temperatura, proverka, povtor, sverka and so) and for this forum it was rapidly manually converted.

How can i switch relay with heating load on at 30 seconds, then switch em off,...

A couple different approaches:

  1. use a timer: keep a free running timer that increments a software counter. Whenever a switch is turned on, record the reading of that free running timer; On future runs, test the counter to see if sufficient time has elapsed and act accordingly.
  2. use a loop counter: if your loop time is (fairly) constant, you can count the number of loops that have passed and use that to determine if sufficient time has gone by.

I don't fully understand the requirement here, but assuming you tweak your sketch to incorporate checking millis against an array of unsigned longs for remembering when each relay was turned on, does it matter that having turned the relays off, they may well be immediately turned back on if the threshold is still exceeded?

In other words, why worry about the 30 seconds at all - turn the relay on when the threshold is exceeded and off when it isn't. A second threshold would let you add some hysterisis to avoid continuous cycling of the relay around the threshold value.