Go Down

Topic: Button to tell time (Read 521 times) previous topic - next topic

agrinshtein

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?
Code: [Select]
// 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.

PaulS

Code: [Select]
    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.

Quote
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.

agrinshtein

Gotcha.

Code: [Select]
// 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.

ash901226

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

PaulS

Code: [Select]
    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?

agrinshtein

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.
Code: [Select]
Serial.println("press the button and we will tell you the time.");}

2. 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.

Code: [Select]
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__));

   }

}


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

UKHeliBob

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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

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:
Code: [Select]
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.

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.

HazardsMind


@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*/
}
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Quick5pnt0

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

agrinshtein

Code: [Select]
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.

Go Up