Pages: [1]   Go Down
Author Topic: Touble with saving RTC timestamp to SD (noob)  (Read 751 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all. I am a biologist researching penguins in South America. I have made a scale that logs the wieght and direction of the penguins as they move across the scale and then logs the data with a timestamp to a libelum microsd module.  

I made use of the following thread:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1257531430/0

I'm using the filelogger library and I've made liberal use of the code from various threads on the board, so sorry if it is a bit untidy.

It works fine except for the month, day and year entries.  Year is easy enough to fix, but the month entry increases by 1 every 24 hours and the day only cycles through 10 before resetting.  I've tested the coding for the clock without saving and it is fine so I think it has to do with the how the data is converted to be saved to the SD card.

Any help would be appreciated.

Code:
#include <Wire.h>
#include <FileLogger.h>
#define MEM_PW 8

# define LOAD1 0 //select the input pin for the sensor resistor
# define LOAD2 1
# define LOAD3 2
# define LOAD4 3
int second=0;
int hour=0;
int minute=0;
int month=0;
int day=0;
int year=0;

int val1 = 0; // variable to store the value coming sensor resistor
int val2 = 0;
int val3 = 0;
int val4 = 0;
int sumval = 0;
int nestside = 0;
int seaside = 0;
float weight=0;
byte inSerByte = 0; // variable used when reading from serial


void setup()
{ pinMode(MEM_PW, OUTPUT);
  digitalWrite(MEM_PW, HIGH);  //the MicroSD Module the power comes from a digital pin this activates it at all times
  Serial.begin(9600);//configure the serial port to command the card and read data
  Wire.begin();
}




void loop() {
  // Below required to reset the register address to 0.
  

val1 = analogRead(LOAD1); // read the valueS from the sensorS
  val2 = analogRead(LOAD2);  
  val3 = analogRead(LOAD3);  
  val4 = analogRead(LOAD4);  

  nestside = val3 + val4;  
  seaside = val1 + val2;

  sumval = nestside + seaside;
  
 
   if (sumval>100)
   {
     Wire.beginTransmission(104); // transmit to device #104, the ds 1307
  Wire.send(0x0);
  Wire.endTransmission();    // stop transmitting

  Wire.requestFrom(104, 7);    // request 7 bytes from slave ds1307, we'll assume it'll send them all even though it doesn't have to
   second = Wire.receive();
  minute = Wire.receive();
  hour = Wire.receive();
  day = Wire.receive();
  month = Wire.receive();
  year = Wire.receive();

  // Convert all the BCD values that might have "tens" to decimal.  Most arduino folks do this w/shifts but this just looks easier to me .
  int hour1=hour/16 * 10 + hour % 16;
  int minute1=minute/16 * 10 + minute % 16;
  int second1=second/16 * 10 + second % 16;
  int day1=day/16 * 10 + day % 16;
  int month1=month/16 * 10 + month % 16;
  int year1=2000 + year/16 * 10 + year % 16;

    
 weight= sumval;
long nestside1=nestside;
long seaside1 = seaside;
long weight1= weight;
long second2=second1;
long minute2=minute1;
long hour2=hour1;
long day2=day1;
long month2=month1;
long year2=year;
 
 char message[20];
 sprintf(message,"%ld,%ld,%ld,%ld,%ld,%ld,%ld,%1d,%1d,%ld,%1d,%1d/n ",nestside1, seaside1, weight1, second2, minute2, hour2, day2, month2, year2,0,0 );
 unsigned long length = strlen(message);
  
  FileLogger::append("hola.txt", (byte*)message, length);
 Serial.println (message);
 delay(800);
 }}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49071
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Wire.requestFrom(104, 7);    // request 7 bytes from slave ds1307, we'll assume it'll send them all even though it doesn't have to
   second = Wire.receive();
  minute = Wire.receive();
  hour = Wire.receive();
  day = Wire.receive();
  month = Wire.receive();
  year = Wire.receive();

The data being sent back is seconds, minute, hour, day-of-week, day, month, year. You can't just ignore day-of-week in the middle because you are not interested in it.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks...I have to go back to the field to test it out, I'll keep you posted on how things work.

Best,

Marc
Logged

Pages: [1]   Go Up
Jump to: