#include <LiquidCrystal_I2C.h>
#include <OneButton.h>
#include <DHT.h>
#include <HCSR04.h>
#define LED_PIN 9
#define PHOTO_PIN A0
#define BTN_PIN 4
#define DHTPIN 7
#define DHTTYPE DHT11
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define LCD_ADDR 0x27
enum AppState {BOOT, DHT_STATE, LUM_DIST, CALIBRATION};
AppState currentState = BOOT;
// Définir les variables globales
unsigned long currentTime;
LiquidCrystal_I2C lcd(LCD_ADDR,16,2);
OneButton button(BTN_PIN);
DHT dht(DHTPIN, DHTTYPE);
HCSR04 hc(TRIGGER_PIN, ECHO_PIN);
void setup() {
Serial.begin(9600);
initPins();
lcdInit();
buttonInit();
dht.begin();
}
void initPins() {
pinMode(LED_PIN, OUTPUT);
}
void lcdInit() {
lcd.init(); // initialize the lcd
lcd.backlight();
}
void buttonInit() {
// Faire le code pour l'initialisation du bouton
}
void loop() {
currentTime = millis();
switch(currentState) {
case BOOT:
bootState();
break;
case DHT_STATE:
dhtState();
break;
}
readSomeDataTask();
otherTask();
}
// Représente une tâche quelconque qui doit être exéctué de façon continue
void readSomeDataTask(){
static unsigned long lastTime = 0;
const int rate = 3000;
if (currentTime - lastTime > rate) {
lastTime = currentTime;
// Exemple de tâche
// Lire distance
}
}
void otherTask(){
static unsigned long lastTime = 0;
const int rate = 1000;
if (currentTime - lastTime > rate) {
lastTime = currentTime;
// Exemple de tâche
// Lire distance
}
}
void bootState() {
static unsigned long lastTime = 0;
const int exitTime = 3000;
const int lcdRate = 250;
if (currentTime - lastTime > lcdRate )
{
lastTime = currentTime;
lcd.clear();
lcd.setCursor(0,0);
// Écrire numéro étudiant
lcd.print("Etd 1: 1234567");
lcd.setCursor(0,1);
lcd.print("Etd 2: 7654321");
}
if (currentTime < exitTime) return;
currentState = DHT_STATE;
}
void dhtState() {
static unsigned long lastTime = 0;
const int lcdRate = 250;
if (currentTime - lastTime > lcdRate )
{
lastTime = currentTime;
lcd.clear();
lcd.setCursor(0,0);
// Écrire numéro étudiant
lcd.print("YOLO!!");
lcd.setCursor(0,1);
lcd.print(currentTime);
}
}