Hello,
recently I created a project with a friend, and we wanted to save Data on a SD Card which we got from the DHT11. We noticed that because of the dht.read...() method our Datalogger is unable to save anything.
#include <DHT.h>
//#include <DHT_U.h>
#include <Elegoo_GFX.h> // Core graphics library
#include <Elegoo_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
//#include <String>
DHT dht(13, DHT11);
RTC_DS1307 rtc;
#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
#define TS_MINX 120
#define TS_MAXX 900
#define TS_MINY 70
#define TS_MAXY 920
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4
#define MINPRESSURE 10
#define MAXPRESSURE 1000
// Assign human-readable names to some common 16-bit color values:
#define RED 0xF800
#define GREEN 0x07E0
#define BLUE 0x001F
#define WHITE 0xFFFF
#define BLACK 0x0000
Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
File myFile;
unsigned long previousMillis = 0;
unsigned long shitypreviousMillis = 0;
const long interval = 5000;
const long shityinterval = 10000;
boolean druck = false;
boolean druck2 = false;
float temp = 2;
float humi = 1;
int zahl = 0;
void setup(void)
{
Serial.begin(9600);
dht.begin();
rtc.begin();
tft.reset();
//pinMode(10, OUTPUT);
identifier();
tft.setRotation(3);
tft.fillScreen(GREEN);
//Jahr,Monat,Tag,Stunde,Minute,Sekunde
rtc.adjust(DateTime(2020, 6, 4, 8, 46, 0));
Serial.println("Initializing SD card...");
if (!SD.begin())
{
Serial.println("Card error");
return;
}
Serial.println("Card initialized");
if (! rtc.begin())
{
Serial.println("No RTC found");
}
else
{
Serial.println("RTC clock found");
}
if (! rtc.isrunning())
{
Serial.println("RTC not configured");
}
tft.fillScreen(RED);
}
void loop()
{
humi = dht.readHumidity();
temp = dht.readTemperature();
// tft.fillScreen(BLACK);
TSPoint p = ts.getPoint();
// if sharing pins, you'll need to fix the directions of the touchscreen pins
// pinMode(XP, OUTPUT);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
// pinMode(YM, OUTPUT);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
writeData(getTime() + ", " + "Temp: " + temp + " Humidity: " + humi + "%");
}
if ((druck == true))
{
if ((druck2 == true))
{
//getValue();
anzeigen();
Serial.println("Bildschirm on ");
druck2 = false;
}
if ((currentMillis - shitypreviousMillis) >= shityinterval)
{
tft.fillScreen(BLUE);
Serial.println("Bildschirm off ");
druck = false;
}
}
if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
{
// scale from 0->1023 to tft.width
p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
// p.x = tft.width()-map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
p.y = (tft.height() - map(p.y, TS_MINY, TS_MAXY, tft.height(), 0));
// p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);
Serial.print("( x=");
Serial.print(p.x);
Serial.print(" , y=");
Serial.print(p.y);
Serial.println(" )");
druck = true;
druck2 = true;
shitypreviousMillis = currentMillis;
}
}
void anzeigen()
{
tft.fillScreen(RED);
tft.setCursor(60, 80);
tft.setTextColor(WHITE);
tft.setTextSize(5);
tft.print(temp);
tft.print(" C");
tft.setCursor(60, 120);
tft.print(humi);
tft.println(" %");
Serial.print(temp);
Serial.println("C");
}
//String getValue() {
//temp = 40;
//humi = 40;
//humi = dht.readHumidity();
//temp = dht.readTemperature();
//Serial.print(humi);
//Serial.print(temp);
//String text = String("Temp: ") + dht.readTemperature() + " Humidity: " + dht.readHumidity() + "%";
//return (text);
//}
String getTime()
{
DateTime now = rtc.now();
String timeString;
if (now.day() <= 9)
{
timeString = "0";
timeString += now.day();
}
else
{
timeString = now.day();
}
timeString += ".";
if (now.month() <= 9)
{
timeString += "0";
timeString += now.month();
}
else
{
timeString += now.month();
}
timeString += ".";
if (now.year() <= 9)
{
timeString += "0";
timeString += now.year();
}
else
{
timeString += now.year();
}
timeString += ", ";
if (now.hour() <= 9)
{
timeString += "0";
timeString += now.hour();
}
else
{
timeString += now.hour();
}
timeString += ":";
if (now.minute() <= 9)
{
timeString += "0";
timeString += now.minute();
}
else
{
timeString += now.minute();
}
timeString += ":";
if (now.second() <= 9)
{
timeString += "0";
timeString += now.second();
}
else
{
timeString += now.second();
}
return timeString;
}
void writeData(String writetext)
{
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile)
{
myFile.println(writetext);
myFile.close();
zahl++;
Serial.print(zahl);
Serial.print(" ");
Serial.println(writetext);
}
else
{
myFile.println("fehler");
myFile.close();
Serial.print(myFile);
Serial.print("Error, could not create/write file ( ");
Serial.print(writetext);
Serial.println(" )");
return;
}
}
void identifier()
{
uint16_t identifier = tft.readID();
if (identifier == 0x9325)
{
Serial.println(F("Found ILI9325 LCD driver"));
}
else if (identifier == 0x9328)
{
Serial.println(F("Found ILI9328 LCD driver"));
}
else if (identifier == 0x4535)
{
Serial.println(F("Found LGDP4535 LCD driver"));
}
else if (identifier == 0x7575)
{
Serial.println(F("Found HX8347G LCD driver"));
}
else if (identifier == 0x9341)
{
Serial.println(F("Found ILI9341 LCD driver"));
}
else if (identifier == 0x8357)
{
Serial.println(F("Found HX8357D LCD driver"));
}
else if (identifier == 0x0101)
{
identifier = 0x9341;
Serial.println(F("Found 0x9341 LCD driver"));
}
else
{
Serial.print(F("Unknown LCD driver chip: "));
Serial.println(identifier, HEX);
// Serial.println(F("If using the Elegoo 2.8\" TFT Arduino shield, the line:"));
// Serial.println(F(" #define USE_Elegoo_SHIELD_PINOUT"));
// Serial.println(F("should appear in the library header (Elegoo_TFT.h)."));
// Serial.println(F("If using the breakout board, it should NOT be #defined!"));
// Serial.println(F("Also if using the breakout, double-check that all wiring"));
// Serial.println(F("matches the tutorial."));
identifier = 0x9341;
}
tft.begin(identifier);
}
Arduino_Uno(doesnotworkbutitshould).ino (6.75 KB)