Ok, finally i figured out how to use the ftoa function. Here is my code to read temperature data from one DB18B20 and to store it on the SD-Card in the libelium module. It worked once (with a 15mb hola.txt filled up with ETX caracters...don´t ask) and after i tried it with a normal hola.txt it wont work again. I´m sure the code works so it has something to do with this bloody hola.txt/ETX/libelium thing. I will abandon this buggy module an try to get this thing working on an S65 shield....
Anyway, here ist my code. I hope the comments are useful.
Greetings,
Jean
#include <OneWire.h>
#include <DallasTemperature.h>
#include "SDuFAT.h"
// Data wire is plugged into port 7 on the Arduino
#define ONE_WIRE_BUS 7
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device address
DeviceAddress insideThermometer;
// variable used when reading from serial
byte inSerByte = 0;
void setup(void)
{
// start serial port
Serial.begin(9600);
// locate devices on the bus
Serial.print("Locating devices...");
sensors.begin();
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// assign address manually. the addresses below will beed to be changed
// to valid device addresses on your bus. device address can be retrieved
// by using either oneWire.search(deviceAddress) or individually via
// sensors.getAddress(deviceAddress, index)
//insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
// Method 1:
// search for devices on the bus and assign based on an index. ideally,
// you would do this to initially discover addresses on the bus and then
// use those addresses and manually assign them (see above) once you know
// the devices on your bus (and assuming they don't change).
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
// show the addresses we found on the bus
Serial.print("Device 0 Address: ");
printAddress(insideThermometer);
Serial.println();
// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(insideThermometer, 10);
Serial.print("Device 0 Resolution: ");
Serial.print(sensors.getResolution(insideThermometer), DEC);
Serial.println();
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
// function to print the temperature for a device to serial
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.println(tempC);
//Serial.print(" Temp F: ");
//Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}
// function to store temp-data on the card
void saveTemperature(DeviceAddress deviceAddress)
{
char float_conv[10]; // array to hold the data for the ftoa fuction
float temp1 = sensors.getTempC(deviceAddress); // variable to store temperature before converting
SD.println("hola.txt", ftoa(float_conv, temp1, 2)); // saving the directly converted temp1 to card
}
// function to convert the temperature (given in float) to a string
// (http://www.arduino.cc/cgi-bin/yabb
/YaBB.pl?num=1164927646)
char *ftoa(char *a, double f, int precision)
{
long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
char *ret = a;
long heiltal = (long)f;
itoa(heiltal, a, 10);
while (*a != '\0') a++;
*a++ = '.';
long desimal = abs((long)((f - heiltal) * p[precision]));
itoa(desimal, a, 10);
return ret;
}
void loop(void) {
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
//Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
//Serial.println("Done");
// It responds almost immediately. Let's print out the data
printTemperature(insideThermometer); // Use a simple function to print out the data
delay(100);
saveTemperature(insideThermometer); // use the above created function to save the data to the card
delay(4900); //delay because of delay
}