Hello,
I am new to Arduino and programming. After a few days of lurking into the libraries and examples, I tried to improve an arduino sketch for my home project, that is based on an exact idea of this instructable: http://www.instructables.com/id/Environmental-Mushroom-Control-Arduino-Powered/
The sketch was later improvised by the author himself using DHT22 sensor on my request.(I haven't tried physically yet)
I gave it a thought and decided to be future proof with a robust and reliable sensor SHT-75. So I tried to upgrade the sketch with Sensirion SHT1x library.
I tried it myself for the first time using Arduino IDE. After a few errors and re-corrections, I found my sketch was compiled error free. I couldn't believe that I did it right as a newbie. I still doubt it if the sketch works properly in the real world with zero issues. I don't know it might.
I don't want to screw up with the project. Especially SHT-75 (ouch!!! very expensive!). I need to be sure of it before trying.
So someone who's more knowledgeable than me and experienced could please check the 1st part of the code below and approve if okay.
Please feel free for comments, ideas, suggestions ..
Thank you.
#include <SHT1x.h>
#include <LiquidCrystal.h>
#include <stdlib.h>
#include <EEPROM.h>
#define ARRAY_SIZE 2
#define dataPin 2
#define clockPin 3
/**********************/
/*Hysterisis Intervals*/
#define TEMP_LO 1
#define TEMP_HI 1
#define HUM_LO 5
#define HUM_HI 0
/**********************/
/*Define output Pins */
//Digital
#define RELAY1 11
#define RELAY2 12
#define RELAY3 13
#define BACKLIGHT 10
/***********************/
/* Interval Definitions*/
#define INTERVAL 30000
#define SENSOR_INTERVAL 100
#define BACKLIGHT_TIME 180000
#define EE_PROM_INTERVAL 600000
#define AIR_DIVISION 10
/******Define Size of storage array for values*/
#define ARRAY_STORE 200
SHT1x sht1x(dataPin, clockPin);
/*****************************/
/*set Triggers *************/
int heatTrigger1 = 20;
int HumTrigger = 80;
int duty = 25;
int thermValue1 = 0;
int humValue = 0;
int therm1[ARRAY_SIZE];
int humval[ARRAY_SIZE];
/*Set time check ms */
long last_check = millis();
long backlight = millis();
long time_eeprom = millis();
/** LCD Shield */
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
char buf[5];
int configure = 0;
int showReading = 0;
int adc_key_in = 1024;
int adc_key_val[5] = {100, 160, 360, 770, 800 };
int NUM_KEYS = 5;
int key= -1;
char trigger_names[3][15] = {"Temp Trigger ","Humid Trigger","Air Duty Cycle"};
/** Setup Booleans for LCD Relay indicator Persistance */
boolean Heat_on = false;
boolean Humid_on = false;
boolean Air_on = false;
/* Set up index value for storing values in eeprom*/
int k=0;
void setup(void)
{Serial.println("Starting up");
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
int i;for(i=0; i< ARRAY_SIZE; i++)
{therm1[i] = 20;humval[i] = 80;}
lcd.begin(16, 2);
pinMode(BACKLIGHT, OUTPUT);
digitalWrite(BACKLIGHT,HIGH);
Serial.begin(115200);
delay(100);}
void loop(void)
{ key = get_key(adc_key_in); //In case
float therml;
float humidity;
{
int i;for (i=0; i < ARRAY_SIZE -1 ; i++)
{therm1[i] = therm1[i+1];
humval[i] = humval[i+1];}
/*** Read in sensor values */
therm1[ARRAY_SIZE -1] = sht1x.readTemperatureC();
humval[ARRAY_SIZE -1] = sht1x.readHumidity();
Serial.print(therm1[ARRAY_SIZE -1]);
Serial.print(":");
Serial.println(humval[ARRAY_SIZE -1]);
/**Add first line of Display**/
if (configure == 0)
{lcd.setCursor(0,0); lcd.print("Tem:Hum:Duty");}
thermValue1 = mov_avg(therm1);
humValue = mov_avg(humval);
/**** Add second line of the Display *****/
if (configure == 0)
{lcd.setCursor(0,1);
itoa(thermValue1, buf, 10);
lcd.print(buf);
lcd.setCursor(4,1);
itoa(humValue, buf, 10);
lcd.print(buf);
lcd.setCursor(8,1);
itoa(duty, buf, 10);
lcd.print(buf);} }
/*EEPROM STORAGE ***/
if(millis() < time_eeprom) { time_eeprom = millis(); }
/**Put values into eeprom*/
if(millis() - time_eeprom > EE_PROM_INTERVAL)
{time_eeprom = millis();
EEPROM.write((k*2),(thermValue1));
EEPROM.write((k*2+1),(humValue));
k=k+1;
if(k>=ARRAY_STORE) { k=0; }}
if( millis() < last_check ) { last_check = millis(); }
if (millis() - last_check > INTERVAL)
{last_check = millis();
//Temperature check and Relay Change
if (thermValue1 < (heatTrigger1 +TEMP_HI) ){digitalWrite(RELAY1, LOW);Heat_on =false ; }
if (thermValue1 > (heatTrigger1 -TEMP_LO) ) { digitalWrite(RELAY1, HIGH); Heat_on = true; }
//Humidity Check and Relay change //
if ( humValue > HumTrigger+ HUM_HI ) {digitalWrite(RELAY3, LOW);Humid_on = false; }
if ( humValue <= HumTrigger - HUM_LO ){digitalWrite(RELAY3, HIGH);Humid_on = true;}
and continued in attachment:
arduino remaining code attachment.txt (4.09 KB)