SD datalogger et OneWire (débutant)

Bonjour,

Pour un projet perso - capteur température et stockage SD - j'essaie d'ajouter à un sketch OneWire pour mon DS18B20 des lignes de code pour ajouter le "SD Datalogger" (qui est prévu pour plusieurs capteurs analogiques donc ...)
Je me retrouve forcément avec plusieurs fonctions "setup" et "loop"à la suite, et je ne sais pas comment avancer .. En espérant que ce soit la bonne façon de faire en fait :wink: Si quelqu'un peut m'aiguiller, merci beaucoup !

Mon code :


#include <OneWire.h>

// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// OneWire Arduino Library, connecting 1-wire devices (DS18S20, etc) to Teensy
//
// The DallasTemperature library can do all this work for you!
// MilesBurton.com

OneWire ds(10); // on pin 10 (a 4.7K resistor is necessary)

void setup(void) {
Serial.begin(9600);
}

void loop(void) {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;

if ( !ds.search(addr)) {
// Serial.println("No more addresses.");
Serial.println();
ds.reset_search();
delay(250);
return;
}

// Serial.print("ROM =");
// for( i = 0; i < 8; i++) {
// Serial.write(' ');
// Serial.print(addr*, HEX);*

  • // }*

  • if (OneWire::crc8(addr, 7) != addr[7]) {*

  • Serial.println("CRC is not valid!");*

  • return;*

  • }*

  • ds.reset();*

  • ds.select(addr);*

  • ds.write(0x44, 1); // start conversion, with parasite power on at the end*

  • delay(1000); // maybe 750ms is enough, maybe not*

  • // we might do a ds.depower() here, but the reset will take care of it.*

  • present = ds.reset();*

  • ds.select(addr); *

  • ds.write(0xBE); // Read Scratchpad*

  • // Serial.print(" Data = ");*

  • // Serial.print(present, HEX);*

  • Serial.print(" ");*

  • for ( i = 0; i < 9; i++) { // we need 9 bytes*
    _ data = ds.read();_
    // Serial.print(data*, HEX);*
    // Serial.print(" ");
    * }*
    * // Serial.print(" CRC=");*
    * // Serial.print(OneWire::crc8(data, 8), HEX);*
    * Serial.println();*
    * // Convert the data to actual temperature*
    * // because the result is a 16 bit signed integer, it should*
    * // be stored to an "int16_t" type, which is always 16 bits*
    * // even when compiled on a 32 bit processor.*
    * int16_t raw = (data[1] << 8) | data[0];
    if (type_s) {
    _ raw = raw << 3; // 9 bit resolution default*
    * if (data[7] == 0x10) {
    // "count remain" gives full 12 bit resolution*
    * raw = (raw & 0xFFF0) + 12 - data[6];
    }
    } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them*
    * if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms*
    * else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms*
    * else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms*
    * //// default is 12 bit resolution, 750 ms conversion time*
    * }
    celsius = (float)raw / 16.0;
    fahrenheit = celsius * 1.8 + 32.0;
    // Serial.print(" Temperature = ");
    Serial.print(celsius);
    // Serial.print(" Celsius, ");
    // Serial.print(fahrenheit);
    // Serial.println(" Fahrenheit");
    }
    /

    * SD card datalogger*
    onewire 10
    /

    #include <SD.h>
    // On the Ethernet Shield, CS is pin 4. Note that even if it's not*
    // used as the CS pin, the hardware CS pin (10 on most Arduino boards,
    // 53 on the Mega) must be left as an output or the SD library
    // functions will not work.
    const int chipSelect = 4;
    void setup()
    {
    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
    }
    * Serial.print("Initializing SD card...");
    // make sure that the default chip select pin is set to*
    * // output, even if you don't use it:
    pinMode(10, OUTPUT);*_

* // see if the card is present and can be initialized:*
* if (!SD.begin(chipSelect)) {*
* Serial.println("Card failed, or not present");*
* // don't do anything more:*
* return;*
* }*
* Serial.println("card initialized.");*
}
void loop()
{
* // make a string for assembling the data to log:*
* String dataString = "";*
* // read sensor and append to the string:*
* for (int OneWire = 10; OneWire < 3; OneWire++) {*
* int sensor = digitalRead(OneWire);*
* dataString += String(sensor);*
* if (OneWire < 2) {*
* dataString += ",";*
* }*
* }*
* // open the file. note that only one file can be open at a time,*
* // so you have to close this one before opening another.*
* File dataFile = SD.open("datalog.txt", FILE_WRITE);
_ // if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
*_</em> <em><em>**</em></em> <em>_**_