Asked — Edited

Numeric Keypad - Mobile Builder

Good Afternoon,

I am looking to create a numeric keypad within ARC. I want to pre-program a script for example 133. How do you combine the numbers into a string value, and that value is a number?

Thanks WayneA


ARC Pro

Upgrade to ARC Pro

Experience the transformation – subscribe to Synthiam ARC Pro and watch your robot evolve into a marvel of innovation and intelligence.

United Kingdom
#1  

One method would be to use simple math.

First digit is hundreds Second is tens Third is units

So, take the script, when the first number is used multiply it by 100 and add it to the register. Second, multiply by 10 and add to the register and finally add the third to the register.

Something like (this is just the math part, receiving the number data is a different matter):


$keycode = ($keypad1*100) + ($keypad2 * 10) + $keypad3

#2  

Thank You Rich,

To go one step further, my keypad would be 0-9. My thought was to make it easy by using just one string value and combining that, but looking at your script above, that wont work.

So, how would you get the data into the string values?

United Kingdom
#3  

I presumed it would be 0 to 9. You end up with a 3 digit code which varies between 000 and 999, it's a numeric value at the end of the day which can be used. Failing that, use quotes to associate the number with a string rather than a value. "1" + "2" + "3" = "123" whereas 1 + 2 + 3 = 6.

Getting the data to the string will totally depend on the keypad and how it works, how it's connected, how ARC is aware of the activity on the keypad etc.

#4  

What is the best way to configure the buttons?

United Kingdom
#5  

I need more information on the keypad to be able to even think about how to answer that. How does it work? How does it connect to the EZ-B or the PC? Is it just 10 push switches or does it have it's own PIC? etc. etc. etc.

Trinidad/Tobago
#6  

where would the entered number go? is it for calculations?

#7  

No, I would use the button from the Mobile Interface. When you press the button, my first thought was to create a string value. Imagine a your tablet with buttons in a layout of a simply keypad. The button press would make $Button1 = "1" . In my main script I would declare that $Button1 = "". Then somehow recognize that change into an if then statement. Then use that if then statement to ControlCommand the numbered script. No hardware keypad all use from software.

Hope that clears things up.

United Kingdom
#8  

Sorry I misunderstood, I thought you had an actual keypad.

In that case it's easy. It's a lot of scripts which are pretty much the same but it's easy.

For instance, button 4 script would be;


IF($sequence = 1)
  $keycode = 4000
  $sequence = 2
  Halt()
ElseIf($sequence = 2)
  $keycode = $keycode + 400
  $sequence = 3
  Halt()
ElseIf($sequence = 3)
  $keycode = $keycode + 40
  $sequence = 4
  Halt()
ElseIf($sequence = 4)
  $keycode = $keycode + 4
  $sequence = 0
  Halt()
Else
  SayEZB("There has been an error")
EndIf

Button 0 would be;


IF($sequence = 1)
  $keycode = 0
  $sequence = 2
  Halt()
ElseIf($sequence = 2)
  $keycode = $keycode + 0
  $sequence = 3
  Halt()
ElseIf($sequence = 3)
  $keycode = $keycode + 0
  $sequence = 4
  Halt()
ElseIf($sequence = 4)
  $keycode = $keycode + 0
  $sequence = 0
  Halt()
Else
  SayEZB("There has been an error")
EndIf

However the $keycode = $keycode + 0 lines are redundant.

Your keycode script would also need to be running;


:loop
WaitFor($sequence = 0)
IF($keycode = 0)
  # Run action for 0000
ElseIf($keycode = 1)
  # Run action for 0001
...
ElseIf($keycode = 4444)
  # Run action for 4444
ElseIf($keycode = 4445)
  # Run action for 4445
...
ElseIf($keycode = 9998)
  # Run action for 9998
ElseIf($keycode = 9999)
  # Run action for 9999
Else
  SayEZB("There has been an error")
EndIf
Sleep(200)
$sequence = 1
Goto(loop)

Each button script will check the position in the code and add the correct number to the position. The other script will loop, wait for the sequence to be finished and then check the number, run the required code, reset the sequence to 1 and wait for a completed code again.

#9  

Thank You Rich. You have been a big help.

#10  

@VGosine Yes, a future project would be to expand on this keypad to do calculations. Do you have any ideas on how it could be done?

#11  

@rich I tried to use your code, it is way over my head. Thank You for the help, I got to break down your code and try to understand it a bit better.

Trinidad/Tobago
#12  

@WayneA if you wanted to do calculations with the number stored you would need to setup variables that would store a number entered, it would then store another variable for the operation when the operation is entered it would reset the buttons for entering a new number.

When i have some time i will see if i could come up with a similar interface and share the project with you.

United Kingdom
#13  

@WayneA, I don't know how I can break the code down further for you. Which part(s) are you struggling to get your head around?

#14  

I guess I do not understand the sequences that you have set up. I guess my thinking process is that for each button that is pressed, the script within the button is checking the running script to see if any other button is pressed. If it is, the button script knows how to handle.

Another thought that I had was to to just setup a string value, lets say $N. Create my 10 buttons and each press of the button would make the string value of $N = "1". Every time the $N value changed store that into another string separated by commas. Depending the amount of commas and the amount of numbers contained in the string, the output would equal to for example "444".

