Hiiii, guys!
I’ve made a 450 line program code I use for my arduino mega. The program controls a viviarum (rainforest terrarium) and ensures the humidity and temperature is at the right level, while it also works as a time clock by using 8 relays I’ve hooked up. You can also turn off / on different things such as light, or the waterfall using buttons.
The time and temp can be displayed on a 16x2 LCD screen.
The problem is that sometimes it weird characters like ! = 3¤%(6)54)3== on both lines. It is not a lose connection.
If I reset the Arduino it will work just fine again, until next randomly time.
It often comes and reset itself when I use the buttons or when a sensor activates one of the relays.
Some of the program can be found below (max 9000 characs), the LCD printing is at the bottom. Any ideas would be appreciated thanks!
//under arbejde - test stadie
//=================Libaries used
#include "DHT.h"
#include <Wire.h>
#include "Sodaq_DS3231.h"
#include <LiquidCrystal.h>
//Humid + Temp sensor (DHT22)
#define DHTPIN 13 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
//Relay pins
#define fanBIG 9
#define fanSMALL 8
#define mistking 7
#define dagslys 6
#define nattelys 5
#define waterheater 4
#define LEDdagslys 3
#define vandfald 2
//Regulator opposite
#define ON false
#define OFF true
//SETPOINTS
#define humidalarm 40
#define Tmax 29
#define Tmin 20
#define humifanBIG 95
#define humifanSMALL 95
#define humimistking 40
#define HYSTRESE 40
#define HYSTRESEmist 5
#define tempFAN 29
#define tempmistking 21
#define dagslysstart 8
#define dagslyssluk 20
//Manuel betjening
#define MANnightday 40
#define MANvent 30
#define MANmist 26
#define MANwaterfall 48
int MANnightdaystate = 0;
int MANventstate = 0;
int MANmiststate = 0;
int MANwaterfallstate = 0;
//LCD setup
LiquidCrystal lcd(31, 33, 35, 37, 39, 41);
//Anti-BUG system
boolean Dmistking = false;
boolean mistkings = false;
boolean DfanSMALL = false;
boolean fanSMALLs = false;
boolean DfanBIG = false;
boolean fanBIGs = false;
boolean waterfallonoff = true;
//Time day deffinition
char weekDay[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
//==============GENERAL SETUP====================
void setup() {
Serial.begin(9600);
dht.begin();
lcd.begin(16,2);
Wire.begin();
rtc.begin();
pinMode(MANvent, INPUT);
pinMode(MANmist, INPUT);
pinMode(MANnightday, INPUT);
pinMode(fanBIG, OUTPUT);
pinMode(fanSMALL, OUTPUT);
pinMode(mistking, OUTPUT);
pinMode(WaterLED, OUTPUT);
pinMode(TimeLED, OUTPUT);
pinMode(temphumalarmLED, OUTPUT);
pinMode(dagslys, OUTPUT);
pinMode(nattelys, OUTPUT);
pinMode(vandfald, OUTPUT);
pinMode(waterheater, OUTPUT);
pinMode(LEDdagslys, OUTPUT);
digitalWrite(fanBIG, OFF);
digitalWrite(fanSMALL, OFF);
digitalWrite(mistking, OFF);
digitalWrite(WaterLED, OFF);
digitalWrite(TimeLED, OFF);
digitalWrite(temphumalarmLED, OFF);
digitalWrite(dagslys, OFF);
digitalWrite(nattelys, OFF);
digitalWrite(vandfald, OFF);
digitalWrite(waterheater, OFF);
digitalWrite(LEDdagslys, OFF);
}
//====================GENERAL PROGRAM=============
void loop() {
delay(500);
float h = (dht.readHumidity()-16);
// Read temperature as Celsius (the default)
float t = (dht.readTemperature()-0.5); // Trækker 2 grader fra temp for at få mere realistisk!
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
//================TIDS REGULATOR==================
DateTime now = rtc.now();
int years = now.year();
int months = now.month();
int day = now.date();
int hours = now.hour();
int minutes = now.minute();
int seconds = now.second();
//==========LILLE VENTILATOR===============
if ((minutes == 0) || (minutes == 30))
{
DfanSMALL = true;
digitalWrite(fanSMALL, ON);
}
else if (fanSMALLs == false) {
DfanSMALL = false;
digitalWrite(fanSMALL, OFF);
}
//==STOR VENTILATOR==
if ((minutes == 0) || (minutes == 30))
{
DfanBIG = true;
digitalWrite(fanBIG, ON);
}
else if (fanBIGs == false)
{
DfanBIG = false;
digitalWrite(fanBIG, OFF);
}
//===TIMER ALARM==
timercheck2 = seconds;
if ((years != 2017) || (timercheck1-timercheck2 == 0)){
digitalWrite(TimeLED, HIGH);
tone(51, 3000, 500);
}
//===============RESERVOIR LEVEL+VARMELEGME=============
int wlvl = analogRead(WaterLevel);
if (wlvl > 990)
{
digitalWrite(WaterLED, HIGH);
digitalWrite(waterheater, OFF);
tone(51, 3000, 500);
}
else {
digitalWrite(WaterLED, LOW);
digitalWrite(waterheater, ON);
}
//Temp Humid alarm
if (t <= Tmin || t >= Tmax || h <= humidalarm)
{
digitalWrite(temphumalarmLED, HIGH);
tone(51, 3000, 2000);
}
//========VENTILATORER 2==
if (digitalRead(fanSMALL) == ON)
{
if ( (h < (humifanSMALL - HYSTRESE)) && (t < tempFAN) && (DfanSMALL == false) ) // Tjekker om den standard timing er igang... Hvis den er det, må den ikke kunne slukke
{
fanSMALLs = false;
digitalWrite(fanSMALL, OFF);
} //Hvis luftfugtighed er mindre end setpunkt OG temeperatur er mindre end setpunkt så slå fra
}
else
{
if ( (h > humifanSMALL) || (t > tempFAN) )
{
fanSMALLs = true;
digitalWrite(fanSMALL, ON);
} //Hvis luftfugtighed er større end setpunkt ELLER temeperatur er større end setpunkt så slå til
}
//==========Manual Tænd-styring - Vil altid overrulle det andet!==================
//========================MANUEL VENTILATORER=====================================
MANventstate = digitalRead(MANvent);
if (MANventstate == HIGH)
{
DfanBIG = true;
DfanSMALL = true;
digitalWrite(fanBIG, ON);
digitalWrite(fanSMALL, ON);
}
DfanBIG = false;
DfanSMALL = false;
//===========================MANUEL MISTKING===================================
MANmiststate = digitalRead(MANmist);
if (MANmiststate == HIGH) {
Dmistking = true;
digitalWrite(mistking, ON);
}
else {
Dmistking = false;
}
//===========================NATTE/DAG===================================
MANnightdaystate = digitalRead(MANnightday);
if (MANnightdaystate == HIGH)
{
if ((hours >= dagslyssluk) || (hours < dagslysstart)) {
digitalWrite(nattelys, OFF);
digitalWrite(dagslys, ON);
digitalWrite(LEDdagslys, ON);
}
else {
digitalWrite(nattelys, ON);
digitalWrite(dagslys, OFF);
digitalWrite(LEDdagslys, OFF);
}
}
//===========================VANDFALD ON/OFF===================================
MANwaterfallstate = digitalRead(MANwaterfall);
if (MANwaterfallstate == HIGH)
{
waterfallonoff = !waterfallonoff;
}
if (waterfallonoff == false) {
digitalWrite(vandfald, OFF);
}
else if (waterfallonoff == true) {
digitalWrite(vandfald, ON);
}
//==========================AFSLUTNING AF REGULERING============================
delay(500);
timercheck1 = seconds;
//Sensor
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
//Waterlevel
Serial.println(wlvl);
//Time
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.date(), DEC);
Serial.print(' ');
//LCD Display Print
lcd.setCursor(0,0);
lcd.print(t); // Temperature from temp sensor
lcd.print((char)223);
lcd.print("C ");
lcd.print("H:");
lcd.print(h); // Humidity from humid sensor
lcd.print("%");
lcd.setCursor(0,1);
if (hours < 10) { // This line is for adding a "0" infront of 1,2,3... makes it look nice
lcd.print("0");
lcd.print(now.hour(), DEC);
}
else {
lcd.print(now.hour(), DEC);
}
lcd.print(':');
if (minutes < 10) { // This line is for adding a "0" infront of 1,2,3... makes it look nice
lcd.print("0");
lcd.print(now.minute(), DEC);
}
else {
lcd.print(now.minute(), DEC);
}
lcd.print(':');
if (seconds < 10) { // This line is for adding a "0" infront of 1,2,3... makes it look nice
lcd.print("0");
lcd.print(now.second(), DEC);
}
else {
lcd.print(now.second(), DEC);
}
lcd.print(' ');
if (day < 10) { // This line is for adding a "0" infront of 1,2,3... makes it look nice
lcd.print("0");
lcd.print(now.date(), DEC);
}
else {
lcd.print(now.date(), DEC);
}
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('-');
lcd.print(now.year() - 2000, DEC); // Just want a 17 to bee shown therefor i subtract 2000
}