Dunque, sono riuscito ad ottenere ciò che volevo ma ho ancora un'altro problema.
Nel momento in cui faccio il reset della board il buzzer non suona se non prima di arrivare al valore di snoozeDuration. E' chiaro che non ho intenzione di definirlo a 10 secondi ma a circa 5 minuti percui, significherebbe dover attendere questo tempo prima che il buzzer inizi a suonare al primo avvio e ciò non è normale.
Come potrei risolvere?
#include <OneWire.h>
#include <DallasTemperature.h>
#define Temp_Max_Stored 24.70
#define led_red D5
#define led_green D6
#define ONE_WIRE_BUS D4 // Data wire is plugged to arduino pin
#define buzzer D7 // buzzer to arduino pin D7
#define snoozeDuration 10000 // attesa 5 minuti (1000 millisecondi equivalgono ad un secondo)
#define pauseBetweenNotes 1000
#define noteDuration 1000
#define relay_pin D0
float tempDALLAS;
unsigned long currentMillisAlarm = 0;
unsigned long currentMillisSilenzia = 0;
unsigned long previousMillisAlarm = 0;
unsigned long previousMillisSilenzia = 0;
boolean outputTone = false; // Records current state
bool snoozed = false;
bool silenzia = false;
OneWire oneWire1(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire1);
unsigned long t = 0;
void setup()
{
Serial.begin(115200);
// set up LEDs
pinMode (led_red, OUTPUT); digitalWrite(led_red, LOW);
pinMode (led_green, OUTPUT); digitalWrite(led_green, LOW);
pinMode (relay_pin, OUTPUT); digitalWrite(relay_pin, HIGH);
Serial.println(""); Serial.println("ArduAcquario MALAWI ");
}
void temperature()
{
if (millis() - t > 5000) // Una volta ogni 5 secondi:
{
t = millis();
sensors.requestTemperatures();
tempDALLAS = sensors.getTempCByIndex(0); // Read temperature of Dallas sensor
}
}
void loop()
{
temperature();
while (tempDALLAS > Temp_Max_Stored)
{
Serial.println("================= ALARM TIME! ================");
Serial.print ("Temp_Max_Stored = "); Serial.println (Temp_Max_Stored);
temperature();
digitalWrite (led_green, LOW); //set the LED off
digitalWrite (led_red, HIGH); //set the LED on
Serial.println(); Serial.print ("Temperatura acqua troppo ALTA! ");
Serial.println(tempDALLAS); Serial.println();
currentMillisSilenzia = millis();
int Value = analogRead(0);
int Btn = 0;
if (Value > 5 && Value < 20) Btn = 1; //enter
if (Value > 240 && Value < 300) Btn = 2; //up
if (Value > 400 && Value < 450) Btn = 3; //down
if (Value > 500 && Value < 600) Btn = 4; //back
if (Btn >= 1 && Btn <= 3)
{
previousMillisSilenzia = currentMillisSilenzia;
silenzia = false;
noTone(buzzer);
}
else silenzia = true;
allarme();
digitalWrite(relay_pin, LOW);
delay(500);
Serial.println(); Serial.println();
Serial.println(currentMillisSilenzia - previousMillisSilenzia);
Serial.println(snoozeDuration);
}
digitalWrite (led_green, HIGH ); //set the LED off
digitalWrite (led_red, LOW); //set the LED on
Serial.println("==============================================");
Serial.print("Temperatura acqua coretta ");
Serial.println(tempDALLAS);
digitalWrite(relay_pin, HIGH);
noTone(buzzer);
}
void allarme()
{
if (currentMillisSilenzia - previousMillisSilenzia >= snoozeDuration)
{
silenzia = false;
}
else
{
silenzia = true;
}
if (!silenzia) {
currentMillisAlarm = millis();
if (outputTone)
{
if (currentMillisAlarm - previousMillisAlarm >= noteDuration)
{
previousMillisAlarm = currentMillisAlarm;
noTone(buzzer);
outputTone = false;
}
} // END if (outputTone)
else
{
// We are currently in a pause
// Check if it's been long enough and turn on if so
if (currentMillisAlarm - previousMillisAlarm >= pauseBetweenNotes)
{
previousMillisAlarm = currentMillisAlarm;
tone(buzzer, 1, 1000);
outputTone = true;
}
} // END else
} // END if silenzia
} // END allarme()