Hello all, I read on the internet that math with millis()
need to put UL
when doing calculation, that what I tried to use in my code.
So each time ร run the code (arduino UNO), the number 1 is automaticaly put inside the tpsInsolation
UL variable. So, my program is ended after 1 second only, instead of 60 000 ms (variable declaration at the beginning of the code)
Curiosly, when, In the loop()
, I replace tpsInsolation
by for exemple 10 000
, the program works...
Need help please because I tried many things without result...
/*pin assignรฉ
Relai : 2
bouton vert 3:
bouton rouge 4:
buzzer : 5
LCD : I2C
*/
#include <Arduino.h>
#include <Wire.h> //pour la communication I2C (SD, ATH20,LCD)
#include "OneButton.h" // bibliotheque qui gรจre l'รฉvenement des buttons
#include "rgb_lcd.h"
rgb_lcd lcd; // pour le LCD
const char symboleE = B11011111;//symbole รฉ
const byte relai = 2;
const byte buzzer = 5;
// Setup a new OneButton on pin A1.
OneButton bpVert(4, true);
// Setup a new OneButton on pin A2.
OneButton bpRouge(3, true);
unsigned long tpsIncrementation = 1UL * 60000; // en millisecondes pour l'incrementation du tps d'insolation, mn/mn
unsigned long tpsIncrementation2 = 5UL * 60000; // pour incrementation avec bouton stop
unsigned long tpsInsolation = 1UL*60000; //en millisecondes, initialisรฉ ร 1mn
unsigned long tpsInsolationMax = 60UL*60000; // 1h d'insolation
unsigned long tpsEcoule = 0;
unsigned long dcy = 0;
bool cycleEnCours = false;
bool finDeCycle = false ;
bool validationFinDeCycle = false;
//ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง
//ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง SETUP ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง
void setup() {
Serial.begin(9600);
while (!Serial) {};// attente du demarrage port serie
lcd.begin(16, 2);// initialisation nb libre et colonne du lcd
lcd.clear();
delay(50);
pinMode(relai, OUTPUT);
digitalWrite(relai, LOW);
delay(50);
pinMode(buzzer, OUTPUT);
digitalWrite(relai, LOW);
delay(50);
bpVert.attachClick(incrementation); // lancement de la fonction
bpVert.attachLongPressStart(dcyInsolation); //lancement de la fonction insolation
bpRouge.attachClick(arretInsolation);
}// fin de la fonction setup
//ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง
//ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง LOOP ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง
void loop() {
// keep watching the push buttons:
bpVert.tick();
bpRouge.tick();
//delay(10);
if (cycleEnCours == true){
tpsEcoule = 1UL*(millis()-dcy);
if(tpsEcoule > tpsInsolation) {
//cycleEnCours = false;
arretInsolation();
}
}
affichageLcd();
/*Serial.print("Cycle en cours:");
Serial.println(cycleEnCours);
Serial.print("tpsEcoule:");
Serial.println(tpsEcoule/1000);
Serial.print("tpsInsolation = ");
Serial.println(tpsInsolation);
//Serial.print("Millis:");
//Serial.println(millis());
//Serial.print("tpsInsolation + millis = ");
//unsigned long tps = millis() + tpsInsolation;
//Serial.println(tps);
Serial.print("dcy:");
Serial.println(dcy);
Serial.println();
delay(2000);*/
}// fin du loop
//ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง
//ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง DCY INSOLATION ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง
void dcyInsolation()
{
if(tpsInsolation =! 0 && cycleEnCours == false) {
digitalWrite(relai,HIGH);
dcy = millis();
lcd.clear();
buzzerDcy();
cycleEnCours = true;
finDeCycle = false ;
}
}// fin de la fonction
//ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง
//ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง ARRET INSOLATION ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง
void arretInsolation() {
if(cycleEnCours == true) {
cycleEnCours = false;
digitalWrite(relai,LOW);
buzzerFin();
finDeCycle = true;
}
else{
tpsInsolation = 1UL*(tpsInsolation + tpsIncrementation2); // incrementation de 5mn/5mn si on est en dehors d'un cycle
}
if (tpsInsolation > tpsInsolationMax) tpsInsolation = 60000;
/*Serial.println(tpsIncrementation);
Serial.println(tpsInsolation);
Serial.print(tpsInsolation/60000);
Serial.println("mn");
Serial.println();*/
}// fin de la fonction DHT
//ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง
//ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง INCREMENTATION ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง
void incrementation(){
if (cycleEnCours == false) {
tpsInsolation = 1UL*(tpsInsolation+tpsIncrementation); // incrementation de la variable "incrementation"
if (finDeCycle == true) finDeCycle = false;
}
if (tpsInsolation > tpsInsolationMax) tpsInsolation = 60000;
}// fin de la fonction
//ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง
//ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง LCD ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง
void affichageLcd()
{
//static bool changementEtatCycle = false;
static unsigned long tpsInsolationLcdMn = 0;
static unsigned long tpsRestantLcdS = 0;
tpsInsolationLcdMn = 1UL*(tpsInsolation/60000);
tpsRestantLcdS = 1UL*((tpsInsolation-tpsEcoule)/1000);
if (cycleEnCours==false && finDeCycle == false) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Tps insolation:");//5 char
lcd.setCursor(1, 1);
lcd.print(" ");//
lcd.setCursor(0, 1);
lcd.print(tpsInsolationLcdMn);//
lcd.setCursor(3, 1);
lcd.print("mn (max 60)");//
}
if (cycleEnCours==true){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Cycle en cours...");//5 char
lcd.setCursor(0, 1);
lcd.print("Reste :");//
lcd.setCursor(8, 1);
lcd.print(" ");//
lcd.setCursor(7, 1);
lcd.print(tpsRestantLcdS);//
lcd.setCursor(12, 1);
lcd.print("s");//
}
if (finDeCycle == true){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Insolation");
lcd.setCursor(0, 1);
lcd.print("fini !");
}
}
//ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง
//ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง BUZZER DCY ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง
void buzzerDcy()
{
for(int i = 0 ; i<3; i++){
tone (buzzer, 1100);
delay(500);
noTone(buzzer);
delay(200);
}
}
//ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง
//ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง BUZZER FIN ยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยงยง
void buzzerFin()
{
for(int i = 0 ; i<3; i++){
tone (buzzer, 1100);
delay(2000);
noTone(buzzer);
delay(1000);
}
}