Tuesday 6 November 2018

Arduino - V

In this short post on Arduino, we introduce the time element into the serial output. In the previous posts on Arduino, we have used LEDs and interacted with the serial monitor. More often than not, sensors are hooked to such boards and data is read live from these sensors. But, any sensor data makes sense only when the sensor data is captured along with the time. For all the work in this post, we will use the same Arduino Uno that we have used in previous environments.

There are multiple ways we can add the time element into sensor data that is read off Arduino including adding separate hardware, etc. While we may explore various such possibilities in the near future, we now focus on how to add the time element in the simplest possible manner. We do so by importing a new library called Time. Navigate as Tools --> Manage Libraries... as shown below:
















In the Library Manager window that comes up, search for Time as shown below and click on Install button against the latest version 1.5.0. In our case, this library has already been installed. So the Install button is grayed out:
















Once the library is installed, click on Close button. Then, paste below code:


#include <TimeLib.h>

void setup()  {
  Serial.begin(9600);
  setTime(1541502061);        //The argument to setTime must be Unix time 
  Serial.print("The hour in 24 hour format: ");
  Serial.println(hour());            // the hour in 24 hour format
  Serial.print("The hour in 12 hour format: ");
  Serial.println(hourFormat12());    // the hour in 12 hour format
  Serial.print("The minute in 60 minute format: ");
  Serial.println(minute());          // the minute in 60 minute format
  Serial.print("The second in 60 second format: ");
  Serial.println(second());          // the second in 60 second format
  Serial.print("The day of month: ");
  Serial.println(day());             // the day of month
  Serial.print("Day of the week starting with Sunday as 1: ");
  Serial.println(weekday());         // day of the week starting with Sunday as 1
  Serial.print("The month of year with January as 1: ");
  Serial.println(month());           // the month of year with January as 1
  Serial.print("The full four digit year: ");
  Serial.println(year());            // the full four digit year
  Serial.print("Returns 1 if time now is AM else 0: ");
  Serial.println(isAM());            // returns true if time now is AM
  Serial.print("Returns 1 if time now is PM else 0: ");
  Serial.println(isPM());            // returns true if time now is PM
  Serial.print("Returns the current time as seconds since January 1 1970: ");
  Serial.println(now());             // returns the current time as seconds since January 1 1970
}

void loop() {
}

To see the date elements in the serial output, we need to set Unix time as parameter in setTime. It is the number of seconds since 00:00:00 1st January 1970 as is indicated in the last line of above code. Upload the above code to Arduino Uno. The output in serial monitor is shown below:



















We will revisit the time part in later posts. This concludes the post