12-12-2017, 13:21
(Ten post był ostatnio modyfikowany: 12-12-2017, 17:17 przez CEO.
Powód edycji: Proszę edytować bieżący post i nie pisać jednego pod drugim
)
Kod:
#include <SPI.h>
#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DS1302.h>
DS1302 rtc(8, 9, 10);
char file;
char currDate;
File myFile;
#define ONE_WIRE_BUS 5 //wyjscie dla DS18B20
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
rtc.halt(false);
rtc.writeProtect(false);
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
sensors.requestTemperatures(); // Send the command to get temperature readings
// Serial.print("Temperatura za zdjęciami: ");
// Serial.print(sensors.getTempCByIndex(0));
// Serial.println(" *C ");
myFile = SD.open("test5.csv", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
for(int i=0; i <= 15; i++){
//Serial.print("Temperatura: ");
myFile.print(rtc.getDateStr());
myFile.print(" ");
myFile.print(rtc.getTimeStr());
myFile.print(" ");
myFile.println(sensors.getTempCByIndex(0));
delay(4000);
}
// close the file:
myFile.close();
Serial.println("done.");
}
else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
myFile = SD.open("test5.csv");
if (myFile) {
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
int temp;
void loop() {
// nothing happens after setup
}
Pozdrawiam,
Piotr
EDIT:
Pomyślałem trochę nad tym i wymyśliłem taką prowizorkę:
Kod:
#include <SPI.h>
#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DS1302.h>
DS1302 rtc(8, 9, 10);
char date;
char currDate;
File myFile;
#define ONE_WIRE_BUS 5 //wyjscie dla DS18B20
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
rtc.halt(false);
rtc.writeProtect(false);
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// Serial.print("Temperatura za zdjęciami: ");
// Serial.print(sensors.getTempCByIndex(0));
// Serial.println(" *C ");
}
int temp;
void loop() {
date=rtc.getDateStr();
date=date+".csv";
sensors.requestTemperatures(); // Send the command to get temperature readings
myFile = SD.open(&date, FILE_WRITE);
if (myFile) {
//Serial.print("Temperatura: ");
myFile.print(rtc.getDateStr());
myFile.print(" ");
myFile.print(rtc.getTimeStr());
myFile.print(" ");
myFile.println(sensors.getTempCByIndex(0));
// close the file:
myFile.close();
Serial.println("done.");
}
else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
myFile = SD.open(date);
if (myFile) {
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.print("error opening ");
Serial.println(date);
}
delay(4000);
currDate=rtc.getDateStr();
currDate=currDate+".csv";
if(currDate!=date)
date=currDate;
}