Go Down

Topic: Arduino Clock: Setting Time (Read 11088 times) previous topic - next topic

florinc

Your C# GUI app can send the 2 bytes (hour, minute) on serial port to arduino. What is wrong with this approach? Why do you want to include Processing in the mix?


bugzy4real

oK. So, my project is due pretty soon and I'm already deep into using processing and that's why i rather not use C# now. If you guys are telling me, there's no way i can use arduino to get user input, that's fine

mem

I don't think anyone has said that. If you want to get user input without an external computer, you can use an LCD or other display device. If you are ok using an application running on your PC then you can use Processing or C#. Arduino doesn't care as long as send it a value that it can recognize as an alarm time.  

It seems that you have two parts to your project:
- Processing or C# gets an alarm time from a user and sends this to arduino via the serial port
- Arduino receives an alarm time from a PC and responds accordingly

not sure which of these you are having trouble with, but there are many ways you can implent both of the tasks.

bugzy4real

Quote: Processing or C# gets an alarm time from a user and sends this to arduino via the serial port

Yes, that's  my question- how can the user send the input time from processing over to the arduino?
This is the part of my code that gets user input from processing. h and m are the user input variables But how do you send these values to the arduino?

//Processing Code:

import javax.swing.JOptionPane;
String user_hour  = (String)JOptionPane.showInputDialog(null, "Hour :","Datainputdialog", JOptionPane.PLAIN_MESSAGE);
           String user_min  = (String)JOptionPane.showInputDialog(null, "Minute:","Datainputdialog", JOptionPane.PLAIN_MESSAGE);
           int h = Integer.parseInt(user_hour);
           int m = Integer.parseInt(user_min);

mem

You can convert the hour and minute into elapsed seconds using the logic posted in Reply#6 and send this to Arduino using similar code to the example Processing sketch that sets the time (pick a different header byte so you can differentiate the message to set the alarm from the one to set the time.

Then use the Arduino code that you have working from post#6 to  trigger the alarm.

bugzy4real

#20
Apr 11, 2009, 12:04 am Last Edit: Apr 11, 2009, 01:39 am by bugzy4real Reason: 1
mem: Can you tell me with syntax how I can send two numbers (h and m) using headers over the serial port?
Also, where in the arduino code below is the time header retrieved from processing? I'm asking so i know how to modify the code. sorry i'm having you baby-sit me thru the process

//arduino code
#include <DateTime.h>
#include <DateTimeStrings.h>

#define TIME_MSG_LEN  11   // time sync to PC is HEADER and unix time_t as ten ascii digits
#define TIME_HEADER  255   // Header tag for serial time sync message

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

void  loop(){  
 getPCtime();   // try to get time sync from pc        
 if(DateTime.available()) { // update clocks if time has been synced
   unsigned long prevtime = DateTime.now();
   while( prevtime == DateTime.now() )  // wait for the second to rollover
     ;
   DateTime.available(); //refresh the Date and time properties
   digitalClockDisplay( );   // update digital clock

   // send our time to an app listening on the serial port
   Serial.print( TIME_HEADER,BYTE); // this is the header for the current time
   Serial.println(DateTime.now());      
 }
}

void getPCtime() {
 // if time available from serial port, sync the DateTime library
 while(Serial.available() >=  TIME_MSG_LEN ){  // time message
   if( Serial.read() == TIME_HEADER ) {        
     time_t pctime = 0;
     for(int i=0; i < TIME_MSG_LEN -1; i++){  
       char c= Serial.read();          
       if( c >= '0' && c <= '9')  
         pctime = (10 * pctime) + (c - '0') ; // convert digits to a number            
     }  
     DateTime.sync(pctime);   // Sync DateTime clock to the time received on the serial port
   }  
 }
}

void digitalClockDisplay(){
 // digital clock display of current time
 Serial.print(DateTime.Hour,DEC);  
 printDigits(DateTime.Minute);  
 printDigits(DateTime.Second);
 Serial.print(" ");
 Serial.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
 Serial.print(" ");  
 Serial.print(DateTimeStrings.monthStr(DateTime.Month));  
 Serial.print(" ");
 Serial.println(DateTime.Day, DEC);  
}

