Hi, I am new to coding and Arduino as well. I've been through most of the beginners tutorials and a few example sketches and circuit also. I have since moved on to begin building my project and am painstakingly trying to audit my code. I know I am pretty far away from having a complete functional code, but before I get too far into the deep end, I was hoping some of you can critique my code point out known errors.
Before I get into the code, I want it to be known that I am using a Mega/r3 (counterfeit), an Ethernet Shield (clone), Data Logging Shield with RTC (DS1307)(Adafruit), two DHT22 sensors to read temps and humidity and an 8 channel relay module (Sainsmart).
I want to have a web server (open to suggestion), or an internet accessible web page to view sensor readings, look at logged data and also have function on/off capabilities, an override if you will. I don't know which of the Firmata or Ethernet examples will get me as close to where I want to go with this, hence (open to suggestions).
4 of the 8 relays will be assigned timed alarms to trigger them on for 12 hours and off for 12 hours. The other channels will be reserved for "if" tests and be cycled on or off according to the DHT sensor readings.
I am still yet to learn how to graft in code to log the data and make it retrievable over the internet. I am mostly concerned with the fact that I am using 2 DHT sensors, and dealing with all of the variable names. I fear I may have over did the names as I duplicated everything and terminated each with a capital A or B. The relays may also present similar problems for me, but the simplicity of their function may be my saving grace on that. If you can see my code heading in the wrong direction, please outline my errors in example format so that you don't end up giving me the answers without the knowledge as the knowledge of how to make my own code is what I mostly seek. I've been through Robin2's How to Do Multiple Things thread already and it has gotten me pretty far.
//=====================================VARIABLE DEFINITIONS=======================================//
relayA - pin8/const int/digitalWrite()/
relayB - pin9/const int/digitalWrite()/
relayC - pin10/const int/digitalWrite()/
relayD - pin11/const int/digitalWrite()/
relayE - pin12/const int/digitalWrite()/
relayF - pin13/const int/digitalWrite()/
relayG - pin14/const int/digitalWrite()/
relayH - pin15/const int/digitalWrite()/
relay0 - boolean/beginning state is false
relay1 - boolean/beginning state is true
dhtA - pin4/const int/analogRead()/
dhtB - pin5/const int/analogRead()/
hA - float/analogRead()//dhtA humidity value
hB - float/analogRead()//dhtB humidity value
tA - float/analogRead()//dhtA celcius value
tB - float/analogRead()//dhtB celcius value
fA - float/analogRead()//dhtA fahrenheit value
fB - float/analogRead()//dhtB fahrenheit value
//===========================================CODE====================================================//
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetStreamEnc28J60.h>
#include <EthernetStreamW5100.h>
#include <EthernetUdp.h>
#include <util.h>
#include <Wire.h>
#include <RTClib.h>
#include <Time.h>
#include <TimeAlarms.h>
#include <SD.h>
const int dhtA = 4;
const int dhtB = 5;
const int relayA = 8; //relayA Pin #
relayA = relay0
const int relayB = 9; //relayB Pin #
relayB = relay0
const int relayC = 10; //relayC Pin #
relayC = relay1
const int relayD = 11; //relayD Pin #
relayD = relay1
const int relayE = 12; //relayE Pin #
const int relayF = 13; //relayF Pin #
const int relayG = 14; //relayG Pin #
const int relayH = 15; //relayH Pin #
boolean relay0 = false; //beginning state of relayA and relayB
boolean relay1 = true; //beginning state of relayC and relayD
#include "DHT.h"
#define DHTTYPE DHT22
DHT dhtA(dhtA, DHTTYPE); // Initialize DHT sensor A for normal 16mhz Arduino
DHT dhtB(dhtB, DHTTYPE); // Initialize DHT sensor B for normal 16mhz Arduino
void setup() {
dht.begin();
Serial.begin(9600); //DHT, DataLogger, Time Alarms - 9600, RTC - 57600,
Serial.println("DHTxx test!");
pinMode(relayA, OUTPUT);
pinMode(relayB, OUTPUT);
pinMode(relayC, OUTPUT);
pinMode(relayD, OUTPUT);
pinMode(relayE, OUTPUT);
pinMode(relayF, OUTPUT);
pinMode(relayG, OUTPUT);
pinMode(relayH, OUTPUT);
pinMode(dhtA, INPUT);
pinMode(dhtB, INPUT);
}
{
Alarm.alarmRepeat(9,00,0, MorningAlarm); // 9:00am every day
Alarm.alarmRepeat(21,01,0, EveningAlarm); // 9:01pm every day
//Alarm.alarmRepeat();
//Alarm.alarmRepeat();
//Alarm.alarmRepeat();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float hA = dht.readHumidity();
dhtA = hA; //hA value stored in dhtA
float hB = dht.readHumidity();
dhtB = hB; //hB value stored in dhtB
// Read temperature as Celsius
float tA = dht.readTemperature();
dhtA = tA; //tA value stored in dhtA
float tB = dht.readTemperature();
dhtB = tB; //tB value stored in dhtB
// Read temperature as Fahrenheit
float fA = dht.readTemperature(true);
dhtA = fA; //fA value stored in dhtA
float fB = dht.readTemperature(true);
dhtB = fB; //fB value stored in dhtB
// Check if any reads failed and exit early (to try again).
if(isnan(h) || isnan(t) || isnan(f))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
// Must send in temp in Fahrenheit!
float hi = dht.computeHeatIndex(fA. fB, hA, hB);
Serial.print("HumidityA: ");
Serial.print("HumidityB: ");
Serial.print(hA);
Serial.print(hB);
Serial.print(" %\tA");
Serial.print(" %\tB");
Serial.print("TemperatureA: ");
Serial.print("TemperatureB: ");
Serial.print(tA);
Serial.print(tB);
Serial.print(" *CA ");
Serial.print(" *CB ");
Serial.print(fA);
Serial.print(fB);
Serial.print(" *F\tA");
Serial.print(" *F\tB");
Serial.print("Heat indexA: ");
Serial.print("Heat indexB: ");
Serial.print(hiA);
Serial.print(hiB);
Serial.println(" *FA");
Serial.println(" *FB");
}
// functions to be called when an alarm triggers:
void MorningAlarm()
{
Serial.println("Alarm: - turn lights off");
}
void EveningAlarm(){
Serial.println("Alarm: - turn lights on");
}
void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
///////////Below tests DHT sensor for humidity and temp. If temp or humidty is high, relayE on pin 12 is energized
{
if(dhtA h >= 55); //if humidity is 55% or more
{
digitalWrite(relayEPin, HIGH); //Turns on fan connected to relayE
}
else if(dhtB t >= 27.777); //or else if celcius equals or exceeds 27°
{
digitalWrite(relayEPin, HIGH); //Also turns on fan connected to relayE
}
else
{
digitalWrite(relayEPin, LOW); //Otherwise relayE is unenergized (do I need this line?)
}
As you can probably tell, I just copy/pasted example code into a single sketch. I am now going through it over and over looking for syntax errors and such and trying to tie it all together as seamlessly as my limited understanding will afford me. I've made some notes in the comments here and there.
Any and all feedback is most welcomed and greatly appreciated!