Hi everybody
This is my first time here and i'm getting started with Arduino.
As my first project i'm trying to get temperatures from 5 DS18B20 sensors and save them in a Sd card. Almost everything works, but i can't update the date and time
I'm using Arduino Uno and a PCF8523 as you can see in the image.
my code and a snapshot of the serial monitor ate the ones attached.
Can someone help me?
//#include <TimeLib.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
//#include <DS1307RTC.h>
//For tempsensor
#include <OneWire.h>
#include <DallasTemperature.h>
// A simple data logger for the Arduino analog pins
// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL 1000 // mills between entries (reduce to take more/faster data)
// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 0 // Wait for serial input in setup()
// the digital pins that connect to the LEDs
#define redLEDpin 2
#define greenLEDpin 3
#define BANDGAPREF 14 // special indicator that we want to measure the bandgap
#define aref_voltage 3.3 // we tie 3.3V to ARef and measure it with a multimeter!
#define bandgap_voltage 1.1 // this is not super guaranteed but its not -too- off
// Setting tempsensor variabels
// Koppla in enligt:
// http://www.14core.com/wp-content/uploads/2015/11/Temperature-Sensor-Dallas-Pinout-Diagram.jpg
const char tempPin2=2;
const char tempPin3=3;
const char tempPin4=4;
const char tempPin5=5;
const char tempPin6=6;//= 2;
OneWire oneWire2(tempPin2);
DallasTemperature DS18B202(&oneWire2);
OneWire oneWire3(tempPin3);
DallasTemperature DS18B203(&oneWire3);
OneWire oneWire4(tempPin4);
DallasTemperature DS18B204(&oneWire4);
OneWire oneWire5(tempPin5);
DallasTemperature DS18B205(&oneWire5);
OneWire oneWire6(tempPin6);
DallasTemperature DS18B206(&oneWire6);
float Temp[5];// = 0; // Digital port where the sensor is connected
int tempSensor[5]; //first is zeri, second=1, third =2....
RTC_DS1307 RTC; // define the Real Time Clock object
// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;
// the logging file
File logfile;
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
// red LED indicates error
digitalWrite(redLEDpin, HIGH);
while(1);
}
void setup(void)
{
Serial.begin(9600);
Serial.println();
RTC.begin();//////////////////////////////////////////////////////
//setSyncProvider(RTC.get);/////////////////////////////////////////
tempSensor[0] = 0;
tempSensor[1] = 0;
tempSensor[2] = 0;
tempSensor[3] = 0;
tempSensor[4] = 0;
// use debugging LEDs
pinMode(redLEDpin, OUTPUT);
pinMode(greenLEDpin, OUTPUT);
#if WAIT_TO_START
Serial.println("Type any character to start");
while (!Serial.available());
#endif //WAIT_TO_START
// initialize the SD card
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)) {
error("Card failed, or not present");
}
Serial.println("card initialized.");
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
// connect to RTC
Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
logfile.println("millis,stamp,datetime,light,temp pin 2,temp pin 3,temp pin 4,temp pin 5,temp pin 6,vcc");
#if ECHO_TO_SERIAL
Serial.println("millis,stamp,datetime,light,temp pin 2,temp pin 3,temp pin 4,temp pin 5,temp pin 6,vcc");
#endif //ECHO_TO_SERIAL
}
void loop(void)
{
DateTime now;
// delay for the amount of time we want between readings
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
digitalWrite(greenLEDpin, HIGH);
// log milliseconds since starting
uint32_t m = millis();
logfile.print(m); // milliseconds since start
logfile.print(", ");
#if ECHO_TO_SERIAL
Serial.print(m); // milliseconds since start
Serial.print(", ");
#endif
// fetch the time
now = RTC.now();
// log time
logfile.print(now.unixtime()); // seconds since 1/1/1970
logfile.print(", ");
logfile.print('"');
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(" ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
logfile.print('"');
#if ECHO_TO_SERIAL
Serial.print(now.unixtime()); // seconds since 1/1/1970
Serial.print(", ");
Serial.print('"');
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.print('"');
#endif //ECHO_TO_SERIAL
// reed Tempsensor
DS18B202.requestTemperatures();
Temp[0] = DS18B202.getTempCByIndex(tempSensor[0]);
DS18B202.requestTemperatures();
Temp[1] = DS18B203.getTempCByIndex(tempSensor[1]);
DS18B204.requestTemperatures();
Temp[2] = DS18B204.getTempCByIndex(tempSensor[2]);
DS18B205.requestTemperatures();
Temp[3] = DS18B205.getTempCByIndex(tempSensor[3]);
DS18B206.requestTemperatures();
Temp[4] = DS18B206.getTempCByIndex(tempSensor[4]);
// If temps are 85... something is just not right
if ((Temp[0] == 85)) {
Serial.println("Error in temp values... retry");
delay(5000);
} /*else {
//Serial.print("Temp=");
Serial.println(Temp);
//Serial.println(" *C");
}*/
// converting that reading to voltage, for 3.3v arduino use 3.3, for 5.0, use 5.0
//float voltage = tempReading * aref_voltage / 1024;
//float temperatureC = (voltage - 0.5) * 100 ;
//float temperatureF = (temperatureC * 9 / 5) + 32;
logfile.print(", ");
logfile.print(Temp[0]);
logfile.print(", ");
logfile.print(Temp[1]);
logfile.print(", ");
logfile.print(Temp[2]);
logfile.print(", ");
logfile.print(Temp[3]);
logfile.print(", ");
logfile.print(Temp[4]);
#if ECHO_TO_SERIAL
Serial.print(", ");
Serial.print(Temp[0]);
Serial.print(", ");
Serial.print(Temp[1]);
Serial.print(", ");
Serial.print(Temp[2]);
Serial.print(", ");
Serial.print(Temp[3]);
Serial.print(", ");
Serial.print(Temp[4]);
#endif //ECHO_TO_SERIAL
// Log the estimated 'VCC' voltage by measuring the internal 1.1v ref
analogRead(BANDGAPREF);
delay(10);
int refReading = analogRead(BANDGAPREF);
float supplyvoltage = (bandgap_voltage * 1024) / refReading;
logfile.print(", ");
logfile.print(supplyvoltage);
#if ECHO_TO_SERIAL
Serial.print(", ");
Serial.print(supplyvoltage);
#endif // ECHO_TO_SERIAL
logfile.println();
#if ECHO_TO_SERIAL
Serial.println();
#endif // ECHO_TO_SERIAL
digitalWrite(greenLEDpin, LOW);
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
// which uses a bunch of power and takes time
if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();
// blink LED to show we are syncing data to the card & updating FAT!
digitalWrite(redLEDpin, HIGH);
logfile.flush();
digitalWrite(redLEDpin, LOW);
}

