Problem with clock setting on year

This forum topic has been closed.

07 Nov 2012

I'm using ExtraPutty to debug. When i include values for the year, it keeps on calling back the Inputyear function. Can anyone help me with it? Thanks

#include "mbed.h"
#include "time.h"

Serial pc(USBTX,USBRX);

char c;
char sav[40];
struct tm t;
char str[32];
time_t seconds;

void Input()
{
    memset(sav, 0, 40);
    do {
        c = pc.getc();
        pc.putc (c);
        strcat(sav, &c);
    } while (c != ' ');
}

void Inputyear()
{
    pc.printf("\nPlease input local year then press [space] - YYYY ( Year 1900 onwards)\n\r");
    Input();
    if ( c >= 1900 ) {
        t.tm_year = atoi(sav);
    } else {
        printf("Error, please input the correct local year again\n\n\r");
        Inputyear();
    }
}

void Inputmonth()
{
    pc.printf("\nPlease input local month then press [space] - MM (Between 01-11)\n\r");
    Input();
    if ( c >= 1 && c <= 11) {
        t.tm_mon = atoi(sav);
    } else {
        printf("Error, please input the correct local month again\n\n\r");
        Inputmonth();
    }
}

void Inputday()
{
    pc.printf("\nPlease input local day then press [space] - DD (Between 01-31)\n\r");
    Input();
    if ( c >= 1 && c <= 31) {
        t.tm_mday = atoi(sav);
    } else {
        printf("Error, please input the correct local day again\n\r");
        Inputday();
    }
}

void Inputhour()
{
    pc.printf("\nPlease input local hour then press [space] - HH (Between 00-23)\n\r");
    Input();
    if ( c <= 23) {
        t.tm_hour = atoi(sav);
    } else {
        printf("\nError, please input the correct local hour again\n\r");
        Inputhour();
    }
}

void Inputmin()
{
    pc.printf("\nPlease input local minute then press [space] - mm (Between 00-59)\n\r");
    Input();
    if ( c <= 59) {
        t.tm_min = atoi(sav);
    } else {
        printf("Error, please input the correct local minute again\n\r");
        Inputmin();
    }
}

void Inputsec()
{
    pc.printf("\nPlease input local seconds then press [space] - ss (Between 00-59)\n\r");
    Input();
    if ( c <= 59) {
        t.tm_sec = atoi(sav);
    } else {
        printf("Error, please input the correct local seconds again\n\n\r");
        Inputsec();
    }
}

void TimeSet() // time setting function
{
    pc.printf("=====SETTING TIME=====\n");

    do {
        Inputyear();
        Inputmonth();
        Inputday();
        Inputhour();
        Inputmin();
        Inputsec();
        pc.printf("\n\r Is the time correct? [Y/N] then press [space]\n\n\r");
        c = pc.getc();
    } while (c != 'y' && c != 'Y');

    t.tm_year = t.tm_year - 1900;
    t.tm_mon = t.tm_mon - 1;

    set_time(mktime(&t));
}

void TimeStart() // print out the time every 1 seconds
{
    time_t seconds = time(NULL);
    strftime(str, 32,"%H:%M:%S", localtime(&seconds));
    pc.printf("%s.\n\n\r", str);
    wait(1);
}

int main()
{
    TimeSet();
    TimeStart();
}
07 Nov 2012

void Inputyear()
{
    pc.printf("\nPlease input local year then press [space] - YYYY ( Year 1900 onwards)\n\r");
    Input();
    if ( c >= 1900 ) {
        t.tm_year = atoi(sav);
    } else {
        printf("Error, please input the correct local year again\n\n\r");
        Inputyear();
    }
}

You're comparing c, a char, to 1900. chars will hold values up to 256 so can never be greater than 1900. You probably want compare the result of atoi(sav) to 1900, this is the case for all your Input... functions.

Steve

07 Nov 2012

Closed this thread as it is duplicated here http://mbed.org/questions/45/Problem-with-time-setting-code/