// Taupunktberechnung
// k = kurzer Sensor
// l = langer Sensor
#include "DHT.h" //Temperatur- + Luftfeuchtesensoren
#include "math.h" //Taupunktberechnung
#include "SD.h" //SD-Card
// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 10; //SD-Card
#include "Wire.h" //Uhrzeit
#define DS1307_I2C_ADDRESS 0x68 // This is the I2C address
//Temperatur- + Luftfeuchtesensoren
#define DHTPINk 2 // D2 PIN2 für kurzen Sensor
#define DHTPINl 4 // D4 PIN4 für langen Sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dhtk(DHTPINk, DHTTYPE);
DHT dhtl(DHTPINl, DHTTYPE);
//Konstanten zur Taupunktberechnung #####################
const float a = 7.5;
const float b = 237.3;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void setup() {
//Temperatur- + Luftfeuchtesensoren ###################
dhtk.begin();
dhtl.begin();
//Motor ###############################################
//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B
pinMode(13, OUTPUT); //Initiates Motor Channel A pin
pinMode(8, OUTPUT); //Initiates Brake Channel A pin
//Uhrzeit ############################################
Wire.begin(); //Uhrzeit
//SD-Card ##########################################
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
Serial.begin(9600);
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("SD-Card failed, or not present");
// don't do anything more:
//rote LED leuchten lassen
return;
}
Serial.println("SD-Card initialized.");
//Fensterschließen lassen als Referenzierung
}
//Ende Void setup
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float hk = dhtk.readHumidity();
float tk = dhtk.readTemperature();
float hl = dhtl.readHumidity();
float tl = dhtl.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(tk) || isnan(hk)) {
Serial.println("Failed to read from DHTk");
//rote LED leuchten lassen
} else {
Serial.print("DHTk Humidity: ");
Serial.print(hk);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(tk);
Serial.print(" *C ");
}
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(tl) || isnan(hl)) {
Serial.println("Failed to read from DHTl");
//rote LED leuchten lassen
} else {
Serial.print("DHTl Humidity: ");
Serial.print(hl);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(tl);
Serial.print(" *C ");
}
Serial.print("Taupunkt fuer DHTk: ");
float TPk = taupunkt(hk,tk);
Serial.print(TPk);
Serial.println(" *C");
//Speicherung auf SD-Karte
// make a string for assembling the data to log:
String dataString = "";
//5 Stellen reservieren
char* s = " ";
//append to the string:
s = dtostrf(hk,5,1,s);
dataString += s;
dataString += ";";
s = dtostrf(tk,5,1,s);
dataString += s;
dataString += ";";
s = dtostrf(hl,5,1,s);
dataString += s;
dataString += ";";
s = dtostrf(tl,5,1,s);
dataString += s;
dataString.replace('.', ',');
//Uhrzeit vorne anfügen, nach Umwandlung von . in ,
dataString = getDateDs1307SRTRING() + dataString;
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
//rote LED leuchten lassen
}
//MOTOR-Test
Serial.println("Fenster oeffnen...");
OpenWindow();
Serial.println("warten (3s) ...");
delay(3000);
Serial.println("Fenster schliessen...");
CloseWindow();
Serial.println("warten (1s) ...");
delay(1000);
Serial.println("Ende");
}
//Ende void loop
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Hilfsfunktion zur Taupunktberechnung #################
float taupunkt(float h, float t)
{
float result;
float tstern;
tstern = (a*t) / (b+t);
result = (b*(log10(h) -2 + tstern)) / (a - log10(h) +2 - tstern);
//Berechnung mit +/-2, da Humidity in % angegeben wird
return result;
}
//Hilfsfunktion: Datum +Uhrzeit als String #############
// Gets the date and time from the ds1307 and prints result
String getDateDs1307SRTRING()
{
char second, minute, hour, dayOfWeek, dayOfMonth, month, year;
String DateString = "";
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
// alle müssen ausgelesen werden, auch wenn nicht weiter verwendeten werden, wg. Reihenfolge
second = bcdToDec(Wire.read() & 0x7F);
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read() & 0x3F); // Need to change this if 12 hour am/pm
dayOfWeek = bcdToDec(Wire.read());
dayOfMonth = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
DateString += String(dayOfMonth, DEC);
DateString += ".";
DateString += String(month, DEC);
DateString += ".";
DateString += String(year, DEC);
DateString += " ";
DateString += String(hour, DEC);
DateString += ":";
DateString += String(minute, DEC);
DateString += ";";
return DateString;
}
//Uhrzeit ################################################
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
//Fenster öffnen #########################################
void OpenWindow()
{
//MOTOR A ---- forward @ full speed
digitalWrite(12, LOW); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
delay(3000);
//anhalten
digitalWrite(9, HIGH); //Eengage the Brake for Channel A
delay(1000); //Verzögerung, damit Bremse noch Strom hat (min 1 s)
analogWrite(3, 0); //Strom für Motor abschalten --> Bremse ohne Funktion
}
//Fenster schliessen #####################################
void CloseWindow()
{
//MOTOR A ---- backward @ full speed
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
delay(3000);
//anhalten
digitalWrite(9, HIGH); //Eengage the Brake for Channel A
delay(1000); //Verzögerung, damit Bremse noch Strom hat (min 1 s)
analogWrite(3, 0); //Strom für Motor abschalten --> Bremse ohne Funktion
}