Buongiorno,
ho nuovamente bisogno del vostro aiuto perchè non riesco a venirne a capo.
Ho provato diverse soluzioni ma non riesco ad annidare i cicli in modo da silenziare il buzzer.
Con la soluzione che posto di seguito riesco a mettere in silenzio il buzzer però non rispetta i tempi, è come se non tenesse in considerazione la mia variabile snoozeDuration e resta muto perennemente.
Vi sarei inoltre grato se riusciste a snellire anche il codice.
Grazie.
#include <OneWire.h>
#include <DallasTemperature.h>
double Temp_Max_Stored = 25.70;
int led_red = D5;
int led_green = D6;
// Data wire is plugged into port 2 on the Arduino
const int ONE_WIRE_BUS = D4;
float tempDALLAS;
const int buzzer = D7; //buzzer to arduino pin D7
uint8_t relay_pin = D0;
unsigned long previousMillisAllarm = 0;
unsigned long previousMillisSilenzia = 0;
const long snoozeDuration = 30000; // attesa 5 minuti (1000 millisecondi equivalgono ad un secondo)
const long pauseBetweenNotes = 1000;
const long noteDuration = 1000;
boolean outputTone = false; // Records current state
bool snoozed = false;
bool muto = false;
OneWire oneWire1(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire1);
void setup() {
// put your setup code here, to run once:
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()
{
unsigned long t;
if (millis() - t > 5000) // Una volta al secondo:
{
t = millis();
sensors.requestTemperatures();
tempDALLAS = sensors.getTempCByIndex(0); // Read temperature of Dallas sensor
}
}
void loop() {
// put your main code here, to run repeatedly:
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;
}
}
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;
}
}
}
}