Arduino Forum

Using Arduino => Networking, Protocols, and Devices => Topic started by: french_guy on Jun 17, 2019, 12:49 pm

Title: Arduino Pro Mini and DS3231
Post by: french_guy on Jun 17, 2019, 12:49 pm
Hello

I have a Pro Mini 5V (from Microcenter - Inland brand) with the FTDI programmer
Both seems to work fine since I can upload the blink program
I'm now trying to setup a RTC (DS3231) but can't find something that work
I'm using the code from the Examples>TRClib>DS3231................but i receive weird values thru Monitor
Could anyone point me to a link that explained in details what to do exactly to (1) setup the time once and for all and (2) read the time?
Thanks
Title: Re: Arduino Pro Mini and DS3231
Post by: srnet on Jun 17, 2019, 01:08 pm
Can you read the post "How to use this Foum - please read" and follow the guidelines.



You have said you have a problem, but you have not posted the code that is causing the problem.

You are maybe using a library for the DS3231, but not told us which one.

Do not assume that everyone has the \examples\TRCLib folder, that probably comes with a specific library.

Explain your problem in some detail, for instance what are 'weird values'. 
Title: Re: Arduino Pro Mini and DS3231
Post by: french_guy on Jun 18, 2019, 12:24 pm
Sorry for the lack of information...
I did a test with a Uno yesterday, and it works
I realized I was not using the I2C pins (A4,A5) in the Pro Mini since they both need to be welded
I will try to find some connectors and make tests, but I'm pretty sure it will work
Thanks
Title: Re: Arduino Pro Mini and DS3231
Post by: french_guy on Jun 22, 2019, 07:17 pm
I'm "playing" with Arduino and the DS3231
I'm using the following code:
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3f,20,4);
RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {

#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero
#endif

  Serial.begin(9600);

  delay(3000); // wait for console opening

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
}

void loop () {
    DateTime now = rtc.now();
   
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
   
    lcd.init();                      // initialize the lcd
    lcd.backlight();
    lcd.setCursor(0,0);
    lcd.print ("DATE:");
    lcd.setCursor(6,0);
    lcd.print(now.month());
    lcd.print ("/");
    lcd.print (now.day());
    lcd.print ("/");
    lcd.print(now.year());
    lcd.setCursor(0,1);
    lcd.print ("TIME:");
    lcd.setCursor(6,1);
    lcd.print(now.hour());
    lcd.print (":");
    lcd.print (now.minute());
   
    Serial.println();
    delay(3000);
}

I get the right time and date thru serial monitor, but on my LCD display, 10:05 will show as 10:5....Any idea why I'm missing the "0" for times with single digit (0 to 9) for the minutes?
Thanks
Title: Re: Arduino Pro Mini and DS3231
Post by: srnet on Jun 22, 2019, 09:15 pm
lcd.print (now.minute());
    
}

I get the right time and date thru serial monitor, but on my LCD display, 10:05 will show as 10:5....Any idea why I'm missing the "0" for times with single digit (0 to 9) for the minutes?
Thanks
One would assume that now.minute() returns a number, say 0,1,2,3,4,5,6,7,8,9,10,11 etc.

You then lcd.print  that number.

Why would you expect the lcd.print to somehow assume or know that when you ask it to print number 5 it should print 05 instead ?
Title: Re: Arduino Pro Mini and DS3231
Post by: Erik_Baas on Jun 22, 2019, 09:47 pm
Code: [Select]
if (now.minute()) < 10 {
  Serial.print('0');
}
Serial.print(now.minute(), DEC);
Serial.print(':');

Do the same for hours and seconds.
Title: Re: Arduino Pro Mini and DS3231
Post by: french_guy on Jun 23, 2019, 01:57 am
Code: [Select]
if (now.minute()) < 10 {
  Serial.print('0');
}
Serial.print(now.minute(), DEC);
Serial.print(':');

Do the same for hours and seconds.
Thanks Erik...
But when I type this code, i have the following error:
expected primary-expression before '<' token
Any idea?
Title: Re: Arduino Pro Mini and DS3231
Post by: french_guy on Jun 23, 2019, 02:47 am
One would assume that now.minute() returns a number, say 0,1,2,3,4,5,6,7,8,9,10,11 etc.

You then lcd.print  that number.

Why would you expect the lcd.print to somehow assume or know that when you ask it to print number 5 it should print 05 instead ?
Super helpful............thanks !