E' così?
unsigned long t=0 lo dichiarerei globalmente all'inizio.
Al posto delle const puoi usare dei #define, che sostituiscono i valori al momento della compilazione:
#include <OneWire.h>
#include <DallasTemperature.h>
#define Temp_Max_Stored 25.70
#define led_red D5
#define led_green D6
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS D4
#define buzzer D7 // buzzer to arduino pin D7
#define snoozeDuration 30000 // attesa 5 minuti (1000 millisecondi equivalgono ad un secondo)
#define pauseBetweenNotes 1000
#define noteDuration 1000
#define relay_pin D0
float tempDALLAS;
unsigned long previousMillisAllarm = 0;
unsigned long previousMillisSilenzia = 0;
boolean outputTone = false; // Records current state
bool snoozed = false;
bool muto = 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("================= ALLARM 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();
allarm();
digitalWrite(relay_pin, LOW);
delay(500);
}
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 silenzia()
{
unsigned long currentMillis = 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)
{
if (currentMillis - previousMillisSilenzia < snoozeDuration)
{
noTone(buzzer);
muto = true;
Serial.println(); Serial.println();
Serial.println(currentMillis - previousMillisSilenzia);
Serial.println(snoozeDuration);
previousMillisSilenzia = currentMillis;
Serial.print("******************** IF SILENZIA******************** ");
}
else
{
Serial.print("button = ");
Serial.println(Btn);
Serial.println ("BUTTON PRESSED - ALLARM SNOOZED");
muto = false;
Serial.print("******************** ELSE SILENZIA******************** ");
}
}
/*
else if (Btn=4)
{
//AllertBuzzer = false;
Serial.println("===========================================");
Serial.println("====== TASTO BACK - TACITA PREMUTO ========");
Serial.println("===========================================");
}
*/
}
void allarm()
{
silenzia();
if (!muto)
{
unsigned long currentMillis = millis();
if (outputTone)
{
// We are currently outputting a tone
// Check if it's been long enough and turn off if so
if (currentMillis - previousMillisAllarm >= noteDuration)
{
previousMillisAllarm = currentMillis;
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 (currentMillis - previousMillisAllarm >= pauseBetweenNotes)
{
previousMillisAllarm = currentMillis;
tone(buzzer, 1000);
outputTone = true;
}
} // END else
} // END if (outputTone)
} // END allarm()
P.s.: Allarme in inglese è Alarm, con una "l" sola ![]()