Asked — Edited
Resolved Resolved by JustinRatliff!

Variable Not Defined: $Associate

So I'm getting an error that says: " Error on line 21: Variable not defined: $Associate " So how do I define a variable?



Code:

:Start

# check for motion
if(getDigital(D18)=1)
Print (" associate detected")
$Associate = Y
$AssociateTime = $time
sayEZB(" I SEE a associate")
sleep(3000)
endif

if(getDigital(D17)=1)
$Customer =  Y
$CustomerTime = $time
Print ("customer detected")
sayEZB(" I SEE an Customer. ")
sleep(3000)
endif

# Lets see if we need to greet associate 
if($Associate = Y)
 if($AssociateGreeting = N)
     $AssociateGreeting = y
     # turn head to look in associate direction LEFT
      Servo(D0,135)
      Servo(D1, 100)
    print(" Greeting Associate")
  if($customer = N)   
    $x101 = GetRandom( 1,7 )
    if($x101 = 1)
     SayEZBWait(" sIR  ")
     elseif ($x101 = 2)
     SayEZBWait(" HELLO SIR ")
     elseif ($x101 = 3)
     SayEZBWait(" hello ")
     elseif ($x101 = 4)
     SayEZBWait(" Hi")
     elseif ($x101 = 5)
     SayEZBWait(" hi there  ")  
     elseif ($x101 = 6)
     SayEZBWait(" back a gin sir?  ")       
     endif
     $x101 = 0
  else
        $x101 = GetRandom( 1,7 )
     if($x101 = 1)
      SayEZBWait(" Sir can you help this costomer?  ")
     elseif ($x101 = 2)
      SayEZBWait(" HELLO SIR this costomer needs your help ")
     elseif ($x101 = 3)
     SayEZBWait(" hello sir, we have a costomer ")
     elseif ($x101 = 4)
     SayEZBWait(" Sir we have a customer who needs your help. ")
     elseif ($x101 = 5)
     SayEZBWait(" Sir we have a customer  ")  
     elseif ($x101 = 6)
     SayEZBWait(" Customer sir.  ")       
     endif
     $x101 = 0 
 
  endif    
    # face forward
    Servo(D0, 90)
    Servo(D1, 100)
      
 endif
endif

goto(Start)



ARC Pro

Upgrade to ARC Pro

Stay on the cutting edge of robotics with ARC Pro, guaranteeing that your robot is always ahead of the game.

#1  

Before :Start or in a separate Int script you run first you might want to declare it as follows:


$Associate = "0"

#2  

But do you know why you need to do that? It's a good idea to declare all variables upfront with default values before they are used.

#3  

You don't have to declare a variable but you have to assign a value to a variable... Just put $Associate ="null" or $Associate =0 or whatever at the beginning of your script.... You have it = y... but does y have a value?

#4  

Y is the value he is settings, he is settings Y and N values for the $Customer and $Associate. You need set string quotes around all the values you are setting variables to, like this:


$Associate = "Y"

And you also need to make sure what you set is the same value, because Y is the not the same is y. Lots of little mistakes.

#5  

Here is your script with the corrections needed:


$Associate = "null"
$AssociateGreeting = "null"
:Start

# check for motion
if(getDigital(D18)=1)
Print (" associate detected")
$Associate = "Y"
$AssociateTime = $time
sayEZB(" I SEE a associate")
sleep(3000)
endif

if(getDigital(D17)=1)
$Customer = "Y"
$CustomerTime = $time
Print ("customer detected")
sayEZB(" I SEE an Customer. ")
sleep(3000)
endif

# Lets see if we need to greet associate 
if($Associate = "Y")
 if($AssociateGreeting = "N")
 $AssociateGreeting = "Y"
 # turn head to look in associate direction LEFT
 Servo(D0,135)
 Servo(D1, 100)
 print(" Greeting Associate")
 if($customer = "N") 
 $x101 = GetRandom( 1,7 )
 if($x101 = 1)
 SayEZBWait(" sIR ")
 elseif ($x101 = 2)
 SayEZBWait(" HELLO SIR ")
 elseif ($x101 = 3)
 SayEZBWait(" hello ")
 elseif ($x101 = 4)
 SayEZBWait(" Hi")
 elseif ($x101 = 5)
 SayEZBWait(" hi there ") 
 elseif ($x101 = 6)
 SayEZBWait(" back a gin sir? ") 
 endif
 $x101 = 0
 else
 $x101 = GetRandom( 1,7 )
 if($x101 = 1)
 SayEZBWait(" Sir can you help this costomer? ")
 elseif ($x101 = 2)
 SayEZBWait(" HELLO SIR this costomer needs your help ")
 elseif ($x101 = 3)
 SayEZBWait(" hello sir, we have a costomer ")
 elseif ($x101 = 4)
 SayEZBWait(" Sir we have a customer who needs your help. ")
 elseif ($x101 = 5)
 SayEZBWait(" Sir we have a customer ") 
 elseif ($x101 = 6)
 SayEZBWait(" Customer sir. ") 
 endif
 $x101 = 0 

 endif 
 # face forward
 Servo(D0, 90)
 Servo(D1, 100)

 endif
endif

goto(Start)

#6  

Just an additional note for the future. Variables are global across scripts, so if you set it in one script, you can use it in another. This means you can initialize all of your variables in your init script, and you can use data learned in one script in another. It can be bad because if you used a variable in one script and use the same variable name in another script but intend for it to mean something else, you can get unpredictable results.

To be sure not to have that happen, I initialize all of my variables in my init script, but that is just personal preference.

Alan

#8  

Thank you all for the information. I love the idea that variables are script wide. That's really useful. Thanks JustinRatliff , thetechguru and DJ for all the help. I went and looked over the tutorial DJ suggested and learned a lot. Thanks.