# Problem using Tiny RTC I2C module

Hi, I’m trying to make a simple stopwatch project for my time management using Arduino UNO Rev3 and a module with DS1307 chip (which you can find further information on it by visiting the following link: RTC I2C module or just search it on Google.com). The aim of the project is to have a few push-buttons which will calculate the amount of time of each specific button. Let’s explain with an example: We want to calculate the total time of studying 4 subjects like Math, Physics, Chemistry and Biology (and their details like the beginning and the end time of each times that a specific subject had been studied.) So we’re gonna have another button called “Break Time” for telling the Arduino that it’s the ending time for the last session of study and a “Total Time” button which will add the total times; the following text is an example of what I mean:

``````//after pressing the "Math" button (in the serial monitor):
Studying session of Math has just began! | March 26, 2015 @ 08:45 AM.

//after pressing the "Break Time" button:
Studying session of Math has just ended! | March 26, 2015 @ 09:45 AM.
Time: 1 hour.

//after pressing the "Physics" button:
Studying session of Physics has just began! | March 26, 2015 @ 10:00 AM.

//after pressing the "Break Time" button:
Studying session of Physics has just ended! | March 26, 2015 @ 11:30 AM.
Time: 1 hour and 30 minutes.

//after pressing the "Math" button (in the serial monitor):
Studying session of Math has just began! | March 26, 2015 @ 12:00 AM.

//after pressing the "Break Time" button:
Studying session of Math has just ended! | March 26, 2015 @ 1:00 PM.
Time: 1 hour.

//after pressing the "Total Time" button:
--------------------------------------
Total time of studying: 3 hours and 30 minutes.
Total time of breaks: 0 hour and 45 minutes.

Total time of Math: 2 hours and 0 minutes.
Total time of Physics: 1 hour and 30 minutes.
Total time of Chemistry: 0 hour and 0 minutes.
Total time of Biology: 0 hour and 0 minutes.
``````

I’ve soldered and wired up the project and tried to run a simple test code, but every time I’ve got either a compiler error message or weird characters in the serial monitor like the one in the attachment (I used the code in this Adafruit’s link.)
Thank you all.

post the FULL image of your serial monitor, Im guessing you don't have your baud rate set to 57600, either that or your wiring is wrong. As for the compiler errors, well...you didn't post the compiler error(I cannot comprehend why you wouldn't) so I cannot help you with that...

Ps991:
post the FULL image of your serial monitor, Im guessing you don't have your baud rate set to 57600, either that or your wiring is wrong. As for the compiler errors, well...you didn't post the compiler error(I cannot comprehend why you wouldn't) so I cannot help you with that...

Thanks, the weird characters problem fixed by changing the baud rate as you mentioned. Now I'm trying to write a proper code for it!

So I wrote something like this, a simple code with just 2 buttons:

``````#include <Button.h>
#include <Wire.h>
#include "RTClib.h"
Button button = Button(12, BUTTON_PULLUP_INTERNAL);
Button totaltime = Button(11, BUTTON_PULLUP_INTERNAL);

RTC_DS1307 RTC;

void setup() {
// put your setup code here, to run once:
Serial.begin(57600);
pinMode(13,OUTPUT); //debug to led 13
Wire.begin();
RTC.begin();
int h, m, s;

if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
}

}

void loop() {
// put your main code here, to run repeatedly:
while(!totaltime.uniquePress()){
if(button.uniquePress()){
digitalWrite(13,HIGH);
DateTime now = RTC.now();
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();

h = now.hour();
m = now.minute();
s = now.second();

}else{
digitalWrite(13,LOW);
}
}
DateTime now = RTC.now();
Serial.println("-----------------------");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();

int he = now.hour();
int me = now.minute();
int se = now.second();

int ht = he - h;
int mt = me - m;
int st = se - s;

Serial.println("Total time: ");
Serial.println(ht);
Serial.println(":");
Serial.println(mt);
Serial.println(":");
Serial.println(st);

}
``````

But the compiler gives these error messages:

``````Arduino: 1.6.1 (Windows 7), Board: "Arduino Uno"

sketch_mar27a.ino: In function 'void loop()':

sketch_mar27a.ino:37:5: error: 'h' was not declared in this scope

sketch_mar27a.ino:38:5: error: 'm' was not declared in this scope

sketch_mar27a.ino:39:5: error: 's' was not declared in this scope

sketch_mar27a.ino:58:16: error: 'h' was not declared in this scope

sketch_mar27a.ino:59:16: error: 'm' was not declared in this scope

sketch_mar27a.ino:60:16: error: 's' was not declared in this scope

Error compiling.

"Show verbose output during compilation"
enabled in File > Preferences.
``````

Could you guys help me with this code? I need this project soon so any helps would be appreciated.
Thanks.

C variables are "scoped" to the function that they are declared in. Your h,m,s variables are declared in setup(), so they are only valid inside setup(). Move their declaration to before setup(), or inside loop() if that is the only place you will use them.

Also,

`````` int ht = he - h;
``````

These are not very good variable names. You should use something more descriptive. "he" doesn't tell me anything at all about what it represents. You might now, but try looking again after 6 months and see if you can remember.