DD2-monitor/simulator/rdtemp/rdtemp.ino

145 lines
3.2 KiB
C++

/*
* Phytotron
* lecture des capteurs de temperature LM35
*/
/* -------------------------------------------------- */
#define NBVAL 4
#define DELAI 1000
#define RELAIS_FRIGO 42
#define RELAIS_VENTILO 40
#define prt(a) Serial.print(a)
#define prtln(a) Serial.println(a)
typedef struct {
unsigned short magic;
char tag[4];
short delai;
short temp_maxi;
short temp_mini;
short control;
} Global;
Global parametres;
/* -------------------------------------------------- */
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(RELAIS_FRIGO, OUTPUT);
pinMode(RELAIS_VENTILO, OUTPUT);
Serial.print("\n");
/* XXX */
/* changing the voltage reference of the ADC
* greatly increase the prcision on the limited
* range of our temperatures.
*/
analogReference(INTERNAL1V1); // Pour Arduino Mega2560
Serial.print("\n");
read_config(0, &parametres);
// display_config(&parametres);
Serial.print("M running now\n");
}
/* ================================================== */
/*
* the light captor is on A4 pin
*/
void readlight(void)
{
short value;
delay(100);
value = analogRead(A4);
Serial.print("L ");
Serial.print(value);
Serial.print('\n');
}
/* ================================================== */
short adc_pins[] = { A0, A1, A2, A3 };
#define NB_PASSE 4
/* -------------------------------------------------- */
void updatevalues(short *ptr)
{
short foo, pass;
for (foo=0; foo<NBVAL; foo++) { ptr[foo] = 0; }
digitalWrite(LED_BUILTIN, HIGH);
for (pass=0; pass<NB_PASSE; pass++) {
for (foo=0; foo<NBVAL; foo++) {
ptr[foo] += analogRead(adc_pins[foo]);
delay(50);
}
}
for (foo=0; foo<NBVAL; foo++) { ptr[foo] /= NB_PASSE; }
digitalWrite(LED_BUILTIN, LOW);
}
/* -------------------------------------------------- */
void sendvalues(short *ptr)
{
int foo;
Serial.print("T");
for (foo=0; foo<NBVAL; foo++) {
Serial.print(" ");
Serial.print(ptr[foo]);
}
Serial.print('\n');
}
/* -------------------------------------------------- */
void update_and_send(void)
{
short values[NBVAL];
updatevalues(values);
sendvalues(values);
}
/* ================================================== */
void loop() {
static int foo = 0;
int key;
update_and_send();
if (foo++ > 5) {
readlight(); foo = 0;
}
/* check for external request */
if (Serial.available()) {
key = Serial.read();
switch (key) {
case 'U':
Serial.println("M cli request");
phytocli();
break;
case '+': /* allume le frigo */
controle_frigo(1);
break;
case '-': /* eteint le frigo */
controle_frigo(0);
break;
case '1': /* allume le ventilo */
controle_ventilo(1);
break;
case '0': /* eteint le ventilo */
controle_ventilo(0);
break;
default:
prtln("M bad control code");
break;
}
}
delay(DELAI);
/* ETERNAL LOOP END HERE */
}
/* -------------------------------------------------- */