0
Offline
Edison Member
Karma: 6
Posts: 1398
Arduino rocks
|
 |
« Reply #15 on: April 09, 2009, 09:07:59 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #16 on: April 10, 2009, 09:46:19 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #17 on: April 10, 2009, 10:09:08 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #18 on: April 10, 2009, 11:51:48 am » |
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);
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #19 on: April 10, 2009, 12:51:36 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #20 on: April 10, 2009, 05:04:27 pm » |
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); }
|
|
|
|
« Last Edit: April 10, 2009, 06:39:41 pm by bugzy4real »
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #21 on: April 11, 2009, 02:09:38 am » |
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. 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 } } }
|
|
|
|
« Last Edit: April 11, 2009, 02:10:25 am by mem »
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #22 on: April 11, 2009, 04:20:33 pm » |
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);
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #23 on: April 12, 2009, 01:16:36 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #24 on: April 13, 2009, 02:47:46 pm » |
Mem: Thanks a lot!!!
|
|
|
|
« Last Edit: April 13, 2009, 04:25:14 pm by bugzy4real »
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #25 on: April 16, 2009, 01:06:39 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 38
Arduino rocks
|
 |
« Reply #26 on: April 16, 2009, 01:51:41 pm » |
I saw this too and changed the second test in the if statement to:
previousMidnight(DateTime.now())+ alarmOffTime;
and it worked for me.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 7
Arduino rocks
|
 |
« Reply #27 on: May 29, 2010, 03:46:50 pm » |
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 =/)
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35559
Seattle, WA USA
|
 |
« Reply #28 on: May 29, 2010, 04:09:59 pm » |
None of this code is inside a function: 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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 7
Arduino rocks
|
 |
« Reply #29 on: May 29, 2010, 04:31:01 pm » |
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 :-/
|
|
|
|
|
Logged
|
|
|
|
|
|