United Kingdom
Asked — Edited

Speaking The Time

@Rich.

I was looking through the forum and came across a thread where you mentioned you made a script to speak the current time. I tried it out but I'm having a problem with it. When I start the script, it only says "The time is dollar the time". Is there something that I need to change in the syntax as I know this posted a year ago and something might have changed.


IF ($hour>12)
  $hour_word=$hour-12
ELSE 
  $hour_word=$hour
ENDIF 
  
IF ($hour_word=0)
  $hour_word=12
ENDIF 
  
IF ($Minute = 0)
  $thetime = "$hour_word"
ELSEIF ($minute = 1)
  $thetime = "one minute past $hour_word"
ELSEIF ($minute = 15)
  $thetime = "quarter past $hour_word"
ELSEIF ($minute = 30)
  $thetime = "half past $hour_word"
ELSEIF ($minute  30)
  $hour_word=$hour_word+1
  $minute_word = 60-$minute
  $thetime = "$minute_word minutes to $hour_word"
ELSE 
  $thetime = "$hour_word $minute"
ENDIF 
  
IF ($hour 21)
  $thetime = "$thetime at night"
ELSEIF ($hour > 17)
  $thetime = "$thetime in the evening"
ELSEIF ($hour > 11)
  $thetime = "$thetime in the afternoon"
ENDIF 
  
Say("The time is $thetime")

Many thanks.


ARC Pro

Upgrade to ARC Pro

ARC Pro is your gateway to a community of like-minded robot enthusiasts and professionals, all united by a passion for advanced robot programming.

#1  

You made a mistake on the last line... You put the variable in quotes... Say("The time is $thetime") should be Say("The time is "+$thetime)

You can also use the reserved variable for time.... which is $time... There is also $hour, $minute...etc... these variables can be found at the end if the script function list in every script...


#quick and dirty code
Say("The time is "+$time)

Variable Constants/Reserved Words These variables are read-only reserved words and cannot be assigned. $direction $date $month $year $day $dayName $hour $minute $second $monthName $time $pi

#2  

... Oh man, who wrote this it's a mess? I know for sure @Rich didn't...LOL...Way more corrections... variables shouldn't be in quotes and some operator signs were missing... There still may need more corrections...



IF ($hour>12)
  $hour_word=$hour-12
ELSE 
  $hour_word=$hour
ENDIF 
  
IF ($hour_word=0)
  $hour_word=12
ENDIF 
  
IF ($Minute = 0)
  $thetime = $hour_word
ELSEIF ($minute = 1)
  $thetime = "one minute past "+$hour_word
ELSEIF ($minute = 15)
  $thetime = "quarter past "+$hour_word
ELSEIF ($minute = 30)
  $thetime = "half past "+$hour_word
ELSEIF ($minute =30)
  $hour_word=$hour_word+1
  $minute_word = 60-$minute
  $thetime = $minute_word+" minutes to "+$hour_word
ELSE 
  $thetime = $hour_word+ $minute
ENDIF 
  
IF ($hour >21) #I assume it is greater than as the sign was left out in the code
  $thetime = $thetime+" at night"
ELSEIF ($hour > 17)
  $thetime = $thetime+" in the evening"
ELSEIF ($hour > 11)
  $thetime = $thetime+" in the afternoon"
ENDIF 
  
Say("The time is "+$thetime)

#3  

@Steve.... Tried to run my version, but it doesn't work well at all.... I think this has to be completely re-written my friend... I could fix it, but I won't have anytime today... Sorry dude

United Kingdom
#4  

@Richard.

