Moin Leute,
habe mit mit meinem UNO ne kleine Schaltung gebaut um mit einem DHT22 an Pin 7 Temperatur und Feuchtigkeit zu messen, diese Mittels Datenlogger Shild und RTC auf einer SD zu speichern und gleichzeitig Uhrzeit, Temperatur und so auf einem I2C Display wiederzugeben (Uhrzeit und DAtum werden in jedem zyklus erneuert, die messungen nur alle 5 Minuten durchgeführt).
Jetzt habe ich das Programm noch dahingehend erweitert das die Hintergrundbeleuchtung nur auf Tastendruck (Taster an Pin6) angeht und bei der nächsten Messung wieder verlischt.
Wenn ich den Arduino über USB versorge funktioniert alles super.
Versorge ich den Arduino gedoch mittels 12V netzteil ist das display (Hintergrundbeleuchtung) ständig an und "blinkt" während einer messung nur kurz.
Wäre cool wenn mal einer von euch drüber gucken könnte.
Sollte zwar eig nicht am Sketch liegen aber bevor einer meckert ![]()
Sketch:
// für DHT22
#include <SimpleDHT.h>
// for DHT22,
// VCC: 5V or 3V
// GND: GND
// DATA: 2
int pinDHT22 = 7;
SimpleDHT22 dht22;
//für die SD
#include <SPI.h>
#include <SD.h>
//SD Save
File dataFile;
const int chipSelect = 10; // Der SelectPin für die SD Karte liegt auf Pin 10
//für die RTC
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
//für das LCD Display
#include <LiquidCrystal_I2C.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x3F,20,4); // set the LCD address to 0x3F for a 20 chars and 4 line display
// Generally, you should use "unsigned long" for variables that hold time
// will store last time LED was updated
unsigned long previousMillis = 0;
// constants won't change:
// interval at which to blink (milliseconds)
const long interval = 300000;
//Gradzeichen einfügen
byte grad[8] = {
B00111,
B00101,
B00111,
B00000,
B00000,
B00000,
B00000,
};
//Eingang für Displaybeleuchtung
const int buttonPin = 6;
int buttonState = 0;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup(void)
{
// start serial port
Serial.begin(9600);
//Eingänge für Displaysteuerung
pinMode(buttonPin, INPUT);
lcd.init(); // initialize the lcd
//lcd.backlight(); // Hintergrundbeleuchtung einschalten
//lcd.noBacklight(); // Hintergrundbeleuchtung ausschalten
//Festwerte für Datum und Zeit setzen
lcd.print("Date:");
lcd.setCursor(9, 0);
lcd.print(".");
lcd.setCursor(12, 0);
lcd.print(".");
lcd.setCursor(0, 1);
lcd.print("Time:");
lcd.setCursor(9, 1);
lcd.print(":");
lcd.setCursor(12, 1);
lcd.print(":");
//SD initialisieren
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(SS, OUTPUT);
// see if the card is present and can be initialized:
SDSTART:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
digitalWrite(3, LOW);
goto SDSTART;
} else {
digitalWrite(3, HIGH);
}
Serial.println("card initialized.");
// Open up the file we're going to log to!
SDWRITE:
dataFile = SD.open("datalog.csv", FILE_WRITE);
if (! dataFile) {
Serial.println("error opening datalog.csv");
// Wait forever since we cant write data
digitalWrite(3, LOW);
goto SDWRITE;
} else {
digitalWrite(3, HIGH);
}
// Gradzeichen auf Byte 0 schreiben
lcd.createChar(0, grad);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
float temperature = 0;
float humidity = 0;
void loop() {
//Displaybeleuchtung bei drücken einschalten
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){
lcd.backlight();
}
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
Templesen(temperature, humidity);
//saveSD wird aufgerufen um Daten zu speichern
saveSD(temperature, humidity);
lcd.noBacklight();
}
//aufruf des Zeitbausteins
Time(temperature, humidity);
}
void Templesen(float &temp, float &hum){
// start working...
Serial.println("=================================");
// read without samples.
// @remark We use read2 to get a float data, such as 10.1*C
// if user doesn't care about the accurate data, use read to get a byte data, such as 10*C.
int err = SimpleDHTErrSuccess;
if ((err = dht22.read2(pinDHT22, &temp, &hum, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000);
return;
}
Serial.print("Sample OK: ");
Serial.print((float)temp); Serial.print(" *C, ");
Serial.print((float)hum); Serial.println(" RH%");
}
void Time(float temperature, float humidity){
// Uhrzeit auslesen und in "now" speichern:
DateTime now = rtc.now();
// Uhrzeit in die Formation einfügen
lcd.setCursor(7, 0);
lcd.print(now.day(), DEC);
lcd.setCursor(10, 0);
lcd.print(now.month(), DEC);
lcd.setCursor(13, 0);
lcd.print(now.year(), DEC);
lcd.setCursor(7, 1);
lcd.print(now.hour(), DEC);
lcd.setCursor(10, 1);
lcd.print(now.minute(), DEC);
lcd.setCursor(13, 1);
lcd.print(now.second(), DEC);
lcd.setCursor(0, 2);
lcd.print((float)temperature, DEC);
lcd.setCursor(16, 2);
lcd.write(byte(0));
lcd.print("C");
lcd.setCursor(0, 3);
lcd.print(int(humidity), DEC);
lcd.setCursor(16, 3);
lcd.print("%RH");
}
void saveSD(float temperature, float humidity){
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
DateTime now = rtc.now();
String dataString = "";
//Daten in String schreiben zum speichern
dataString += now.day(), DEC;
dataString += "/";
dataString += now.month(), DEC;
dataString += "/";
dataString += now.year(), DEC;
dataString += ";";
dataString += now.hour(), DEC;
dataString += ":";
dataString += now.minute(), DEC;
dataString += ":";
dataString += now.second(), DEC;
dataString += ";";
dataString += temperature;
dataString += ";";
dataString += "°C";
dataString += ";";
dataString += humidity;
dataString += ";";
dataString += "%RH";
dataFile.println(dataString);
Serial.println(dataString);
dataFile.flush();
Serial.println("---Datensatz auf SD gespeichert---");
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
}
