Salve a tutti,
sto facendo un sketch, che nelle intenzioni, dovrebbe permettere la nascita di pulcini.
Quindi ci sono 2 relays, 1 rtc, 1 sensore di temperatura, 1 pulsante.
Stringendo questo sketch fa:
- legge la temperatura e accende o spegne la resistenza in base ad essa
- ogni ora gira le uova
Purtroppo dopo qualche ora si blocca e non da segni di vita finchè non si resetta.
Questo è il codice:
#include <Wire.h>
#include "RTClib.h"
const int LM35_PIN = 1;// Numero del pin analogico sul quale è collegato l'LM35
const float vref = 1.1;// Vref dell'ADC (quell'interno è di 1,1V)
const int relayEggsRotatioPin = 2;
const int relayTemperaturePin = 3;
const int BUTTON_ROTATION_EGG_PIN = 7;
//attiva il debug
boolean blnDebug=true;
//date time
RTC_DS1307 RTC;
DateTime now;
int countDelayPrintDate;
int countDelayPrintTemperature;
boolean blnScheduleCommand=false;
//Temperature
float tempStart=37.7;
float tempEnd=38;
int sample_count;
//eggs rotation
int eggsRotation=0;
//void(* resetFunc) (void) = 0;//declare reset function at address 0
void setup()
{
Serial.begin(9600); // start serial communication
//define pin 7 input
pinMode(BUTTON_ROTATION_EGG_PIN, INPUT);
//define i pin 2 3 out
pinMode(relayEggsRotatioPin, OUTPUT);
pinMode(relayTemperaturePin, OUTPUT);
//beginning i le uscite
digitalWrite(relayEggsRotatioPin, LOW);
digitalWrite(relayTemperaturePin, LOW);
// Start rtc
Wire.begin();
RTC.begin();
//RTC.adjust(DateTime(__DATE__, __TIME__));
//read temperature
analogReference(INTERNAL);
debug("End setup");
}
void loop()
{
printDate();
checkTemperature();
scheduleCommand();
eggs();
}
void eggs(){
eggsRotation=digitalRead(BUTTON_ROTATION_EGG_PIN);
if (eggsRotation == HIGH) {
// debug("eggs rotation on");
digitalWrite(relayEggsRotatioPin, HIGH);
}
else if (eggsRotation == LOW && !blnScheduleCommand) {
// debug("eggs rotation off");
digitalWrite(relayEggsRotatioPin, LOW);
}
}
/* Legge la temperatura */
float readTemp()
{
float temp = 0.0; // valore convertito in temperatura (°C)
int val = 0; // valore quantizzato dall'ADC [0..1023]
int nread = 8; // numero di letture (da 5 a 8)
float somma = 0.0; // somma delle letture
for (int i=0; i<nread; i++)
{
val = analogRead(LM35_PIN); // legge il dato della tensione sul pin 'LM35_pin'
temp = (100.0 * vref * val)/1024.0; // lo converte in °C
somma += temp; // aggiunge alla somma delle temperature lette
}
return ( somma / nread ); // ne calcola il valore medio
}
void checkTemperature(){
float temperature=readTemp();
countDelayPrintTemperature++;
if(countDelayPrintTemperature>600){
Serial.print(temperature,2);
Serial.println(" C");
}
if(temperature<tempStart){
//on
digitalWrite(relayTemperaturePin, HIGH);
if(countDelayPrintTemperature>600){
debug("resistence on");
}
}
if(temperature>tempEnd){
//off
digitalWrite(relayTemperaturePin, LOW);
if(countDelayPrintTemperature>600){
debug("resistence off");
}
}
if(countDelayPrintTemperature>600){
countDelayPrintTemperature=0;
}
}
void debug(String s){
if(blnDebug)
Serial.println(s);
}
void printDate(){
now = RTC.now();
if(blnDebug){
countDelayPrintDate++;
if(countDelayPrintDate>600){
// DateTime now = RTC.now();
//We print the day
Serial.print(now.day(), DEC);
Serial.print('/');
//We print the month
Serial.print(now.month(), DEC);
Serial.print('/');
//We print the year
Serial.print(now.year(), DEC);
Serial.print(' ');
//We print the hour
Serial.print(now.hour(), DEC);
Serial.print(':');
//We print the minutes
Serial.print(now.minute(), DEC);
Serial.print(':');
//We print the seconds
Serial.print(now.second(), DEC);
debug("");
countDelayPrintDate=0;
}
}
}
void scheduleCommand(){
//Faccio il reset ogni ora
/* if(now.minute()==1){
debug("Every 1 minute.");
debug("reset");
resetFunc();
debug("fine reset");
}
*/
if(now.minute()==30 && now.second()==0 && !blnScheduleCommand){
blnScheduleCommand=true;
digitalWrite(relayEggsRotatioPin, HIGH);
debug("Every 1 hour turn eggs.");
}
if(now.minute()==30 && now.second()==7 && blnScheduleCommand){
blnScheduleCommand=false;
digitalWrite(relayEggsRotatioPin, LOW);
debug("Stop turn eggs.");
}
/*if(now.hour()>=r1HourStart && now.hour()<r1HourEnd){
if(!blnScheduleCommand){
blnScheduleCommand=true;
processCommand(CONST_R1_ON);
debug("Start "+ String(r1HourStart) + " hour.");
}
}
else{
if(blnScheduleCommand){
blnScheduleCommand=false;
processCommand(CONST_R1_OFF);
debug("Stop "+String(r1HourEnd)+" hour.");
}
}
*/
}
Ho pensato che si poteva saturare la memoria in qualche modo ma non vedo come.
Comunque sto rinunciando all'idea di usare arduino e sto comprando un termostato elettronico.
Purtroppo ho adsl che non funziona da 3 settimane a causa di un fulmine quindi non sarò spesso online.
Grazie