How to set ds3231 by serial monitor?

This my code . When i set by serial monitor,it cant print correct time...

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

byte Year, Month, Date, Day, Hour, Minute, Second;

void setup() {
	// Start the I2C interface
	Serial.begin(9600);
   
    Serial.println("input Y to set time");
    delay(4000);
    if (Serial.read()==89)
    {
    Serial.println("year");
    delay(2000);
    Year=byte(Serial.parseInt());
    Serial.println("month");
    delay(2000);
    Month=byte(Serial.parseInt());
    Serial.println("date");
    delay(2000);
    Date=byte(Serial.parseInt());
    Serial.println("day");
    delay(2000);
    Day=byte(Serial.parseInt());
    Serial.println("hour");
    delay(2000);
    Hour=byte(Serial.parseInt());
    Serial.println("minute");
    delay(2000);
    Minute=byte(Serial.parseInt());
    Serial.println("second");
    delay(2000);
    Second=byte(Serial.parseInt());
    }
}
void ReadDS3231()
{
  int second,minute,hour,date,month,year,temperature; 
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();
  
  temperature=Clock.getTemperature();
  
  Serial.print("20");
  Serial.print(year,DEC);
  Serial.print('-');
  Serial.print(month,DEC);
  Serial.print('-');
  Serial.print(date,DEC);
  Serial.print(' ');
  Serial.print(hour,DEC);
  Serial.print(':');
  Serial.print(minute,DEC);
  Serial.print(':');
  Serial.print(second,DEC);
  Serial.print('\n');
  Serial.print("Temperature=");
  Serial.print(temperature); 
  Serial.print('\n');
}
void loop() {ReadDS3231();delay(1000);}

You have the time you want to set the clock to. All that seems to be missing is the call to actually set the time.

Your code is not posted properly. Clearly, you couldn't be bothered to read the sticky at the top of the forum - the one that says to read me before posting in this forum. Not a good first impression to create.

when i set time to 2014/1/14 19:23:30 (i input 14 enter 1 enter 14 enter 2 enter 19 enter 23 enter 30enter)
this is my Serial Monitor

input Y to set time
year
month
date
day
hour
minute
second
20165-85-165 25:165:165
Temperature=254

when i set time to 2014/1/14 19:23:30 (i input 14 enter 1 enter 14 enter 2 enter 19 enter 23 enter 30enter)
this is my Serial Monitor

Entering time values in the serial monitor is one thing. Actually telling the ds3231 the time you want it to start with is another thing.

You are doing the first thing. You are NOT doing the second thing. Somewhere, you need something like Clock.setTime(Year, Month, Day, Hour, Minute, Second); (or whatever method your library has for setting the time).

I use this.

http://bildr.org/2011/03/ds1307-arduino/

It is for the DS1307 but I understand it works for DS3231. The actual time is set when you press the reset button.

PaulS:

when i set time to 2014/1/14 19:23:30 (i input 14 enter 1 enter 14 enter 2 enter 19 enter 23 enter 30enter)
this is my Serial Monitor

Entering time values in the serial monitor is one thing. Actually telling the ds3231 the time you want it to start with is another thing.

You are doing the first thing. You are NOT doing the second thing. Somewhere, you need something like Clock.setTime(Year, Month, Day, Hour, Minute, Second); (or whatever method your library has for setting the time).

Now this my code after correction. It print s NowHour NowMinute corretly after I use monitor to set it. But if i dont enter Y to set it, it always prints :
25
165
I think even i dont enter Y to set it, or rest Arduino ,the time that last i set should be stored in ds3231 , but why it prints 25 165??"

#include <Wire.h>

