c - Bytes not being read in Arduino code -
i trying set time , date of real time clock, having problem reading data type in serial monitor. in code below, asks if want change date, , type "y" , press enter. after that, asks year, , type"14" , press enter, nothing happens.
#include <wire.h> const int ds1307 = 0x68; // address of ds1307 see data sheets const char* days[] = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}; const char* months[] = {"january", "february", "march", "april", "may", "june", "july", "august","september", "october", "november", "december"}; // initializes values: byte second = 0; byte minute = 0; byte hour = 0; byte weekday = 0; byte monthday = 0; byte month = 0; byte year = 0; void setup() { wire.begin(); serial.begin(9600); delay(2000); // delay allows mcu read current date , time. serial.print("the current date , time is: "); printtime(); serial.println("please change newline ending settings on lower right of serial monitor"); serial.println("would set date , time now? y/n"); while (!serial.available()) delay(10); if (serial.read() == 'y' || serial.read() == 'y') // set of functions allows user change date , time { serial.read(); settime(); serial.print("the current date , time now: "); printtime(); } serial.println("thank you."); } // continuous function converting bytes decimals , vice versa void loop() { } byte dectobcd(byte val) { return ((val/10*16) + (val%10)); } byte bcdtodec(byte val) { return ((val/16*10) + (val%16)); } // set of codes allows input of data void settime() { serial.print("please enter current year, 00-99. - "); year = readbyte(); serial.println(year); serial.print("please enter current month, 1-12. - "); month = readbyte(); serial.println(months[month-1]); serial.print("please enter current day of month, 1-31. - "); monthday = readbyte(); serial.println(monthday); serial.println("please enter current day of week, 1-7."); serial.print("1 sun | 2 mon | 3 tues | 4 weds | 5 thu | 6 fri | 7 sat - "); weekday = readbyte(); serial.println(days[weekday-1]); serial.print("please enter current hour in 24hr format, 0-23. - "); hour = readbyte(); serial.println(hour); serial.print("please enter current minute, 0-59. - "); minute = readbyte(); serial.println(minute); second = 0; serial.println("the data has been entered."); // following codes transmits data rtc wire.begintransmission(ds1307); wire.write(byte(0)); wire.write(dectobcd(second)); wire.write(dectobcd(minute)); wire.write(dectobcd(hour)); wire.write(dectobcd(weekday)); wire.write(dectobcd(monthday)); wire.write(dectobcd(month)); wire.write(dectobcd(year)); wire.write(byte(0)); wire.endtransmission(); // ends transmission of data } byte readbyte() { while (!serial.available()) delay(10); byte reading = 0; byte incomingbyte = serial.read(); while (incomingbyte != '\n') { if (incomingbyte >= '0' && incomingbyte <= '9') reading = reading * 10 + (incomingbyte - '0'); else; incomingbyte = serial.read(); } serial.flush(); return reading; } void printtime() { char buffer[3]; const char* ampm = 0; readtime(); serial.print(days[weekday-1]); serial.print(" "); serial.print(months[month-1]); serial.print(" "); serial.print(monthday); serial.print(", 20"); serial.print(year); serial.print(" "); if (hour > 12) { hour -= 12; ampm = " pm"; } else ampm = " am"; serial.print(hour); serial.print(":"); sprintf(buffer, "%02d", minute); serial.print(buffer); serial.println(ampm); } void readtime() { wire.begintransmission(ds1307); wire.write(byte(0)); wire.endtransmission(); wire.requestfrom(ds1307, 7); second = bcdtodec(wire.read()); minute = bcdtodec(wire.read()); hour = bcdtodec(wire.read()); weekday = bcdtodec(wire.read()); monthday = bcdtodec(wire.read()); month = bcdtodec(wire.read()); year = bcdtodec(wire.read()); }
update 1:
#include <wire.h> const int ds1307 = 0x68; // address of ds1307 see data sheets const char* days[] = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}; const char* months[] = {"january", "february", "march", "april", "may", "june", "july", "august","september", "october", "november", "december"}; // initializes values: byte second = 0; byte minute = 0; byte hour = 0; byte weekday = 0; byte monthday = 0; byte month = 0; byte year = 0; void setup() { wire.begin(); serial.begin(9600); delay(2000); // delay allows mcu read current date , time. serial.print("the current date , time is: "); printtime(); serial.println("please change newline ending settings on lower right of serial monitor"); serial.println("would set date , time now? y/n"); while (!serial.available()) delay(10); if (serial.read() == 'y' || serial.read() == 'y') // set of functions allows user change date , time { serial.read(); settime(); serial.print("the current date , time now: "); printtime(); } serial.println("thank you."); } // continuous function converting bytes decimals , vice versa void loop() { } byte dectobcd(byte val) { return ((val/10*16) + (val%10)); } byte bcdtodec(byte val) { return ((val/16*10) + (val%16)); } // set of codes allows input of data void settime() { serial.print("please enter current year, 00-99. - "); year = readbyte(); serial.println(year); serial.print("please enter current month, 1-12. - "); month = readbyte(); serial.println(months[month-1]); serial.print("please enter current day of month, 1-31. - "); monthday = readbyte(); serial.println(monthday); serial.println("please enter current day of week, 1-7."); serial.print("1 sun | 2 mon | 3 tues | 4 weds | 5 thu | 6 fri | 7 sat - "); weekday = readbyte(); serial.println(days[weekday-1]); serial.print("please enter current hour in 24hr format, 0-23. - "); hour = readbyte(); serial.println(hour); serial.print("please enter current minute, 0-59. - "); minute = readbyte(); serial.println(minute); second = 0; serial.println("the data has been entered."); // following codes transmits data rtc wire.begintransmission(ds1307); wire.write(byte(0)); wire.write(dectobcd(second)); wire.write(dectobcd(minute)); wire.write(dectobcd(hour)); wire.write(dectobcd(weekday)); wire.write(dectobcd(monthday)); wire.write(dectobcd(month)); wire.write(dectobcd(year)); wire.write(byte(0)); wire.endtransmission(); // ends transmission of data } byte readbyte() { byte reading = 0; while (!serial.available()) delay(10); byte incomingbyte = serial.read(); while (incomingbyte != '\r') { if (incomingbyte >= '0' && incomingbyte <= '9') reading = reading * 10 + (incomingbyte - '0'); else; incomingbyte = serial.read(); } //serial.flush(); return reading; } void printtime() { char buffer[3]; const char* ampm = 0; readtime(); serial.print(days[weekday-1]); serial.print(" "); serial.print(months[month-1]); serial.print(" "); serial.print(monthday); serial.print(", 20"); serial.print(year); serial.print(" "); if (hour > 12) { hour -= 12; ampm = " pm"; } else ampm = " am"; serial.print(hour); serial.print(":"); sprintf(buffer, "%02d", minute); serial.print(buffer); serial.println(ampm); } void readtime() { wire.begintransmission(ds1307); wire.write(byte(0)); wire.endtransmission(); wire.requestfrom(ds1307, 7); second = bcdtodec(wire.read()); minute = bcdtodec(wire.read()); hour = bcdtodec(wire.read()); weekday = bcdtodec(wire.read()); monthday = bcdtodec(wire.read()); month = bcdtodec(wire.read()); year = bcdtodec(wire.read()); }
there few problems.
you have serial.read()
before call settime();
. loses byte.
in readbyte()
returning int instead of byte, real problem here should declaring byte reading = 0;
outside loop. gets sets 0 mistake every loop iteration.
serial.flush()
flushing outgoing data since arduino 1.0. looks mean flushing incoming buffer, not more.
the problem freezing due loop inside readbyte()
. terminals include '\n'
character @ end of line, use '\r'
, , use both. try checking '\r'
, ignoring '\n'
. program may waiting endlessly newline character never sent!
Comments
Post a Comment