I use Arduino uno. I want to read the values from the SD card. Collect it as a variable to compare. With the temperature value that I measured from the module
My problem is TMAX / TMIN. Do not store the values read from the SD Card. How do I write?
#include "DHT.h"
#include <SPI.h>
#include <SD.h>
#define DHTPIN A0
#define DHTTYPE DHT11
int sensorPin = A0;
int ledPin = 13;
int sensorValue = 0;
DHT dht(DHTPIN, DHTTYPE);
const int chipSelect = 10;
File root;
File root1;
void setup() {
Serial.begin(9600);
Serial.println();
Serial.println(F("DHT11 Setup"));
dht.begin();
while (!Serial) {;}
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
while (1);
}
Serial.println("card initialized.");
}
void loop() {
delay(1000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
lcd.setCursor(0,0);
lcd.print("FailedDHTsensor!");
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humi: "));
Serial.print(h);
Serial.print(F("% Temp: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.println(F("°F"));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
int TMAX = root;
root = SD.open("TMAX.txt");
if (root) {
while (root.available()) {
Serial.write(root.read());
}
root1.close();
}
int TMIN = root1;
root1 = SD.open("TMIN.txt");
if (root1) {
while (root1.available()) {
Serial.write(root1.read());
}
root1.close();
}
if (t>=TMAX){
Serial.println("FAN:ON");
}else if (t<=TMIN){
Serial.println("FAN:OFF");
}else{
Serial.println("!Good");
}
}
What do you expect TMAX to contain after this assignment?
root = SD.open("TMAX.txt");
if (root) {
while (root.available()) {
Serial.write(root.read());
}
root1.close();
}
Open one file. Read, and uselessly print, the data from that file. Close some other file.
I really do not understand why you do THAT.
int TMIN = root1;
Another bizarre assignment…
if (t>=TMAX){
That comparison makes no sense.
You need to open the file, read AND STORE, the contents of the file. Then, you need to convert the array you saved the data in to an int, and assign that value to TMIN or TMAX.
By the way, by convention, all capital letter names are reserved for constants, so, after initialization, they never appear on the left of an equal sign.
You have some text in the file. You need to convert that text to an int. The simplest way to do that involves using the parseInt() method that every class, including File, that inherits from Stream, has.
You have some text in the file. You need to convert that text to an int. The simplest way to do that involves using the parseInt() method that every class, including File, that inherits from Stream, has.
Do you have an example for me to study?
Should I use Stream.parseInt ()?
I tried to search for information and I know that Serial.write (root.read ()) writes 8-bit data.
And if I want to convert the data into numbers, can it be stored in the X storage?
The value in SD is 30
I tried to find the information in the example. But I haven't found it yet Can you help me if you have an example
Do you have a Stream object? I could have sworn you had a File object.
If there are multiple records in the file. TMAX will end up containing the last value in the file, which might not be what you want.
[/quote]
ํYes all.