Not a problem mate. I copied this code straight from Rich's example and was curious if there was a new way to write this, as his example is over a year old. If you wouldn't mind haveing a look when you have some free time I would appreciate it. In the meantime I will have a play around too (but I don't hold much hope with my current scripting skills eyeroll).

#5  

@Steve... can you show me the link to it? No promises, but I will see what I can do...

#6  

Try this... It may still need more tweaking.... make sure you copy it exactly as it is below...


IF ($hour>12)
  $hour_word=$hour-12
ELSE 
  $hour_word=$hour
ENDIF 
  
IF ($hour_word=0)
  $hour_word=12
ENDIF 

IF ($Minute = 0)
  $thetime = $hour_word
ELSEIF ($minute = 1)
  $thetime = "one minute past "+$hour_word
ELSEIF ($minute = 15)
  $thetime = "quarter past "+$hour_word
ELSEIF ($minute = 30)
  $thetime = "half past "+$hour_word
ELSEIF ($minute =30)
  $hour_word=$hour_word+1
  $minute_word = 60-$minute
  $thetime = $minute_word+" minutes to "+$hour_word
ELSE 
  $thetime = $hour_word+" hours and "+$minute+" minutes"
ENDIF 
 IF ($hour >21) #I assume it is greater than as the sign was left out in the code
  $thetime = $thetime+" at night"
ELSEIF ($hour > 17)
  $thetime = $thetime+" in the evening"
ELSEIF ($hour > 11)
  $thetime = $thetime+" in the afternoon"
ELSE
  $thetime = $thetime+" in the morning"
ENDIF 
  
Say("The time is "+$thetime)

United Kingdom
#7  

Sure. The link for Rich's project is hear at the end of post #6.

EDIT:

I just tried your code Richard and it works pretty well.

"The time is 12 hours and 51 minutes in the afternoon."

I'd like the lose the "hours" "minutes" and change "afternoon" for "PM" if poss, but hey, it works. Thanks buddy. :)

#9  

LOL.... @Rich must of been pulling an all nighter when he wrote that code.... It's probably not his anyway.... I know, Melvin probably wrote it while Rich was in the shower....:P

United Kingdom
#10  

That would explain it :D. I just posted an edit in post #8.

Thanks again.

United Kingdom
#11  

Got rid of the speech saying "Hours" "Minutes" and changed "Afternoon" to "PM".

Cheers for doing that for me Richard. ;)

United Kingdom
#13  

I haven't read the whole topic as I don't have time right now but always check the syntax on older projects. The syntax was changed a while back but any scripts from before the change may not work as desired.

I did spot @Richard mention the variable needs to be outside of the quotes, which is correct, but it also need a little more to it (which may have been done but like I said, I didn't have time to read it all).

Correct syntax for speaking a sentence including a variable is...

SayWait("The time is " + $hour + $minute)

Note the addition of the + in the code.

Also, I've probably got a much better script now than back then. Time is a tricky one for speaking since we don't say "It's 11:55" we say "It's 5 to 12" or we don't say "It's 7:05" we say "it's 5 past 7" (amongst other variations).

I'll see what other scripts I have floating around in my projects once I've had some sleep:)

United Kingdom
#14  

Hi Rich. I heed your words about checking syntax on old scripts. As I only started using ARC a few months ago, I must admit that it didn't initially think about any code or script changes or improvements at first, but suspected that might be the case after looking through other forum threads. With Richards help, here's what I'm using now...


IF ($hour>12)
$hour_word=$hour-12
ELSE
$hour_word=$hour
ENDIF
IF ($hour_word=0)
$hour_word=12
ENDIF
IF ($Minute = 0)
$thetime = $hour_word
ELSEIF ($minute = 1)
$thetime = "one minute past "+$hour_word
ELSEIF ($minute = 15)
$thetime = "quarter past "+$hour_word
ELSEIF ($minute = 30)
$thetime = "half past "+$hour_word
ELSEIF ($minute =30)
$hour_word=$hour_word+1
$minute_word = 60-$minute
$thetime = $minute_word+" minutes to "+$hour_word
ELSE
$thetime = $hour_word+" "+$minute+ " "
ENDIF
IF ($hour >21) 
$thetime = $thetime+" AM"
ELSEIF ($hour > 17)
$thetime = $thetime+" PM"
ELSEIF ($hour > 11)
$thetime = $thetime+" PM"
ELSE
$thetime = $thetime+" AM"
ENDIF

Say("The time is "+$thetime)

It works very well but I do agree with what you say about how we speak the time, but I am happy with this for now. :)

United Kingdom
#15  

My original project will be re-written with improvements for the time phrases shortly.

