-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqflex_piscina.ino
128 lines (107 loc) · 3.07 KB
/
sqflex_piscina.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// which analog pin to connect
#define THERMISTORPIN A0
// resistance at 25 degrees C
#define THERMISTORNOMINAL 5000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3500
// the value of the 'other' resistor
#define SERIESRESISTOR 10000
#include <virtuabotixRTC.h>
// Creation of the Real Time Clock Object
//SCLK -> 4, I/O -> 5, CE -> 6
virtuabotixRTC myRTC(4, 5, 6);
int samples[NUMSAMPLES];
const int relepin = 8;
float temperatura;
float limite_temperatura =38.5;
void setup(void) {
Serial.begin(9600);
//analogReference(EXTERNAL);
// seconds, minutes, hours, day of the week, day of the month, month, year
//para ponerlo en hora alimentar el reloj a 5V
//myRTC.setDS1302Time(00, 02, 10, 2, 2, 3, 2015);
// myRTC.updateTime();
pinMode(relepin, OUTPUT);
}
void loop(void) {
myRTC.updateTime();
print_datetime();
uint8_t i;
float average;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
Serial.print("Average analog reading ");
Serial.println(average);
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
Serial.print("Thermistor resistance ");
Serial.println(average);
float steinhart;
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
Serial.print("Temperature ");
Serial.print(steinhart);
Serial.println(" *C");
temperatura= steinhart;
controla_rele();
delay(1000);
}
void print_datetime(){
// Start printing elements as individuals
Serial.print("Current Date / Time: ");
Serial.print(myRTC.dayofmonth);
Serial.print("/");
Serial.print(myRTC.month);
Serial.print("/");
Serial.print(myRTC.year);
Serial.print(" ");
Serial.print(myRTC.hours);
Serial.print(":");
Serial.print(myRTC.minutes);
Serial.print(":");
Serial.println(myRTC.seconds);
// Delay so the program doesn't print non-stop
}
void controla_rele(){
if (temperatura > limite_temperatura) {
// turn LED on:
digitalWrite(relepin, LOW);
Serial.println("RELE OFF"); //bomba conectada
}
else {
// turn LED off:
digitalWrite(relepin, HIGH);
Serial.println("RELE ON"); //bomba desconectada
}
/*
if (myRTC.hours > 10) {
// turn LED on:
digitalWrite(relepin, LOW);
Serial.println("RELE OFF");
}
else {
// turn LED off:
digitalWrite(relepin, HIGH);
Serial.println("RELE ON");
}
*/
}