Hi
Can I get some help with this please, I have got the sketch to compile and everything runs fine, except the serial clock seems to reset after 4 minutes.
I’ve run out of ideas of what it could be in the sketch.
It needs to run as a 24hr clock so the 2 time alarms kick into action but as the system resets after 5 minutes & only the 1st alarm is ever activated.
I’ve tried
Serial.println (F("Aquaduino System Starting"));
But it still resets at exactly 5 minutes
The code below has been stripped of some pins and if statements to reverse where the problem maybe, but it still appears and I cant really go back anymore.
Thanks in advance
//Define PIN's
#include <Time.h>
#include <TimeAlarms.h>
#define dht_dpin A4
#define ventilationPin 9
#define solenoidPin 8
#define relaylights 1
#define waterpump 2
#define airpump 3
#define outletballvalveopen 18
#define outletballvalveclosed 17
#define inletballvalve 5
#define c02valve 6
#define weeweevalve 7
#define airwaterjet 12
#define tanklights 16
#define DHTTYPE DHT11
byte compFunc; //for passing error code back from complex functions.
byte dht_dat[4]; //Array to hold the bytes sent from sensor.
void setup()
{
Serial.begin(9600);
Serial.println(F("_________AquaDuino by ArkAnArky_________"));
pinMode(ventilationPin, OUTPUT);
pinMode(solenoidPin, OUTPUT);
pinMode(relaylights, OUTPUT);
pinMode(waterpump, OUTPUT);
pinMode(airpump, OUTPUT);
pinMode(outletballvalveopen, OUTPUT);
pinMode(outletballvalveclosed, OUTPUT);
pinMode(inletballvalve, OUTPUT);
pinMode(c02valve, OUTPUT);
pinMode(weeweevalve, OUTPUT);
pinMode(airwaterjet, OUTPUT);
pinMode(tanklights, OUTPUT);
setTime(05,59,30,1,07,13); //
Alarm.alarmRepeat(06,00,00, MorningAlarm); // 6:00am every day
Alarm.alarmRepeat(18,00,00,EveningAlarm); // 6:00pm every day
delay(300); //Must wait one second for DHT chip to stabilize. This is
//the first part 0.3 seconds.
delay(700); //rest of 1 second. Second part 0.7 seconds/
}
void loop(){
InitDHT(); //Initializing for reading DHT
//measuring temperature & humidity.
ReadDHT(); //Read from DHT chip
switch (compFunc){
case 0:
Serial.print(dht_dat[0], DEC);
Serial.println (F("% Humidity "));
Serial.println (F("Temperature = "));
Serial.print(dht_dat[2], DEC);
Serial.println (F("*C "));
digitalClockDisplay();
Alarm.delay(20000); // wait one second between clock display
break;
case 1:
Serial.println (F("Error 1: DHT start condition 1 not met."));
break;
case 2:
Serial.println (F("Error 2: DHT start condition 2 not met."));
break;
case 3:
Serial.println (F("Error 3: DHT checksum error."));
break;
default:
Serial.println (F("Error: Unrecognized code encountered."));
break;
} //End of switch
if(dht_dat[0] >= 40 && dht_dat[2] >= 30)
{
digitalWrite(ventilationPin, HIGH);
digitalWrite(inletballvalve, LOW);
delay(500);
digitalWrite(airwaterjet, HIGH);
Serial.println (F("ventilation running"));
}
else {
digitalWrite(ventilationPin, LOW);
digitalWrite(airwaterjet, LOW);
delay(500);
digitalWrite(inletballvalve, HIGH);
Serial.println (F("ventilation off"));
}
Serial.println (F(" \n\n"));
delay(2000); //Delay can be adjusted to 800ms but better to
//set it to 2000ms for more stable reading.
}
void InitDHT()
{
pinMode(dht_dpin,OUTPUT);
digitalWrite(dht_dpin,HIGH);
}
void ReadDHT()
{
/*Uses global variables dht_dat[0-4], and compFunc to pass
"answer" back. compFunc=0 if read went okay.
Depends on global dht_dpin for where to look for sensor.*/
compFunc=0;
byte dht_in;
byte i;
// Send "start read and report" command to sensor....
// First: pull-down I/O pin for 23000us
digitalWrite(dht_dpin,LOW);
delay(50); //standard is 23
//Next line: Brings line high again,
//second step in giving "start read..." command
digitalWrite(dht_dpin,HIGH);
delayMicroseconds(40); //DHT22 datasheet says host should
//keep line high 20-40us, then watch for sensor taking
//line low. That low should last 80us. Acknowledges
//"start read and report" command.
//Next: Change Arduino pin to an input, to
//watch for the 80us low explained a moment ago.
pinMode(dht_dpin,INPUT);
dht_in=digitalRead(dht_dpin);
if(dht_in){
compFunc=1; //dht start condition 1 not met
return;
} //end if
delayMicroseconds(80);
dht_in=digitalRead(dht_dpin);
if(!dht_in){
compFunc=2; //dht start condition 2 not met
return;
} //end if
/*After 80us low, the line should be taken high for 80us by the
sensor. The low following that high is the start of the first
bit of the forty to come. The routine "read_dht_dat()"
expects to be called with the system already into this low.*/
delayMicroseconds(80);
//now ready for data reception... pick up the 5 bytes coming from
// the sensor
for (i=0; i<5; i++)
dht_dat[i] = read_dht_dat();
//Next: restore pin to output duties
pinMode(dht_dpin,OUTPUT);
//Next: Make data line high again, as output from Arduino
digitalWrite(dht_dpin,HIGH);
//Next see if data received consistent with checksum received
byte dht_check_sum =
dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];
/*Condition in following "if" says "if fifth byte from sensor
not the same as the sum of the first four..."*/
if(dht_dat[4]!= dht_check_sum)
{
compFunc=3;
}//DHT checksum error
}
byte read_dht_dat()
{
//Collect 8 bits from datastream, return them interpreted
//as a byte. I.e. if 0000.0101 is sent, return decimal 5.
//Code expects the system to have recently entered the
//dataline low condition at the start of every data bit's
//transmission BEFORE this function is called.
byte i = 0;
byte result=0;
for(i=0; i< 8; i++){
//We enter this during the first start bit (low for 50uS) of the byte
//Next: wait until pin goes high
while(digitalRead(dht_dpin)==LOW);
//signalling end of start of bit's transmission.
//Dataline will now stay high for 27 or 70 uS, depending on
//whether a 0 or a 1 is being sent, respectively.
delayMicroseconds(45);//AFTER pin is high, wait further period, to be
//into the part of the timing diagram where a 0 or a 1 denotes
//the datum being send. The "further period" was 30uS in the software
//that this has been created from. I believe that a higher number
//(45?) might be more appropriate.
//Next: Wait while pin still high
if (digitalRead(dht_dpin)==HIGH)
result |=(1<<(7-i));// "add" (not just addition) the 1
//to the growing byte
//Next wait until pin goes low again, which signals the START
//of the NEXT bit's transmission.
while (digitalRead(dht_dpin)==HIGH);
} //end for
return result;
}
// functions to be called when an alarm triggers:
void MorningAlarm(){
Serial.println (F("Good-morning Arky"));
Serial.println (F("Aquaduino System Starting"));
pinMode(solenoidPin, HIGH);
pinMode(relaylights, HIGH);
pinMode(waterpump, HIGH);
pinMode(airpump, HIGH);
pinMode(outletballvalveclosed, LOW);
pinMode(outletballvalveopen, HIGH);
pinMode(inletballvalve, HIGH);
pinMode(c02valve, HIGH);
pinMode(weeweevalve, HIGH);
pinMode(tanklights, HIGH);
}
void EveningAlarm(){
Serial.println (F("Goodnight Arky"));
Serial.println (F("Aquaduino System Sleeping"));
pinMode(solenoidPin, LOW);
Alarm.delay(1000);
pinMode(relaylights, LOW);
Alarm.delay(1000);
pinMode(waterpump, LOW);
Alarm.delay(1000);
pinMode(airpump, LOW);
Alarm.delay(1000);
pinMode(outletballvalveopen, LOW);
Alarm.delay(1000);
pinMode(outletballvalveclosed, HIGH);
Alarm.delay(1000);
pinMode(inletballvalve, LOW);
Alarm.delay(1000);
pinMode(c02valve, LOW);
Alarm.delay(1000);
pinMode(weeweevalve, LOW);
Alarm.delay(1000);
pinMode(tanklights, LOW);
Alarm.delay(1000);
}
void digitalClockDisplay()
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println ();
}
void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}