I am a new user of Arduino Uno and I was wondering if anyone can help me a bit with either understanding the serial port read() and write( ) transfers I posted and attached most of my code, I am simply trying to use Python's Time and Sleep libraries in order to get the current time and date when I first run my Arduino board however the outfit that I feel I should get by looking online and useing the methods is not coming out right. Any help would be greatly appreciated.
--I need to send 2-4 digits at a time or all and parse it before displaying it on my 16x2 lcd display.
I used gettime.py and dateTest.ino in Arduino. However I am getting a random output that I don't want. Here is the print out
o oo osnT... repeats over and over. I don't understand why it is getting this output though?
---------------------------gettime.py-----------------------
s = serial.Serial('COM3', 9600)
timeNow = time.localtime()
month = timeNow.tm_mon
day = timeNow.tm_mday
year = timeNow.tm_year
date = month, " ", day, ", ", year
hour = timeNow.tm_hour
minutes = timeNow.tm_min
while (s.read() != '0'):
passVal = s.read()
print s.read()
if(passVal == -9):
print "got to while1"
s.flush()
s.write(month)
if(passVal == -1):
print "got to while2"
s.flush()
s.write(day)
if(passVal == -2):
s.flush()
s.write(year)
if(passVal == -3):
s.flush()
s.write(hour)
if(passVal == -4):
s.flush()
s.write(minutes)
s.close()
print date, " , ", hour, ":", minutes,"\n"
------------------------dateTest.ino------------------------------------
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
int columns = 16;
int rows = 4;
int minSecsBetweenEmails = 60; // 1 min
String month = "";
String day = "";
String year = "";
String hour = "";
String minutes = "";
long lastSend = -minSecsBetweenEmails * 1000l;
void setup()
{
lcd.begin(rows, columns);
lcd.clear();
Serial.begin(9600);
}
void loop()
{
long now = millis();
if (now > (lastSend + minSecsBetweenEmails * 1000l))
{
Serial.println("READY");
lastSend = now;
for(int i = 0; i < Serial.available(); i++){
if(i <= 2){
month += Serial.read();
}
else if(i <= 4 && i > 2){
day += Serial.read();
}
else if(i <= 8 && i > 4){
year += Serial.read();
}
else{
break;
}
}
lcd.print(month);
lcd.print(day);
lcd.print(year);
}
else
{
Serial.println("Too soon");
}
delay(500);
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.
gettime.py (883 Bytes)
dateTest.ino (1.21 KB)