Notifications
Clear all

RTC Project and Datalogging to an SD

27 Posts
5 Users
0 Likes
6,753 Views
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@berner @robo-pi

Looks like I owe you guys an apology, it wasn't your code I was criticising, it was Bills.

So I guess I have now committed heresy, by posting my version of how this should be done.


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 
Posted by: @pugwash

Looks like I owe you guys an apology, it wasn't your code I was criticising, it was Bills.

So I guess I have now committed heresy, by posting my version of how this should be done.

DroneBot Heresy

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
Spyder
(@spyder)
Member
Joined: 5 years ago
Posts: 846
 
Posted by: @pugwash

Fahrenheit is only kept to humor {American spelling) Americans! Other than that it is of no use at all and has to be calculated.

One day, we might even get them to stop measuring wavelength in feet and inches!

There was a Thursday, quite some number of decades ago, when all of America was supposed to be switching over to the metric system

But, Americans, as they are wont to be, were quite adverse to the change in their daily lifestyle, clinging as they do to their guns and their Bibles, and their foot long sandwiches

So, when this fateful Thursday in November rolled around when they were supposed to give up their standard measuring sticks, and footwear whose size is actually based on the size of a barleycorn rather than... feet, they promptly, and as a whole, completely ignored the entire absurd idea, and instead opted to give up smoking for a day


   
ReplyQuote
Berner
(@berner)
Member
Joined: 5 years ago
Posts: 31
Topic starter  
Posted by: @pugwash

@berner

Not sure if this is the place to ask this one, but does anyone know if the AM2320 is capable of outputting the temp in F? In my other test sketches I figured how to do it with a formula in the code, but just thought I would ask. Again..... thanks.

I find it hard to believe you asked this question. ICs that output Fahrenheit are about as rare as "rocking horse manure". Fahrenheit is only kept to humor {American spelling) Americans! Other than that it is of no use at all and has to be calculated.

One day, we might even get them to stop measuring wavelength in feet and inches! ? ? ? ? ? 

Edit: I am not entirely correct, Fahrenheit can be used for calculating BTUs, but I prefer joules!

I think the MCP9808  has functionality for rocking horse manure, so I thought I would ask.  I do agree with your commentary though, here in the states we were taught early on to learn metric because it was coming! Only it never did, what happened with that? Oh wait, I know, its a system that makes sense and we cant have that now can we! Didn't we even lose a Mars rover because someone missed a metric conversion? I guess I wont ask for the code to convert ounces to stones. ? 

In all seriousness though, the reason I asked about looking for Fahrenheit was because I am assisting with my sons 7th grade STEM class and thought making a small sensor project would be fun. Since everyone relates to F, I thought I would want to display results in that way. 

I also wanted to thank you for the code assist, it helped me a lot. I am still working through some things on it as I try to understand it. ? 

 

 


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@berner

Sorry but there is nothing on this datasheet that mentions Fahrenheit!

 

Didn't we even lose a Mars rover because someone missed a metric conversion?

Yes, it was an ESA/NASA joint venture, the descent was calculated in feet per second but the landing actually happened in metres per second! SPLATT!


   
ReplyQuote
Berner
(@berner)
Member
Joined: 5 years ago
Posts: 31
Topic starter  

@pugwash

MCP9808: Then I think the library I used had it built in function to do that... I just know I didn't have to do a conversion. 


   
ReplyQuote
Berner
(@berner)
Member
Joined: 5 years ago
Posts: 31
Topic starter  

   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@berner

I can't see any logical reason why your SD card is being written to every second!

Can you attach the whole sketch to your next post, there may be a glitch elsewhere.


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@berner

This might help, it is at least worth a try!

String print2digits(int number){
  String tString = String(number);
    if (tString.length() < 2){
    tString = "0" + tString;
  }
  return tString;
}

// Print to the serial monitor

void printCurrentTime(float hum, float tem){
  tmElements_t tm;
  String dataString;
  if (RTC.read(tm)) {
    dataString = print2digits(tm.Hour) + ":" +
    print2digits(tm.Minute) + ":" +
    print2digits(tm.Second) + "-" +
    tmYearToCalendar(tm.Year) + "-" +
    print2digits(tm.Month) + "-" +
    print2digits(tm.Day) +
    " Hum: " + hum + "% - Temp: " + tem + " C" + "\n";

  Serial.println(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("datalog1.txt", FILE_WRITE);

// if the file is available, write to it:

if (dataFile) {
    dataFile.print(dataString);
    dataFile.close();
  }else {
    Serial.println("error opening datalog.txt");
  }
}
}

Best of luck, let me know if it works or not!


   
ReplyQuote
Berner
(@berner)
Member
Joined: 5 years ago
Posts: 31
Topic starter  

@pugwash

Maybe my brain is getting too mushy..... I worked on so many copies of my code over the weekend I very well may have crossed a few wires. I have my original sketches on my workbench setup, I will grab that when I get home and send a copy. 


   
ReplyQuote
Berner
(@berner)
Member
Joined: 5 years ago
Posts: 31
Topic starter  

Nevermind! I just came home and looked at my code, I missed an end bracket for the section that deals with updating the serial monitor every second. I think I am good to go (for the moment ? ). You need a well organized thought process for coding and an eye for detail. 


   
ReplyQuote
dxj
 dxj
(@dxj)
Member
Joined: 4 years ago
Posts: 27
 

You can eliminate many lines of code and unneeded sub-routines by using the sprintf function. There are aged old arguments one way or the other ... readability vs. maintainability vs. efficiency etc. It's not without it's drawbacks, but thought I'd mention it for those interested.

/* Get date/time into a readable format */
char datastr[22];
sprintf(datastr, "%4d/%02d/%02d - %02d:%02d:%02d",
    year(), month(), day(), hour(), minute(), second());
Serial.println(datastr) // example: 2020/02/13 - 20:59:44

p.s. - Make sure your char buffer is large enough or you can use snprintf instead. Also Arduino doesn't support floats in sprintf, so you can use the dtostrf function for floats.

https://arduinobasics.blogspot.com/2019/05/sprintf-function.html

This post was modified 4 years ago 2 times by dxj

   
ReplyQuote
Page 2 / 2