Noob here, help needed.

I have a very basic issue, however I can't find any solutions online.
What I'm trying to do is print the current second every 0.5 seconds in the serial monitor.

Here is the code:

#include <DS1302.h>
#include <TM1638.h>
#define DS1302_SCLK_PIN 0 // Arduino pin for the Serial Clock
#define DS1302_IO_PIN 1 // Arduino pin for the Data I/O
#define DS1302_CE_PIN 4 // Arduino pin for the Chip Enable AKA rst
DS1302 rtc(DS1302_CE_PIN, DS1302_IO_PIN, DS1302_SCLK_PIN );
#define TM1638_DIO 7
#define TM1638_CLK 6
#define TM1638_STB 5
TM1638 module(TM1638_DIO, TM1638_CLK, TM1638_STB);
#include <Streaming.h>
#include <InvertedTM1638.h>
#include <TM1638QYF.h>
#include <TM1640.h>
#include <TM16XX.h>
#include <TM16XXFonts.h>

void setup() {
 module.clearDisplay();
 module.setupDisplay(true, 2); // active, brightness = 1
 Serial.begin(9600); 
}

void loop() {
  Time t = rtc.time();
  int a = t.sec;
  Serial << a << endl;
  delay(500);
  module.setDisplayToDecNumber(a,0,false);   
}

When I run this for some reason it only prints 85. I've set up the time before using this code:

#include <DS1302.h>
#define DS1302_SCLK_PIN 0 // Arduino pin for the Serial Clock
#define DS1302_IO_PIN 1 // Arduino pin for the Data I/O
#define DS1302_CE_PIN 4 // Arduino pin for the Chip Enable AKA rst
DS1302 rtc(DS1302_CE_PIN, DS1302_IO_PIN, DS1302_SCLK_PIN );
void setup() {
 rtc.writeProtect(false);
 rtc.halt(false);
 Time t(2016, 11, 24, 16, 10, 00, Time::kTuesday); // set the time
 //year mnth day hr min sec dayofWeek
 rtc.time(t);
 rtc.writeProtect(true);
}
void loop() {
 delay(1000); // do nothing
}

  Serial << a << endl;Can you please explain this line ?

a = t.sec which is calling the time class and the variable "sec".

Serial << a << endl; is printing this value in the serial monitor.

Is it really?

I think so :confused:

It's the equivilent of Serial.println(a);

Such construction will not work here. You can use only supported Serial methods.

Supported serial methods? How do you mean?

The serial is not the issue. I'm also displaying it on a 7-SEG device and it also only displays "85".

myhat2you:
It's the equivilent of Serial.println(a);

Is there any reason to use the more obscure form ?

Please ignore the "Serial <<" part. It works and that's not the problem I'm having. I'll take it out of the code I don't need it. I'm just trying to display seconds using the RTC.

myhat2you:
Please ignore the "Serial <<" part. It works and that's not the problem I'm having. I'll take it out of the code I don't need it. I'm just trying to display seconds using the RTC.

Have you run the code without that line and still gotten the same result?

You may not think it is the problem but since the << also represents a left shift, you might be unknowingly changing the value of a

Perhaps this should be advertised more widely.

http://arduiniana.org/libraries/streaming/

#include <Streaming.h> 

Serial << whatever << somethingElse() << endl; // the C++ way :)

I got rid of the "Serial <<" part and still have the same problem. I don't believe the 7SEG or the serial monitor have any problems and I pray the RTC is working fine. Something must be wrong with the code somewhere.

oqibidipo:
Perhaps this should be advertised more widely.

Streaming | Arduiniana

#include <Streaming.h> 

Serial << whatever << somethingElse() << endl; // the C++ way :slight_smile:

Perhaps. I do prefer the C++ way :slight_smile:

Finally found somewhere where they address this issue. If anyone else is having this problem look here:

The Serial class probably overloads the '<<' operator. I hate operator overloading for exactly this reason: you can't tell by looking at the code what it's going to do.

So, let's assume rtc.sec() really is returning 85.

#define DS1302_SCLK_PIN 0 // Arduino pin for the Serial Clock
#define DS1302_IO_PIN 1 // Arduino pin for the Data I/O
#define DS1302_CE_PIN 4 // Arduino pin for the Chip Enable AKA rst
DS1302 rtc(DS1302_CE_PIN, DS1302_IO_PIN, DS1302_SCLK_PIN );

Obvious first problem: pins 0 and 1 are used by the on-board serial. Try plugging your clock into somewhere else.

My next step would be chasing down the docs for the library and finding out what "sec" is supposed to return, but there's no need.

OP: if you are using Serial, leave pins 0 and 1 alone.

Finally found somewhere where they address this issue. If anyone else is having this problem look here:
http://www.instructables.com/answers/Why-doesnt-DS1307-work-on-my-arduino/

This is written about a very different device--the ds1307, and talks about low voltage issues.

The ds1302 is spec'd to run between 2 and 5v.

But the fact that the seconds time value is returning 85 is indicative of an incorrect read.

First thing to do, is follow Paul Murray's advice and move the clock off the serial pins. The sketch you used to set the time had no Serial output. The sketch which is failing has Serial.begin(9600) and the streaming output.

If changing the SCLK and IO pins, or eliminating the Serial code, does not fix your issue, please post a link to the ds1302 library you are using. There are several with the same name.

Here you can find possibly working sketch for your devices.