Go Down

Topic: Using the time library... (Read 466 times) previous topic - next topic


Oct 28, 2013, 09:14 pm Last Edit: Oct 29, 2013, 09:37 pm by rcrowen Reason: 1
Basically what I'm trying to do:
1. On button press, print the current date and then the date x amount of time later (which will depend ultimately on the choice of four different buttons, in this case I was trying for two days later).

2. Not sure how exactly I tell day() to add a value. My attempt did not work.


Code: [Select]

 This is an Arduino library for the Adafruit Thermal Printer.
 Pick one up at --> http://www.adafruit.com/products/597
 These printers use TTL serial to communicate, 2 pins are required.

 Adafruit invests time and resources providing this open source code.
 Please support Adafruit and open-source hardware by purchasing products
 from Adafruit!

 Written by Limor Fried/Ladyada for Adafruit Industries.
 MIT license, all text above must be included in any redistribution.

// If you're using Arduino 1.0 uncomment the next line:
#include "SoftwareSerial.h"
// If you're using Arduino 23 or earlier, uncomment the next line:
//#include "NewSoftSerial.h"

#include "Adafruit_Thermal.h"
#include "adalogo.h"
#include <avr/pgmspace.h>
#include <Time.h>

int printer_RX_Pin = 5;  // This is the green wire
int printer_TX_Pin = 6;  // This is the yellow wire
const int buttonPin = 2;   // the number of the pushbutton pin
int foodType = 1;

int buttonState = 0;  // variable for reading the pushbutton status

Adafruit_Thermal printer(printer_RX_Pin, printer_TX_Pin);

void setup(){
 pinMode(7, OUTPUT); digitalWrite(7, LOW); // To also work w/IoTP printer
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT);

void printDigits (int digits) {
  // utility function for clock display: prints preceding colon and leading 0
  if(digits < 10)

void loop(){
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);

 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
// This prints the current date
 if (buttonState == HIGH) {        
 printer.println(" ");
 printer.println(" ");
 printer.println(" ");

// This statement updates the date depending on type of food i.e. Vegetable, meat, etc.
 if (foodType == 1) {
   day = day + 2;
   printer.println("Use by this date: ");

 printer.sleep();      // Tell printer to sleep
 printer.wake();       // MUST call wake() before printing again, even if reset
 printer.setDefault(); // Restore printer to defaults  


Specifically, I'm talking about my if statement in which I have the code in question currently commented out. Thoughts?

The code does (did) something. What that is is a mystery.
You want the code to do something. What that is is a mystery.

My thought is that you need to finish asking your question.

Code: [Select]
     // This prints the current date of fridge going into fridge
This does what?

Code: [Select]
  if (foodType = 1) {
= != ==

The art of getting good answers lies in asking good questions.


Thanks for the response. Updated original to hopefully make a little clearer.


Jack Christensen

It's worth reading about the Time library and also looking through the actual code (specifically the Time.h and Time.cpp files).

The Time library defines a couple new data types that you will want to be familiar with.  It also defines a bunch of functions for working with these data types.

The time_t data type represents a particular point in time, to a resolution of one second.  Specifically, the value held in a time_t variable is just the number of seconds since 00:00:00 01Jan1970 (the Unix epoch; the Arduino Time library has a lot in common with the standard C time library).

Actually time_t is just an unsigned long integer.

To conveniently determine the month, day, year, hour, minute or second represented by a time_t variable, there are a bunch of functions that are explained on the page linked above.

Not documented at the link above is another data type called tmElements_t.  This is actually a structure that has different members representing year, month, day, etc.  Then there are two functions for converting between a time_t variable and a tmElements_t structure, makeTime() and breakTime().

Very useful library once you get the hang of it.  Have a look, then if you don't get it, ask questions.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131