Hi guys, i've a problem. I've made a sheme with 3 OneWire devices DS18B20 and a self made microSD shield. An idea is - find all OneWire devices, register them in a table, ask each one 1 time in 5 seconds 12 times and print data to a serial port (port monitor of Arduino IDE) and to a file on microSD card. If i comment a part of program which write data to a file, all the devices are found, table and temperature data from them printed to a serial. When i try to write it parallel to microSD, programm write "error opening test.txt", BUT! if the file was not on microSD card, it magically appears with 0b size. If i try to write a simple data with a simple programm to a microSD card like "test 1, 2, 3" whithout any changing in wiring it works great. File appears, data writing to it and so on. Please help me, I'll be glad for any suggestion.
A simple programm, works great:
#include <SD.h>
File myFile;
void setup()
{
Serial.begin(9600);
Serial.print("Initializing SD card...");
// 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.
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
myFile.close();
Serial.println("done.");
} else {
Serial.println("error opening test.txt");
}
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
} else {
Serial.println("error opening test.txt");
}
}
void loop()
{
}
And my programm:
//use OneWire
#include <OneWire.h>
#include <SD.h>
//OneWire on pin 9
OneWire ds(9);
//variables and massives
int HighByte, LowByte, TReading, Tc_100, Whole, Fract, SignBit;
byte i=0,devicenum=0,k=0,j=0,s=0,tempdata[16],address[8],addressmap[10][8];
String devicetype[10];
File myFile;
//setting up
void setup(void)
{
Serial.begin(9600);
/*while devices appears, i find they addresses and write to a matrix whith 10 strings, so i use up to 10 devises in my programm*/
while (ds.search(address)){
for ( i = 0; i < 8; i++) addressmap[k][i]=address[i];
k=k+1;
}
//remember number of OneWire devices
devicenum = k-1;
/*for each device get a Byte which means type, and write it to another matrix*/
for (k=0; k<=devicenum; k++) {
switch (addressmap[k][0]) {
case 16: devicetype[k]="digital thermometer ds1820 (ds1920) series"; break;
case 32: devicetype[k]="4-channel 16 bit analog->digital converter"; break;
case 34: devicetype[k]="digital thermometer ds1822"; break;
case 40: devicetype[k]="digital thermometer ds18b20, up to 12 bit resolution"; break;
default: devicetype[k]="unknoun 1-Wire device"; break;
}
}
//print all the addresses and types of devices to a serial
for( k = 0; k <= devicenum; k=k+1) {
Serial.print("Device #"); Serial.print(k); Serial.print(" address is [ ");
for( i = 0; i < 8; i=i+1) {
Serial.print(addressmap[k][i], HEX);
Serial.print(" ");
}
Serial.print(']'); Serial.print(" type of device is "); Serial.println(devicetype[k]);
}
Serial.println("That's all list of online devices");
Serial.print("Initializing SD card...");
// 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
pinMode(10, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// try to open the file
myFile = SD.open("test.txt", FILE_WRITE);
// if ok, then print all the addresses and types of devices to a file:
if (myFile!=false) {
// print to a serial, that i work whith file
Serial.print("Writing device mass to test.txt");
for( k = 0; k <= devicenum; k=k+1) {
myFile.print("Device #"); myFile.print(k); myFile.print(" address is [ ");
for( i = 0; i < 8; i=i+1) {
myFile.print(addressmap[k][i], HEX);
myFile.print(" ");
}
myFile.print(']'); myFile.print(" type of device is "); myFile.println(devicetype[k]);
}
myFile.println("That's all list of online devices");
// print to a serial, that i work whith file
Serial.print("Writing devices data to test.txt");
// series of 12 measurements
for( s = 0; s < 12; s=s++) {
for( k = 0; k <= devicenum; k=k+1) {
for(i=0;i<8;i++) address[i]=addressmap[k][i];
ds.reset();
ds.select(address);
ds.write(0x44,1); // converting
delay(1000); //maybe 750ms is enough
ds.reset();
// we might do a ds.depower() here, but the reset will take care of it.
ds.select(address);
ds.write(0xBE); // please, write data to a scratchpad
for ( j = 0; j < 9; j++) { // getting 9 Byte data from Scratchpad - page 9 from 27 Datasheet DS18B20
tempdata[j] = ds.read();
// we need only 0 and 1 Byte from Scratchpad - page 9 from 27 Datasheet DS18B20
LowByte = tempdata[0];
HighByte = tempdata[1];
TReading = (HighByte << 8) + LowByte;
SignBit = TReading & 0x8000; // test most sig bit
if (SignBit) TReading = (TReading ^ 0xffff) + 1; // if negative, then 2's comp
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
Whole = Tc_100 / 100; // separate off the whole and fractional portions
Fract = Tc_100 % 100;
}
for (i=0; i<8; i++) Serial.print(address[i],HEX); Serial.print(" "); Serial.print(tempdata[0],HEX); Serial.print(" "); Serial.print(tempdata[1],HEX); Serial.print('\n');
// If its negative
Serial.print(" T = "); if (SignBit) Serial.print("-"); Serial.print(Whole); Serial.print("."); if (Fract < 10) Serial.print("0"); Serial.print(Fract); Serial.println(" C");
}
Serial.println("I'm waiting about 5 sec before next series of measurement");
delay (5000);
}
myFile.close();
Serial.println("Done.");
}
// if the file did not open, writing to a serial error
else Serial.println("error opening test.txt");
}
void loop(void)
{
}