Award
Congratulations Rich, for answering the question and has received an EZ-Credit award! Share what you have learned and create a tutorial to help others by clicking here.

Script To Get My Robot To Say His Age

Assistance Requested

Help Steve G with their question and receive $10 of EZ-Credit to get more robots and parts from our store. The following information was provided about their previous efforts searching tutorials for a resolution.

Steve G claims to have checked these sources:
 
#1

Hi guys.

I need a little help with some script. As the title says, I would like to get K-9 to say his age when somebody asks him. For example,

User. "how old are you?"

K-9. "I am 1 year, 2 months, 5 months and 2 days old."

It will obviously need a start date (Date of Birth) then have the script work out the age from that, but so far this weekend I have had a play around with a "Say the Date" script with different variations but have had no luck with it. Does anyone have a script that can be used to do this?

Thanks in advance,

Steve G.

I have found a few conversations, tutorials and activities that may help with your question. Take a look at these links. I've sorted them by what I believe to be most relevant but that is not always the case as I'm still learning.


Also, consider reviewing the Learn section for informative lessons and activities. Check it out!


#2

I'm not going to write it for you, but will give you a push in the right direction...

There are the following built in variables in EZ-Builder;
$date
$month
$year
$day

You also have the following DateTime functions available;
MinDate()
MaxDate()
MonthName()
AddDays()
AddMonths()
AddYears()
AddHours()
AddMinute()
AddSeconds()
FmtNum()
FmtDate()

You just need to figure out how to;
1) Set the date of birth in EZ-Script
2) Subtract the date of birth from the current date
3) Convert days to days, months and years
4) Speak the result


Alternately, use PandoraBot, you can set a DOB in there and it does the calcs for you Smile

#3

@Rich.... I am sitting here staring at the ceiling trying to figure out how to do this.... LOL. I finally had an idea which was similar to yours, but since my "cpu" is a lot slower than yours you beat me to it... LOL Tongue

#4

Thanks for the replies guys. I have been using the $date $month $year and $day codes which the say the date script uses but no matter what I tried I just couldn't get it to work. I think the main problem is setting the date of birth. I would have posted a script to show you guys, but I changed what I had so many times I ended up deleting it in the end. Frown

Thanks for the Pandorabot suggestion Rich, but I was already aware I could do it this way, but I really wanted to do this using Script's so I could learn much more about how EZ Script works and what I could do with it.

#5

Well I gave writing the "Say your age" script again this evening but to no avail. I've obviously still got a way to go with EZ Script but I'm sure I'll get there. Thanks again for the Pandorabot suggestion Rich. I think I will do it this way. As I mentioned I was aware I could do it this way but never tried it, so reluctantly I'm going to go down this route. Would like to have used EZ Script though as I enjoy using it, but I just dont have enough experience to do this just yet. *confused* Smile

#6

If you want "I am 2 years, 11 months, 14 days, 8 hours, 27 minutes and 37 seconds old" then it's going to be long scripts.

If you wanted something as simple as "I am 7 years old" or "I am 19 months old" then it's a little easier.

For a simple yearly response;

Code:


$yearofbirth = 1980
$age = $year - $yearofbirth

SayWait("I am " + $age + " years old")



For year and month

Code:


$yearofbirth = 1980
$monthofbirth = 1

$ageyear = $year - $yearofbirth
$agemonth = $month - $monthofbirth

IF($agemonth < 0)
$agemonth = $agemonth + 12
$ageyear = $ageyear - 1
EndIf

If($agemonth > 0)
SayWait("I am " + $ageyear + " years and " + $agemonth + " months old")
Else
SayWait("I am " + $ageyear + " years old")
EndIf



With the latter you could use further ifs to force ages below 3 years to months only (as we tend to do with children).

Code:


$yearofbirth = 2012
$monthofbirth = 12

$ageyear = $year - $yearofbirth
$agemonth = $month - $monthofbirth

IF($agemonth < 0)
$agemonth = $agemonth + 12
$ageyear = $ageyear - 1
EndIf

If($ageyear < 3)
$agemonth = $agemonth + $ageyear * 12
SayWait("I am " + $agemonth + " months old")
ElseIf($agemonth > 0)
SayWait("I am " + $ageyear + " years and " + $agemonth + " months old")
Else
SayWait("I am " + $ageyear + " years old")
EndIf



These are just a few quick examples which I hope get you started. I haven't done one for including days, this should be pretty simple to add in if you follow what I did for the years and the months.

#7

I'm glad you provided that example Rich, that much easier than what I was starting to do....which was to take the string of $date and parse it for the values of the month, day and year to do math against a birth date.

#8

That's how I started looking at it earlier today but I've come to learn that there is always a simpler method than how I first think.

#9

That's great. Thanks for the examples Rich. I will give them a try tomorrow. Quite different to what I initially wrote, and a lot shorter. But looking at the examples you supplied some of it is starting to make a bit more sense to me, and I can see parts where I was going wrong, especially setting the birth date, although I'm still not 100% sure I would have fully got it to work.

Thanks again for your help Rich. Smile

#10

Just one more thing I want to ask to get a better understanding of how EZ Script works, like ( ) defines ports ect and " " sets speech or speech commands, what does (in laymans terms Tongue) the "$" sign mean, or rather what does it actually do? I think that's where I was initially having trouble.