LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-27-2016, 04:50 PM   #1
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Mint Suse
Posts: 362

Rep: Reputation: 30
How to make string from displayTime()


In the code below displayTime() prints the date/time/day to the serial monitor in the form: 28/8/2016 9:45:00 Sunday

Code:
void loop()
{  
   displayTime(); // display the real-time clock data on the Serial Monitor,   
  
  delay(2000); // every second
}
How can I 'extract' the date/time/day to form a string that I can use in conjunction with other strings to send to SD card?
 
Old 08-27-2016, 05:18 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
Any of the time formatting functions listed in the ctime(3) man page.

Last edited by jpollard; 08-27-2016 at 05:19 PM.
 
Old 08-27-2016, 05:41 PM   #3
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Mint Suse
Posts: 362

Original Poster
Rep: Reputation: 30
Thank you for your quick reply. Unfortunately the man page is beyond my comprehension. Could you give me an example?
 
Old 08-27-2016, 05:46 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
Google is easy to use:

http://www.tutorialspoint.com/c_stan...tion_ctime.htm

Just search for "examples of using ctime" for instance.
 
Old 08-27-2016, 06:18 PM   #5
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Mint Suse
Posts: 362

Original Poster
Rep: Reputation: 30
I'm not sure whether we are at cross understanding, or I'm displaying my ignorance. I have read the current time from the RTC and created displayTime(), which writes the result to the serial monitor. What I'm seeking is some way to 'extract' the contents of displayTime() into a string.
 
Old 08-27-2016, 07:31 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
If you wrote displayTime then you should know how to use the data already.

BTW, the rtc time is less accurate than the system time. It is only used to set the system time at boot, after which it is updated independently. When the system shuts down, the rtc is reset to what the system has.

In addition, most systems are using NTP (or equivalent) to keep the system time even more accurately. Thus the rtc only serves as an initial "guess" after which NTP is used to get the clock to the correct time. Again, the rtc is really an inaccurate clock as it gains/loses time just due to environment changes.

Last edited by jpollard; 08-27-2016 at 07:37 PM.
 
Old 08-27-2016, 08:26 PM   #7
af7567
Member
 
Registered: Nov 2012
Posts: 213

Rep: Reputation: 78
Quote:
Originally Posted by Johng View Post
In the code below displayTime() prints the date/time/day to the serial monitor in the form: 28/8/2016 9:45:00 Sunday

Code:
void loop()
{  
   displayTime(); // display the real-time clock data on the Serial Monitor,   
  
  delay(2000); // every second
}
How can I 'extract' the date/time/day to form a string that I can use in conjunction with other strings to send to SD card?
If you make your displayTime function return the string then you can use it out of the displayTime function.

Code:
void loop()
{
   char *date_time_day;

   date_time_day = displayTime();  // display clock and return string
   printf("%s\n", date_time_day);  // print string to stdout

   delay(2000); //sleep 2 seconds
}
 
Old 08-27-2016, 11:22 PM   #8
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Mint Suse
Posts: 362

Original Poster
Rep: Reputation: 30
Thank you again folks.

af7567, your suggestion returned: 164:18: error: void value not ignored as it ought to be (164:18 was the "=")

Here is the complete Arduino sketch:

Code:
#include "Wire.h"
#include <SPI.h>
#include <SD.h>

const int chipSelect = 10;  //added


#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );
}

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


//  Serial.print("Initializing SD card...");

  // 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 readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{

  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}

void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  

  // send it to the serial monitor

  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print("20");  
  Serial.print(year, DEC);

  Serial.print("  ");

  Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  Serial.print(":");
  if (minute<10)
  {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second<10)
  {
    Serial.print("0");
  }
  Serial.print(second, DEC);


  Serial.print("  ");        // Serial.print(" Day of week: ");
  switch(dayOfWeek){
  case 1:
    Serial.println("Sunday");
    break;
  case 2:
    Serial.println("Monday");
    break;
  case 3:
    Serial.println("Tuesday");
    break;
  case 4:
    Serial.println("Wednesday");
    break;
  case 5:
    Serial.println("Thursday");
    break;
  case 6:
    Serial.println("Friday");
    break;
  case 7:
    Serial.println("Saturday");
    break;
  }
}


void loop()
{
 // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }

  // open the file.
  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.print(dataString);
    
//    Serial.println(displayTime);
  
 //  displayTime(); // display the real-time clock data on the Serial Monitor,   
     displayTime(); 
  
    
  
  delay(10000); // every second
}
}
The serial monitor output looks like this:
Quote:
0,482,1023 28/8/2016 16:17:59 Sunday
0,481,1023 28/8/2016 16:18:09 Sunday
0,482,1023 28/8/2016 16:18:19 Sunday
The SD card looks like this, no date/time/day:
Quote:
0,482,1023
0,482,1023
0,482,1023
All I am trying to do at this stage is to learn how to get the SD card to record the same data as displayed on the serial Monitor
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how make community string in snmp ghaedi591 Linux - Newbie 1 06-07-2012 09:57 AM
[SOLVED] Separating words from a string in Make sree_ec Programming 2 05-06-2012 03:16 PM
[SOLVED] I'm not getting the proper $KERNELRELEASE string after make rpm markings Linux - Kernel 4 04-30-2012 03:17 PM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
56k string to make speed consistant ? blackcat09 Linux - Networking 1 11-21-2009 07:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration