Uso arduino uno R3 e ide 1.0.3
ho una wireless sd shield montata su e diversi sensori.
Vorrei salvare i dati su sd ma mi accorgo da seriale che, a partire dal 15esimo valore, non apre il file data.txt.
Cosa può essere?
Senza codice non possiamo saperlo neanche noi
Se non hai una shield Wifi col vecchio firmware puoi tranquillamente aggiornare l'IDE alla 1.0.6 o alla beta 1.5.8.
Inoltre senza vedere lo sketch è difficile risponderti.
Eccolo: è abbastanza lungo ed occupa 21.608 bytes
#include <dht.h> //library for DHT22
#include "kSeries.h" //library for K-30_CO2_sensor
#include <SD.h> //library to write to SD card
dht DHT;//DHT22
#define DHT22_PIN 5 //DHT22 is plugged into pin 5 on the Arduino
#define analogPinForRV 1 // MD wind sensor. RV is plugged into pin A1 on the Arduino
#define analogPinForTMP 0 // MD wind sensor. TMP is plugged into pin A0 on the Arduino
const float zeroWindAdjustment = .2; //MD wind sensor. To calibrate your sensor, put a glass over it, but the sensor should not be touching the desktop surface however. Adjust the zeroWindAdjustment until your sensor reads about zero with the glass over it.
int TMP_Therm_ADunits; //MD wind sensor. temp termistor value from wind sensor
float RV_Wind_ADunits; //MD wind sensor. RV output from wind sensor
float RV_Wind_Volts; //MD wind sensor.
unsigned long lastMillis; //MD wind sensor.
int TempCtimes100; //MD wind sensor.
float zeroWind_ADunits; //MD wind sensor.
float zeroWind_volts; //MD wind sensor.
float WindSpeed_MPH; //MD wind sensor.
float A; //MD wind sensor.
float B; //MD wind sensor.
kSeries K_30(6,8); //Create K30 instance on pin 6 & 8
int LDR_A0 = A2; //photoresistor is plugged into analog pin 2 on the Arduino
float Vin_div_values = 0.004882812; //= 5/1024 dove: 5 =Vin e 1024 aono i valori massimi di analogRead
float Vout0 = 0;
float R0 = 0;
float R10k0 = 9739; //resistor in ohm measured
float R1_0 = 94000;
float Lux0 = 0;
float a = 0.7;
const int thermistor = A3; //thermistor10k is plugged into analog pin 3 on the Arduino
File myFile; //SD card: define myFile
void setup()
{
Serial.begin(9600);
pinMode(10, OUTPUT); // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
myFile = SD.open("data.txt", FILE_WRITE); // open the file. note that only one file can be open at a time,
// if the file opened okay, write to it:
if (myFile) {
myFile = SD.open("data.txt", FILE_WRITE);
myFile.println("DHT22_H(%), DHT22_T(C), Thermistor_T(C), AS(MPH), Co2(ppm), LDR (lx) ");
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening data.txt");
}
//Serial.println();
//Serial.println("DHT22_H(%), DHT22_T(C), Thermistor_T(C), AS(MPH), Co2(ppm), LDR (lx) ");
}
void loop()
{
int chk = DHT.read22(DHT22_PIN); //DHT11 reads data
//Serial.print(DHT.humidity, 2); //DHT11 prints humidity value in °C. Il numero indica le cifre decimali del DHT22
//Serial.print(", "); // prints a comma and a space
//Serial.print(DHT.temperature, 2); //DHT11 prints temperature value in °C. Il numero indica le cifre decimali del DHT22
float t = 0.22+((3976/log(((10000*5.0/((5.0 * analogRead(thermistor))/1024))-10000)/0.016167))-273.15); // read temperature from thermistor (+0.22 to align the values)
//Serial.print(", "); // prints a comma and a space
//Serial.print(t); // prints temperature value in °C.
//Serial.print(", "); // prints a comma and a space
//below instructions for the MD wind sensor:
if (millis() - lastMillis > 200){ // read every 200 ms - printing slows this down further
TMP_Therm_ADunits = analogRead(analogPinForTMP);
RV_Wind_ADunits = analogRead(analogPinForRV);
RV_Wind_Volts = (RV_Wind_ADunits * 0.0048828125);
//define the variable A and B:
B = 0.00485785789945844;
A= -4.089 * RV_Wind_Volts * RV_Wind_Volts + 22.697 * RV_Wind_Volts - 29.371;
// these are all derived from regressions from raw data as such they depend on a lot of experimental factors
// such as accuracy of temp sensors, and voltage at the actual wind sensor, (wire losses) which were unaccouted for.
TempCtimes100 = (0.005 *((float)TMP_Therm_ADunits * (float)TMP_Therm_ADunits)) - (16.862 * (float)TMP_Therm_ADunits) + 9075.4;
zeroWind_ADunits = -0.0006*((float)TMP_Therm_ADunits * (float)TMP_Therm_ADunits) + 1.0727 * (float)TMP_Therm_ADunits + 47.172; // 13.0C 553 482.39
zeroWind_volts = (zeroWind_ADunits * 0.0048828125) - zeroWindAdjustment;
WindSpeed_MPH = pow(((RV_Wind_Volts - zeroWind_volts) /.2300) , 2.7265);
//Serial.print((float)WindSpeed_MPH);
//Serial.print(", "); // prints a comma and a space
}
//end instructions for the MD wind sensor
double co2 = K_30.getCO2('p'); // k30 sensor. Get CO2 value from sensor
//Serial.print("Co2 ppm = "); //k30 sensor. Print the value on Serial
//Serial.print(co2); //k30 sensor. Prints the value on Serial
//Serial.print(", "); // prints a comma and a space
Vout0 = (float(Vin_div_values * analogRead(LDR_A0)));
R0 = ((5/Vout0*R10k0)-R10k0);
Lux0 = pow((R0/R1_0),(1/-a));
//Serial.println(Lux0);
//instruction to write all data to sd card
myFile = SD.open("data.txt", FILE_WRITE); //first open the file.
//then, write on it
if (myFile) {
myFile = SD.open("data.txt", FILE_WRITE);
myFile.print(DHT.humidity, 2); //DHT22 prints on the SD Card humidity value in °C. Il numero indica le cifre decimali del DHT11
myFile.print(", "); // prints on the SD Card a comma and a space
myFile.print(DHT.temperature, 2); //DHT22 prints on the SD Card temperature value in °C. Il numero indica le cifre decimali del DHT11
myFile.print(", "); // prints on the SD Card a comma and a space
myFile.print(t); // thermistor prints on the SD Card temperature value in °C.
myFile.print(", "); // prints on the SD Card a comma and a space
myFile.print((float)WindSpeed_MPH); //MD wind speed prints on the SD Card the value
myFile.print(", "); // prints on the SD Card a comma and a space
myFile.print(co2); //k30 sensor prints on the SD Card the value
myFile.print(", "); // prints on the SD Card a comma and a space
myFile.println(Lux0); //photoresistor prints on the SD Card the value
myFile.close();
Serial.println("data printed on data.txt");
} else {
// if the file didn't open, print an error:
Serial.println("error opening data.txt");
}
delay(10000);
}
Non riesco a compilarlo perché mi mancano 2 lib, ma sarebbe da vedere il consumo di RAM. Intanto prova utilizzando la funzione F() per tutte le stringhe costanti. Ad esempio da così:
Serial.println("initialization failed!");
a così:
Serial.println(F("initialization failed!"));
Per ridurre il consumo di RAM.
Ok, ho utilizzato per tutti i Serial.print la funzione F. Gli altri variabili li avevo già disattivati facendoli diventare dei commenti. Ora arrivo a 17 scritture poi "error opening data.txt".
leo72:
Non riesco a compilarlo perché mi mancano 2 lib, ma sarebbe da vedere il consumo di RAM.
Ecco il link alle 3 librerie:
Ok, ho risolto sostituendo la libreria di default SD con SdFat.