void printDigits(byte digits){
 // utility function for digital clock display: prints colon and leading 0
 Serial.print(":");
 if(digits < 10)
   Serial.print('0');
 Serial.print(digits,DEC);  
}

mem

#21
Apr 11, 2009, 09:09 am Last Edit: Apr 11, 2009, 09:10 am by mem Reason: 1
You can see how to send Data to Arduino by studying the Processing Serial examples. There is Arduino code at the end of the Processing example sketches. In Processing, select  : Files/Examples/Libraries/Serial/


I have added some more comments to help clarify how the Arduino sketch gets the time.
Code: [Select]
void getPCtime() {
// if time available from serial port, sync the DateTime library
// a time message consists of a header followed by ten digits (TIME_MSG_LEN equals 11)
while(Serial.available() >=  TIME_MSG_LEN ){  // time message
// you make sure you have the start of the message by checking the header
  if( Serial.read() == TIME_HEADER ) {        
    // header was found so get the next ten digits and convert to a number
    time_t pctime = 0;
    for(int i=0; i < TIME_MSG_LEN -1; i++){  
      char c= Serial.read();          
      if( c >= '0' && c <= '9')  
        pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
// the ASCII character is converted to a digit by subtracting '0' which has an ASCII value 48.
// So, if ch equals '1', its ASCII value is 49. 49- '0' is the same as 49-48 and they both equal 1,
// which is the numeric value of the character '1'        
    }  
    // the next method sets the clock with the pc time received above
    DateTime.sync(pctime);   // Sync DateTime clock to the time received on the serial port
  }  
}
}

bugzy4real

I think my case is different. So, parsing the data the same way  you do as the time clock shouldn't be the same way. I guess the question now is how do you parse the data since i'm sending two integers over the port. The processing code below is wat i'm using to send the data to the arduino, how would you parse it in arduino? Thanks!

//processing code
port.write(header2);  //writes the header first
port.write(h);
port.write(m);

mem

if the header, h, and m are all chars then you don't need to parse. read a byte in arduino and if its the header you can then read h and m directly.

bugzy4real

#24
Apr 13, 2009, 09:47 pm Last Edit: Apr 13, 2009, 11:25 pm by bugzy4real Reason: 1
Mem: Thanks a lot!!!

bugzy4real

Quote:
time_t  alarmOnTime =  (8 * SECS_PER_HOUR) + (30 * SECS_PER_MIN);
time_t  alarmOffTime =  alarmOnTime  + (60 * SECS_PER_MIN);
time_t nextAlarm;


nextAlarm =  previousMidnight(DateTime.now())+ alarmOnTime;

if( DateTime.now() >= nextAlarm  && DateTime.now()  < nextAlarm + alarmOffTime )
   digitalWrite(ledPin), HIGH);
else
  digitalWrite(ledPin), LOW);


Mem: the LED turns on at the precise time (8:30) but does not turn off at all. Do you have any idea why it acts likes that?

hotcarrier

I saw this too and changed the second test in the if statement to:

previousMidnight(DateTime.now())+ alarmOffTime;

and it worked for me.

DarkElf

hello...

I'm totally new to arduino and I have a project to hand next week

my goal to accoplishe is to use arduino as alarm clock that turn on light on specific time

I searched solutions online, most on this forum, but I can't connect it all

