Button to tell time

Have hooked up an RTC to my Arduino and got the time working perfectly. Where the correct time shows up on the Arduino Serial Monitor.

Now I am trying to get the serial monitor to only display the time when the pushbutton is pressed.

I have attached a picture of my setup and here is the code. Where am I going wrong?

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
 
#include <Wire.h>
#include "RTClib.h"
 
RTC_DS1307 RTC;
const int buttonPin = 2;
int buttonState = 0;
 
void setup () {
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();
 
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
    pinMode(buttonPin, INPUT);
   
  }
 
}
 
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(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
 
    Serial.print(" since 1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
    
        buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
    Serial.println();
    delay(3000);}
    else
    Serial.print('press the button and we will tell you the time.');
    
   
}

Thanks.

    Serial.print('press the button and we will tell you the time.');

Which ONE key did you press to get that one character between the single quotes? Strings (more than one character) go in double quotes.

Where am I going wrong?

You print a lot of stuff, then read the switch, and, if it is pressed, print a carriage return and line feed. I would have put all the Serial.print() statements in the block that determines that the switch is indeed pressed.

Gotcha.

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
 
#include <Wire.h>
#include "RTClib.h"
 
RTC_DS1307 RTC;
const int buttonPin = 2;
int buttonState = 0;
 
void setup () {
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();
 
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
    pinMode(buttonPin, INPUT);
   
  }
 
}
 
void loop () {

  DateTime now = RTC.now();
 buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH){
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
 
    Serial.print(" since 1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
    }
    else     {
 
    Serial.println('press the button and we will tell you the time.');}
    
   
}

Does that look better?

However, it doesn't react to the button press and also it prints "25902" which I am not sure how that got there?

P.S. I love the arduino community. The rapid response allows you to keep some sort of workflow.

i got a couple of point i would like to share,
1 how about you debounce your Switch
2 you could make all the time display part to become a fuction that you could call after arduino recive the print signal

    Serial.println('press the button and we will tell you the time.');}

This is STILL wrong.

I think you need to go through your code, and put every { and every } on their own line. Then, run Tools + Auto Format.

When you have done that, explain why the pinMode() call is inside the if statement it is in. The mode should always be set.

How is the switch wired?

  1. Ye. I had changed it and the program went crazy on me so I had to shut down the program before I could save it.

It's changed now.Serial.println("press the button and we will tell you the time.");}

  1. I am not sure what you mean when you say that the pinMode() is in the 'if' statement. It looks like it is in the void setup () curly bracket.
void setup () 
{
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();
     pinMode(buttonPin, INPUT);
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));

   }
 
}
  1. With regards to the wiring: I think that might be the problem as well. See attached picture. Hope it's clear enough.

Paul's comment about where the pinMode() command is executed relates to the fact that it is only executed if the RTC is not running. Put the pinMode() command before the if so that the mode is always set.

I asked you to put EVERY { and EVERY } on new lines, and to run Tools + Auto Format, but you apparently couldn't be bothered. There was a reason for that. If you had, you would have seen that your setup() looked like this:

void setup ()
{
  Serial.begin(57600);
  Wire.begin();
  RTC.begin();
 
  if (! RTC.isrunning())
  {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
    pinMode(buttonPin, INPUT);
  }
}

Here, it is very obvious that the pinMode() call will only be made when setting the clock, which is not what you want.

@Pauls -- thanks so much for your help. I appreciate your chiding and giving me direction at the same time. I finally got it.

The next thing I am working on is figuring out how to make

IF statements based on the time I get out of the RTC.

So something like

IF time is 12:00-13:00
THAN have LED connected to PIN 3 light up.
IF time is 13:00-14:00
THAN have LED connected to PIN 4 light up.
ETC..

Buckling down.

agrinshtein:
@Pauls -- thanks so much for your help. I appreciate your chiding and giving me direction at the same time. I finally got it.

The next thing I am working on is figuring out how to make

IF statements based on the time I get out of the RTC.

So something like

IF time is 12:00-13:00
THAN have LED connected to PIN 3 light up.
IF time is 13:00-14:00
THAN have LED connected to PIN 4 light up.
ETC..

Buckling down.

Someone else made a code that does the following:
if(now.hour() ==13 || now.hour() == 14 ) // add minutes and/or seconds to get more accurate time
{
/* Do code 1*/
}

else {
/* Do code 2*/
}

Your circuit appears to be missing a pull up/pull down resistor to the switch.

Someone else made a code that does the following:
if(now.hour() ==13 || now.hour() == 14 ) // add minutes and/or seconds to get more accurate time
{
   /* Do code 1*/
}

else {
  /* Do code 2*/
}

That looks cool. Ill try it out.