/* =====================================
TEMPORISATION POUR RELAIS PAR TINY85
version 20251127
====================================
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Ce code est distribué gratuitement sous licence !
! Creativ Commons CC-BY-NC-ND 3.0 !
! !
! Cette licence permet à d'autres utilisateurs d'utiliser ce code !
! à des fins non commerciales, dans la mesure où le nom de l'auteur est !
! mentionné. Merci de m'informer de toute modification du code ce qui !
! me permettra de continuer à apprendre. !
! !
! auteur Philippe GUENET - [email protected] - https://wgnt-train.fr !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
RESET / PB5 * * VCC
PB3 * * PB2/ADC1/INT0/SCL
PB4 * * PB1/ADC1
GND * * PB0/ADC2/SDA
La sortie commandera un relais via un montage "Paire de Sziklai"
*/
#include <Bounce2.h>
// ------------------------
// CONFIGURATION DES PINS
// ------------------------
const int pinDetection = PB0; // PB0 = pin 5
const int pinSortie = PB1; // PB1 = pin 6
const int pinPot = A1; // PB2 = pin 7 (ADC1)
// ------------------------
// OBJET BOUNCE2
// ------------------------
Bounce detection = Bounce();
// ------------------------
// VARIABLES TEMPO
// ------------------------
unsigned long tempoStart = 0;
unsigned long tempoDuree = 0;
unsigned long tempoMini = 6000UL; // divisée par 10 pour les tests
unsigned long tempoMaxi = 60000UL; // divisée par 10 pour les tests
bool tempoEnCours = false;
// ------------------------
// MACHINE A ETATS
// ------------------------
enum etat {
ATTENTE,
TEMPO
};
etat etatDetection = ATTENTE;
void setup() {
pinMode(pinSortie, OUTPUT);
digitalWrite(pinSortie, LOW);
detection.attach(pinDetection, INPUT_PULLUP);
detection.interval(10); // 10 ms de debounce
} // Fin de setup()
void loop() {
// Mise à jour de l'état du dédecteur de présence
detection.update();
switch (etatDetection) {
case ATTENTE:
digitalWrite(pinSortie, LOW);
tempoEnCours = false;
// Lecture du potentiomètre : 1 à 10 minutes (déclenchement à l'appui)
if (detection.fell()) {
int potValue = analogRead(pinPot);
tempoDuree = map(potValue, 0, 1023, tempoMini, tempoMaxi);
tempoStart = millis();
tempoEnCours = true;
digitalWrite(pinSortie, HIGH); // LED ON = tempo active
etatDetection = TEMPO; // changement d'état
} // Fin de if (detection.fell()) {
break;
case TEMPO:
// Lorsque le temps est écoulé
if (millis() - tempoStart >= tempoDuree) {
digitalWrite(pinSortie, LOW); // LED OFF = tempo terminée
tempoEnCours = false;
etatDetection = ATTENTE; // retour à l'état d'attente
} // Fin de if (millis() - tempoStart >= tempoDuree)
break;
} // Fin de switch()
} // Fin de loop()