firs idea was that computer send real time to arduino that is displayed on lcd (it's working fine), but after failure I decided to use code from internet that allows to set up time and than counts

a problem is now to start led when desirable time is reached, by chance to slowly start up, opposite of dimming (example)

code I used so far:

#include <LiquidCrystal.h>
#include <DateTime.h>

// simple sketch to display a digital clock on an LCD
// see the LiquidCrystal documentation for more info on this

LiquidCrystal lcd(7,8,9,10,11,12);

int ledPin =  13;    // LED connected to digital pin 13




void setup(){
 //DateTime.sync(1230768000); // set jan 1 2009 as the default time
 DateTime.sync(DateTime.makeTime(0, 40, 10, 29, 5, 2010));
}

void  loop(){    
 if(DateTime.available()) {
   unsigned long prevtime = DateTime.now();
   while( prevtime == DateTime.now() )  // wait for the second to rollover
     ;
   DateTime.available(); //refresh the Date and time properties
   digitalClockDisplay( );   // update digital clock  
 }
}

void printDigits(byte digits){
 // utility function for digital clock display: prints preceding colon and leading 0
 lcd.print(":");
 if(digits < 10)
   lcd.print('0');
 lcd.print(digits,DEC);
}

void digitalClockDisplay(){
 lcd.home();
 // digital clock display of current time
 lcd.print(DateTime.Hour,DEC);  
 printDigits(DateTime.Minute);  
 printDigits(DateTime.Second);  
}


time_t  alarmOnTime =  (10 * SECS_PER_HOUR) + (45 * SECS_PER_MIN);
time_t  alarmOffTime =  alarmOnTime  + (60 * SECS_PER_MIN);
time_t nextAlarm;


nextAlarm =  previousMidnight(DateTime.now())+ alarmOffTime;


if ( DateTime.now() >= alarmOnTime  && DateTime.now()  < alarmOnTime + alarmOffTime ){
   digitalWrite(ledPin), HIGH);
}
else{
  digitalWrite(ledPin), LOW);
}



but error occur


any suggestion is appreciated

(my C programming is in basics =/)

PaulS

None of this code is inside a function:
Code: [Select]
time_t  alarmOnTime =  (10 * SECS_PER_HOUR) + (45 * SECS_PER_MIN);
time_t  alarmOffTime =  alarmOnTime  + (60 * SECS_PER_MIN);
time_t nextAlarm;


nextAlarm =  previousMidnight(DateTime.now())+ alarmOffTime;


if ( DateTime.now() >= alarmOnTime  && DateTime.now()  < alarmOnTime + alarmOffTime ){
  digitalWrite(ledPin), HIGH);
}
else{
 digitalWrite(ledPin), LOW);
}



All executable code needs to be inside a function.

DarkElf

thnx a lot!!

always learning  ;D



code now looks like:

#include <LiquidCrystal.h>
#include <DateTime.h>

// simple sketch to display a digital clock on an LCD
// see the LiquidCrystal documentation for more info on this

LiquidCrystal lcd(7,8,9,10,11,12);

int ledPin =  13;    // LED connected to digital pin 13


void setup(){
 //DateTime.sync(1230768000); // set jan 1 2009 as the default time
 DateTime.sync(DateTime.makeTime(45, 40, 10, 29, 5, 2010));
 lcd.begin(16, 2);
 // initialize the digital pin as an output:
   pinMode(ledPin, OUTPUT);
}

void  loop(){    
 if(DateTime.available()) {
   unsigned long prevtime = DateTime.now();
   while( prevtime == DateTime.now() )  // wait for the second to rollover
     ;
   DateTime.available(); //refresh the Date and time properties
   digitalClockDisplay( );   // update digital clock
   
 time_t  alarmOnTime =  (10 * SECS_PER_HOUR) + (41 * SECS_PER_MIN);
 time_t  alarmOffTime =  alarmOnTime  + (60 * SECS_PER_MIN);
 time_t nextAlarm;


 nextAlarm =  previousMidnight(DateTime.now())+ alarmOffTime;


 if ( DateTime.now() >= alarmOnTime  && DateTime.now()  < alarmOnTime + alarmOffTime ){
   digitalWrite(ledPin, HIGH);
 }
 else{
  digitalWrite(ledPin, LOW);
}  
 }
}

void printDigits(byte digits){
 // utility function for digital clock display: prints preceding colon and leading 0
 lcd.print(":");
 if(digits < 10)
   lcd.print('0');
 lcd.print(digits,DEC);
}

void digitalClockDisplay(){
 lcd.home();
 // digital clock display of current time
 lcd.print(DateTime.Hour,DEC);  
 printDigits(DateTime.Minute);  
 printDigits(DateTime.Second);  
}



but when it hits 10:41:00 nothing happened   :-/

Go Up