Time phrase updates will include: It is $hour o clock in the $timeofday It is $minute minutes past $hour in the $timeofday It is quarter past $hour in the $timeofday It is half past $hour in the $timeofday It is $minutesto to $hour+1 in the $timeofday It is quarter to $hour+1 in the $timeofday It is $minutes past $hour in the $timeofday It is $hour $minutes in the $timeofday

It shouldn't be too difficult to write for these phrases. I'll probably do another speak the "rounded" time where it'll round to 5 or 10 minute intervals (just as we do) with "almost", "around and "just gone".

I needed something to get my teeth stuck in to just to keep me sharp, this should be a good start:)

United Kingdom
#16  

Yeah I know the feeling. Anyway that would be great. I look forward to see what you'll come up with. ;)

United Kingdom
#17  

Just jumped on this quickly while I had a few minutes spare. it's by no means the final draft and I am certain I can improve on it (for one the IFs can use and and or to make less code) but give this a whirl.


# Speak the time phrase script
# Written for: EZ-Script
# Author: Rich Pyke
# Version: 2014.12.12

# Description: Speak the current time in 12 hour
# format and in a human-like style phrase.

# Variables used:
# $hour (built in variable)
# $minute (built in variable)
$spokenhour = $hour
$spokenminute = $minute
$timeofday = 0
$timephrase = 0
$precedingminutes = 0

# Main Script Code


# Get the current minutes and reformat as needed
IF($minute = 0)
  $spokenminute = "o clock"
  $precedingminutes = 0
ElseIf($minute < 15)
  $spokenminute = $minute + "minutes past"
  $precedingminutes = 1
ElseIf($minute = 15)
  $spokenminutes = "quarter past"
  $precedingminutes = 1
ElseIf($minute < 30)
  $spokenminutes = $minute + "minutes past"
  $precedingminutes = 1
ElseIf($minute = 30)
  $spokenminute = "half past"
  $precedingminutes = 1
ElseIf($minute < 45)
  $spokenminute = (60 - $minute) + "minutes to"
  $spokenhour = $hour + 1
  $precedingminutes = 1
ElseIf($minute = 45)
  $spokenminute = "quarter to"
  $spokenhour = $hour + 1
  $precedingminutes = 1
Else
  $spokenminute = 60 - $minute
  $spokenminute =  $spokenminute + "to"
  $spokenhour = $hour + 1
  $precedingminutes = 1
EndIf

# Get the current hour and reformat to 12 hour as needed
IF($spokenhour > 12)
  $spokenhour = $spokenhour - 12
EndIf

# Work out the time of day
If($hour < 12)
  $timeofday = " in the morning"
ElseIf($hour < 19)
  $timeofday = "in the evening"
Else
  $timeofday = "at night"
EndIf

# Create the time phrase
IF($precedingminutes = 1)
  $timephrase = "The time is " + $spokenminute + " " + $spokenhour + " " + $timeofday
ELSE
  $timephrase = "The time is " + $spokenhour + " " + $spokenminute + " " + $timeofday
ENDIF

Say($timephrase)
HALT()

Not fully tested but tested between 6:25 and 6:35 PM and it worked OK. I'm not entirely sure what happens at 11pm and later though but will try it out in a few hours time if I get chance.

It's a starter anyway, feel free to make it better ;)

United Kingdom
#18  

Nice one. Thanks Rich. I just got home so I will try it out this evening and let you know how it goes. Looking forward to 11pm (if I'm still awake). :P

United Kingdom
#19  

Thinking about it, 11pm will be fine. It's when it passes 12:30 am it may give funny times, possibly, I'll have to check that ElseIf out a bit more:)

United Kingdom
#20  

I just tried it out and it sounds really nice. Much more human sounding. Lol, NO chance of me being awake at 12:30. I'll be lucky if I make it to 9:30. tired tired

United Kingdom
#21  

I personally think it's too specific. When did anyone last say "It's 17 minutes to 9"? I plan to round to 5 minute intervals for the one I will be using.

But the script shows a nice big if nest and some other functions which (hopefully) will help people get to grips with the programming:)