Offline
Newbie
Karma: 0
Posts: 33
|
 |
« on: January 14, 2013, 06:25:34 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #1 on: January 14, 2013, 06:29:29 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 33
|
 |
« Reply #2 on: January 14, 2013, 06:44:29 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Malaysia
Offline
Sr. Member
Karma: 7
Posts: 385
|
 |
« Reply #3 on: January 14, 2013, 07:04:59 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #4 on: January 14, 2013, 07:06:15 am » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 33
|
 |
« Reply #5 on: January 15, 2013, 01:03:57 am » |
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.");} 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. 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.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 47
Posts: 1404
May all of your blinks be without delay
|
 |
« Reply #6 on: January 15, 2013, 02:27:26 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #7 on: January 15, 2013, 05:57:35 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 33
|
 |
« Reply #8 on: January 17, 2013, 01:47:52 pm » |
@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.
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 28
Posts: 1551
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #9 on: January 17, 2013, 01:55:42 pm » |
@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*/ }
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
NE PA
Offline
Full Member
Karma: 5
Posts: 149
|
 |
« Reply #10 on: January 17, 2013, 02:24:30 pm » |
Your circuit appears to be missing a pull up/pull down resistor to the switch.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 33
|
 |
« Reply #11 on: January 17, 2013, 02:51:57 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
|