Here is final version of code
//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];
char* 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!=NULL)
{
// print to a serial, that i work whith file
Serial.println("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.println("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++) tempdata[j] = ds.read(); // getting 9 Byte data from Scratchpad - page 9 from 27 Datasheet DS18B20
// 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++) myFile.print(address[i],HEX);
myFile.print(" ");
myFile.print(tempdata[0],HEX);
myFile.print(" ");
myFile.print(tempdata[1],HEX);
myFile.print('\n');
myFile.print(" T = ");
// If its negative
if (SignBit) myFile.print("-");
myFile.print(Whole);
myFile.print(".");
if (Fract < 10) myFile.print("0");
myFile.print(Fract);
myFile.println(" C");
}
Serial.println("I'm waiting about 2 sec before next series of measurement");
delay (2000);
}
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)
{
}