#include <DS3231.h>

 
  byte Year;
  byte Month;
  byte Date;
  byte DoW;
  byte Hour;
  byte Minute;
  byte Second;
  byte OnHour;
  byte OnMinute;
  byte OffHour;
  byte OffMinute;
  bool PM;
  bool h12;
  bool Century=false;
  
  
  DS3231 Clock;
  
 void setup()
 {
   Serial.begin(9600);
   
    Serial.println("input Y to set time");
    delay(4000);
    if (Serial.read()==89)
    {
    Serial.println("year");
    delay(2000);
    Year=byte(Serial.parseInt());
    Serial.println("month");
    delay(2000);
    Month=byte(Serial.parseInt());
    Serial.println("date");
    delay(2000);
    Date=byte(Serial.parseInt());
    Serial.println("day");
    delay(2000);
    DoW=byte(Serial.parseInt());
    Serial.println("hour");
    delay(2000);
    Hour=byte(Serial.parseInt());
    Serial.println("minute");
    delay(2000);
    Minute=byte(Serial.parseInt());
    Serial.println("second");
    delay(2000);
    Second=byte(Serial.parseInt());
    Wire.begin();
    Clock.setSecond(Second);//Set the second 
    Clock.setMinute(Minute);//Set the minute 
    Clock.setHour(Hour);  //Set the hour 
    Clock.setDoW(DoW);    //Set the day of the week
    Clock.setDate(Date);  //Set the date of the month
    Clock.setMonth(Month);  //Set the month of the year
    Clock.setYear(Year);
    }  
 }
 
 void loop()
 {
    int NowHour=Clock.getHour(h12,PM);
    int NowMinute=Clock.getMinute();
    delay(1000);
    Serial.println( NowHour);
    Serial.println( NowMinute);
 }

Nick_Pyner:
I use this.

http://bildr.org/2011/03/ds1307-arduino/

It is for the DS1307 but I understand it works for DS3231. The actual time is set when you press the reset button.

Thank you. Let me have a look.

m33spiral:
This my code . When i set by serial monitor,it cant print correct time...

This is my code that I use for my DS3234 driver. It should work for you (just replace the "writeTime()" call with whatever your library uses to set time).

#include <DS3231.h>
#include <Wire.h>

static DS3231 RTC;

uint8_t h;
uint8_t m;
uint8_t s;
uint8_t mn;
uint8_t dy;
uint16_t yr;

char buffer[32];

void setup (void)
{
	Serial.begin (115200);
}

void loop (void)
{
}

// get user input, set clock
void set_clock (void)
{
	h = 0; m = 0; s = 0;
	mn = 0; dy = 0; yr = 0;
	Serial.print ("Enter the time:\r\n");
	Serial.print ("HH MM SS MM DD YYYY\r\n");
	readline (buffer, 20);
	sscanf (buffer, "%2u %2u %2u %2u %2u %4u", &h, &m, &s, &mn, &dy, &yr);

	if (h + m + s + mn + dy + yr) {
		RTC.writeTime (h, m, s, mn, dy, yr); // "write time" or whatever the function is that sets the RTC
		Serial.print ("\r\nClock is set!\r\n");

	} else {
		Serial.print ("\r\nClock has not been changed\r\n");
	}
}

uint16_t readline (char *buffer, uint16_t limit)
{
	char c;
	uint16_t ptr = 0;

	while (1) {

		if (Serial.available()) {

			c = Serial.read();

			if (c == 0x0D) { // cr == end of line
				*(buffer + ptr) = 0; // mark end of line
				break; // return char count
			}

			if (c == 0x08) { // backspace

				if (ptr) { // if not at the beginning

					*(buffer + --ptr) = 0;
					Serial.print ( (char) 0x08); // erase backspaced char
					Serial.print ( (char) 0x20);
					Serial.print ( (char) 0x08);

				} else {
					Serial.print ( (char) 0x07); // ascii bel (beep) if terminal supports it
				}

			} else { // not a backspace, handle it

				if (ptr < (limit - 1)) { // if not at the end of the line

					Serial.print ( (char) c); // echo char
					*(buffer + ptr++) = c; // put char into buffer

				} else { // at end of line, can't add any more
					Serial.print ( (char) 0x07); // ascii bel (beep) if terminal supports it
				}
			}
		}
	}
	return ptr;
}

By the way, the Arduino serial monitor.... um... stinks. Use a proper terminal emulator instead. It will work a lot better.