Files
Presentations/arduino_proto_threads/tp_serre/tp_serre.ino
T
2026-10-24 04:20:07 +11:00

165 lines
5.2 KiB
Arduino
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* Si la température est < CMINT°C, un cycle de chauffe.
Si la température > CMAXT °C, le ventilateur tourne en proportion.
Si l’Humidité < CMINH %, un cycle arrosage.
Si l’Humidité >CMAXH %, le ventilateur tourne en proportion
*/
#define CMINT 15 // consigne basse
#define CMAXT 25 // Consigne haute
#define CMINH 40 // consigne basse
#define CMAXH 50 // Consigne haute
#define TMIN 0 // echelle basse
#define TMAX 50 // echelle haute
#define HMIN 20 // echelle basse
#define HMAX 80 // echelle haute
#define MAXCYCLE 10 // nb max de cycles arrosage
#define JAUNE 13 // chauffe
#define BLEU 12 // arrosage
#define ROUGE 11 // ventilation
#define VERTE 9 // niveau
#define APVCC 2 // #define MAXCYCLE 500 // nb max de cycles+5V
#define APGND 6 // 0V
#define AP_H A2 // potard humidité
#define AP_T A1 // potard température
#define IHM
#include "serialTrace.h" // infos / trace to serial monitor
#include "pt.h"
static struct pt ptT, ptH, ptV, ptC, ptE, ptN, ptA; // proto threads
int temp=0; int humi=0;
bool alarme = false; int nb_cycles = 0;
void log_acqui(char * msg, int va);
void ledPWM(int led, int ratio) {
int val=constrain(ratio,0,254); // limit to values [0..254]
analogWrite(led, val);
}
int acquis(int broche, int vmin, int vmax) {
// WIP
int val ;
val=analogRead(broche); // 1024 pts, 0 < U < 5V ==> 0 < val < 1023
return map (val, 0, 1023, vmin, vmax ) ;
}
static int ptTemp(struct pt *pt, int repit) {
static unsigned long ttl = 0;
PT_BEGIN(pt);
while(1) {
ttl = millis() + (repit*1000) ;
PT_WAIT_UNTIL(pt, millis() > ttl);
temp = acquis(AP_T, TMIN, TMAX);
ttyPutInt ("temp: ", temp);
}
PT_END(pt);
}
static int ptHumi(struct pt *pt, int repit) {
static unsigned long ttl = 0;
PT_BEGIN(pt);
while(1) {
ttl = millis() + (repit*1000);
PT_WAIT_UNTIL(pt, millis() > ttl);
humi = acquis(AP_H, HMIN, HMAX);
ttyPutInt ("humi: ",humi);
}
PT_END(pt);
}
static int ptAlarme (struct pt *pt, int duree){
static unsigned long ttl;
PT_BEGIN(pt);
while (alarme) {
ttl = millis() + duree;
PT_WAIT_UNTIL(pt, millis() > ttl);
digitalWrite(VERTE, !digitalRead(VERTE));
}
PT_END(pt);
}
static int ptNiveau(struct pt *pt){
static int nb_old = 0; int ratio;
PT_BEGIN(pt);
while (!alarme) {
PT_WAIT_UNTIL(pt, nb_cycles > nb_old ); // attente nouvel arrosage
ratio = map(nb_cycles, 0, MAXCYCLE, 254, 0);
ledPWM(VERTE, ratio);
ttyPutInt ("Niveau: ",ratio);
nb_old = nb_cycles;
}
PT_END(pt);
}
static int ptChauffe(struct pt *pt, int actif, int inactif ){
static unsigned long ttl = 0;
PT_BEGIN(pt);
while (1) {
PT_WAIT_UNTIL(pt, temp < CMINT);
digitalWrite(JAUNE, HIGH);
ttl = millis() + (actif*1000) ;
PT_WAIT_UNTIL (pt, millis() > ttl);
digitalWrite(JAUNE, LOW);
ttl = millis() + (inactif*1000) ;
PT_WAIT_UNTIL (pt, millis() > ttl);
}
PT_END(pt);
} // Chauffe
static int ptEau(struct pt *pt, int actif, int inactif ) {
static unsigned long ttl = 0;
PT_BEGIN(pt);
while (!alarme) {
PT_WAIT_UNTIL(pt, humi < CMINH);
digitalWrite(BLEU, HIGH);
ttl = millis() + (actif*1000) ;
PT_WAIT_UNTIL (pt, millis() > ttl);
digitalWrite(BLEU, LOW);
nb_cycles++;
alarme = ( nb_cycles >= MAXCYCLE);
ttyPutInt ("Cycle: ",nb_cycles);
ttl = millis() + (inactif*1000) ;
PT_WAIT_UNTIL (pt, millis() > ttl);
}
PT_END(pt);
} // Eau
int vRatio(){
int t_ratio=0, h_ratio = 0, i = 0 ;
// On pilote la led sur la plage utile la plus petite
if (temp > CMAXT) t_ratio = map(temp, CMAXT, TMAX, 0,254);
if (humi > CMAXH) h_ratio = map(humi, CMAXH, HMAX, 0,254);
return max(t_ratio, h_ratio );
}
static int ptVenti(struct pt *pt) {
static int temp_old = 0, humi_old = 0;
PT_BEGIN(pt);
while (1) {
PT_WAIT_UNTIL(pt, temp > CMAXT || humi > CMAXH);
temp_old = temp;
humi_old = humi ;
ledPWM(ROUGE, vRatio()) ; // calcul du ration selon les mesures
PT_WAIT_UNTIL(pt, temp != temp_old || humi != humi_old );
ledPWM(ROUGE, vRatio()) ;
}
PT_END(pt);
} // Ventilateur
void initIO() {
pinMode(ROUGE, OUTPUT); analogWrite(ROUGE, 0);
pinMode(JAUNE, OUTPUT); pinMode(BLEU, OUTPUT);
digitalWrite(JAUNE, LOW); digitalWrite(BLEU, LOW) ;
pinMode(VERTE, OUTPUT); analogWrite(VERTE, 254); // max
pinMode(APVCC, OUTPUT); pinMode(APGND, OUTPUT);
digitalWrite(APVCC, HIGH); digitalWrite(APGND, LOW);
}
void setup() {
initIO(); // Setup: set LEDs & pots
// static struct pt ptT, ptH, ptV, ptC, ptE, ptN, ptA ;
PT_INIT(&ptT); PT_INIT(&ptH); PT_INIT(&ptV); PT_INIT(&ptC);
PT_INIT(&ptE); PT_INIT(&ptN); PT_INIT(&ptA);
ttySetIHM(9600);
temp = acquis(AP_T, TMIN, TMAX); // acquis potard T°
humi = acquis(AP_H, HMIN, HMAX); // acquis potard H%
}
void loop() {
ptTemp(&ptT, 4); // acquisition t°chaque 5sec
ptHumi(&ptH, 6); // acquisition % chaque 10sec
ptVenti(&ptV); // ventilation
ptChauffe(&ptC,5,10); // chauffage 5s, inactivité 10s
ptEau(&ptE,3,10); // arrosage 3s, inactivité 40s
ptNiveau(&ptN); // niveau d’eau, PWM Verte
ptAlarme(&ptA,500); // réserve vide, clignote à 1Hz
}