Then, another thought I had was to shift the string value down. (I am taking an online course to help me get the basics of code - Covering Left Shift Bits and Right Shift Bits.)

Then another thought was to re write my project to just press a button and CC("444", scriptstart )

United Kingdom
#15  

Make 10 script buttons in the mobile interface, label them 0 to 9

For each button have a script like this one (this example is for number 4)


IF($sequence = 1)
$keycode = 4000
$sequence = 2
Halt()
ElseIf($sequence = 2)
$keycode = $keycode + 400
$sequence = 3
Halt()
ElseIf($sequence = 3)
$keycode = $keycode + 40
$sequence = 4
Halt()
ElseIf($sequence = 4)
$keycode = $keycode + 4
$sequence = 0
Halt()
Else
SayEZB("There has been an error")
EndIf

To change for other numbers simply replace the 4000, 400, 40 and 4 for whichever number you are writing the script for. I.E. For number 2 use 2000, 200, 20 and 2. For 1 use 1000, 100, 10 and 1.

After each button press the sequence is increased so the scripts will know what value to add to the keycode.

You also need the script running that will trigger the requested scripts or actions when the code is sent to it. That's the other script I posted. You could have up to 10,000 different scripts or actions triggered which would require 10,000 IF statements. Obviously I haven't included all 10,000!

This is the script.


:loop
WaitFor($sequence = 0)
IF($keycode = 0)
# Run action for 0000
ElseIf($keycode = 1)
# Run action for 0001
...
ElseIf($keycode = 4444)
# Run action for 4444
ElseIf($keycode = 4445)
# Run action for 4445
...
ElseIf($keycode = 9998)
# Run action for 9998
ElseIf($keycode = 9999)
# Run action for 9999
Else
SayEZB("There has been an error")
EndIf
Sleep(200)
$sequence = 1
Goto(loop)

When the sequence in the number scripts is complete it resets the $sequence variable to 0. The above code waits for that to become 0 and then checks the sequence and runs the code between that sequences ELSEIF and the next ELSEIF. If the code doesn't match it runs the ELSE code so you needn't do all 10,000 statements.

At the end of the above code it resets the sequence to 1 again so the keypad code number scripts know the digit position. It then loops back to the start and waits for the sequence to be set as 0 again before it moves past the WaitFor line.

Most of the work is done for you above you just need to change the script for each number for the keypad code and fill in the ControlCommands in the second script above.

#16  

Thank You Rich, one more question, when the script runs, the order of numbers is wrong, for instance, if I click 732, the string value is 237, anyway to make outcome in the right order?

United Kingdom
#17  

Enter it in the right order. You could easily add in a cancel button which would simply reset the $keycode to 0 and $sequence to 1 but you couldn't expect a human to know you meant 237 if you said 732 so how can you expect a computer?

The cancel button code would be


$sequence = 1
$keycode = 0

Trinidad/Tobago
#19  

@WayneA Have a look at this. It's still a work in progress but it should give you an example on how to do calculations and receive the entered number in the correct order if you haven't fixed that problem yet

Calculator - Mobile Interface

The only thing wrong with it is that there is no decimal point and that you have to hit equal after each operation excluding the initial number input.

#20  

I have not been able to figure out the order yet. VGosine your script was a really big help. I truly do thank you.

#21  

After almost a couple of hours playing with both scripts, I have decided to just give up. Thank You for all of your help.

Trinidad/Tobago
#22  

I just made some changes to the file try it again ... And my script enters the number in the order you enter it so if you type 6 then 8 then 2 you will have 682 in a variable stored...

#23  

Try your calculator file that you posted?

Trinidad/Tobago
#24  

Yeah if you try the calculator file, when you press the numberpad buttons they enter the number in the order you press it... i'm not sure it that's helpful but this was the code:


IF ($order=0)
  $number=$currentnum
  $order++
ELSE
  $number=$number*Power(10,1)
  $number=$number+$currentnum
  $order++ 
ENDIF

This code is called when a button is pressed and the value of currentnum is changed to the number on the button you press. To run the calculator you would need to declare the variables first by starting the script "Variables".

#25  

What does the following below mean?



ELSE
 $number=$number*Power(10,1)
 $number=$number+$currentnum
 $order++
EndIf


What if you wanted to shift or move down the input data to another string variable? Would that depend on the $order string variable right?



IF ($order=0)
$number=$currentnum
$order++


$order++ means you are adding the number one to it right

Trinidad/Tobago
#26  

The first section of code can be replaced to


ELSE
 $number=$number*10
 $number=$number+$currentnum
 $order++
EndIf

you don't need the power function... but what this does is when you press a key it will move the first number entered to a new column and then add the new number pressed.

so if you press 1 the entered number is 1 if you press 2 the entered number is now 10+2=12 if you press 3 the entered number is now 120+3=123

that way it is just like a calculator when entering numbers on the keypad.

the entered number is stored in $number you can store this value in a different variable if you wish but it would be an integer to start not a string. and order++ increases the amount of digits stored in $number... so by resetting order to 0 you enter a new number into the $number variable.

#27  

How would you store $number into another variable after you are done entering it?

a script to look at the count?

United Kingdom
#28  
$newvariable = $oldvariable

So...

$anothervariable = $number
Trinidad/Tobago
#29  

You would create an enter button that signifies that you are done entering a number and then you would do as Rich said to store the value in another variable.