I am writing a sketch that will switch relay states at predefined times, and/or high/low sensor values. I know my sketch is far from being considered efficiently coded, but in my defense I have language that almost works great... almost.
I know I am needing to get boolean variables added, and also they need to be queried to determine if a state shall be high, and for how long before it reverts back to low, but I am simply unable to grasp the understanding I know I need to effectively use them.
I will attach my entire sketch rather than insert snippets and expect you to know everything else I have therein. This is just so you (my teachers) can get a feel of where I'm at in my understanding of code and advise accordingly. I am not seeking someone to write that snippet per say, but I do wish to learn this stuff as I do intend to share this project with many other gardeners, and want to know the code good enough to further assist others that are yet to get into Arduino.
A link to a good tutorial will probably do me more justice than picking apart my code and renaming my variables and saying things like "you can do this, by changing that" and the like, with no real conveyance of understanding or knowledge.
Thank you all in advance!
#include "DHT.h" // Temp/Humidity Sensor Library
#include <Wire.h> // i2c communications Library
#include "RTClib.h" // RealTimeClock Library for DS1307 and DS3231
#define RELAY_ON 1 // RELAY_ON and RELAY_OFF may need to swich values to work properly
#define RELAY_OFF 0 // Used to switch relay states for on/off of AC devices
#define Relay_A 30 // relay 1 -
#define Relay_B 31 // relay 2 -
#define Relay_C 32 // relay 3 -
#define Relay_D 33 // relay 4 -
#define Relay_E 34 // relay 5 -
#define Relay_F 35 // relay 6 -
#define Relay_G 36 // relay 7 -
#define Relay_H 37 // relay 8 -
// Sensor Declaration
#define DHTPIN A2 // DHT22 data wire is connected to Analog 2 pin. A 10k pullup resistor is needed to connect data to +5V
#define DHTTYPE DHT22
// Sensor Reference Values
int hiTempDHT = 85; // Max Fahrenheit Value -- Change these around to find what works best for you
int lowTempDHT = 60; // Min Fahrenheit Value -- Change these around to find what works best for you
int hiHumDHT = 60; // Max Humidity Value -- Change these around to find what works best for you
RTC_DS1307 RTC;
// declare DHT variables
float h;
float f;
float UTCOffset = -5.0; // Your timezone relative to UTC (http://en.wikipedia.org/wiki/UTC_offset)
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
delay( 50 ); // allow some time (50 ms) after powerup and sketch start, for the Wiznet W5100 Reset IC to release
// and come out of reset. http://www.freetronics.com.au/pages/usb-power-and-reset#.VVpp-Eao28g
Serial.begin(9600);
//Serial2.begin() // For other baud rates
//Serial3.begin() // For other baud rates
//---(Set pins 30-37 as outputs )----
pinMode(Relay_A, OUTPUT);
pinMode(Relay_B, OUTPUT);
pinMode(Relay_C, OUTPUT);
pinMode(Relay_D, OUTPUT);
pinMode(Relay_E, OUTPUT);
pinMode(Relay_F, OUTPUT);
pinMode(Relay_G, OUTPUT);
pinMode(Relay_H, OUTPUT);
delay(1000);
dht.begin();
Wire.begin();
RTC.begin();
RTC.adjust(DateTime(__DATE__, __TIME__)); //this captures the time from the computer that is uploading sketch to Arduino
// so ensure that the uploading computer has the correct time.
delay(1000);
}
void loop()
{ //******************************************************************************//
//****** Setting Time and testing to display 24 hour time as 12 hour time ******//
//******************************************************************************//
DateTime now = RTC.now();
int twelveHour = now.hour() - 12; // Variable used to display 13+ hours in 12 hour format
int zeroHour = 12; // Variable use to convert "0" zero hour to display it as 12:00+
Serial.print('-');
Serial.print(' ');
if (now.hour() == 0){ // First we test if the hour reads "0"
Serial.print (F("AM"));
Serial.print(' ');
Serial.print('-');
Serial.print(' ');
Serial.print(zeroHour); // if yes, serial print a "12" instead
}
else if (now.hour() >= 13){ // if no, Second we test if the hour reads "13 or more"
Serial.print (F("PM"));
Serial.print(' ');
Serial.print('-');
Serial.print(' ');
Serial.print(twelveHour); // if yes, serial print that current hour minus 12
}
else
{
Serial.print (F("AM"));
Serial.print(' ');
Serial.print('-');
Serial.print(' ');
Serial.print(now.hour(), DEC); // if no, Third we conclude that the am hours are being displayed correctly.
}
{
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(' ');
Serial.print('-');
Serial.print(' ');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(' ');
Serial.print('-');
Serial.print(' ');
//Serial.print(F("am"));
//Serial.print(F("PM"));
Serial.println();