Asked
— Edited

@Athena When I write a script using Javascript and I want to set a variable what command should I use. I've used both setVar and simply just Var. Both have worked for me.
For example I have this in my INT Script that runs when my project starts:
setVar( "$adcSoilSpecified", 200 );
And I have this in a stand alone script that operates a motor and watches an encoder:
var Get_Lft_Elbow_P = "2, Getp\r";
UART.hardwareUartWrite(0,Get_Lft_Elbow_P.length,2); //UART 0 on EZB 2
Which is the correct one to use and what is the difference between the two?
Related Hardware EZ-B v4
@Athena , Edit and In addition to my last question post, The returned high and low value from the Kangaroo looks like this:
1,P800
or
1,P-20
Another question about these values
Why does the command UART.hardwareUartAvailable(0, 2); return that there are 8 bytes in the Uart and I only see 6 characters?
sorry for the repost. Again fo Atheina will see it:
@Athena , Edit and In addition to my last question post, The returned high and low value from the Kangaroo looks like this:
1,P800
or
1,P-20
Another question about these values
Why does the command UART.hardwareUartAvailable(0, 2); return that there are 8 bytes in the Uart and I only see 6 characters?
Im' guessing its this...
Because if there's a "-" present, the substring range would be 3, 8
so u could do this..
in the conversation earlier, i mentioned that the response incldues a \r and \n which are new line characters. you can't see new line characters. scroll up and reread for refresh
actually that code i posted checking for a - wont' work
there is no way for this to work at all with teh current ascii protocol. the trouble is you have no idea how many characters are in the buffer.
unless you read everything in the buffer. like the entire thing. and process that. but i dunno how that works. the trouble is that protocol ur using for ascii is made for human readible, not computers. it's impossible to predict if there will be a negative character or not, therefor not knowing how long teh string is going to be.
so reading the entire buffer and crossing your fingers would be...
For setting and using variables in JavaScript within ARC, the distinction between
var
(orlet
/const
in modern JavaScript) andsetVar()
comes down to the scope and persistence of the variable you're working with.Using
var
(orlet
/const
): These are standard JavaScript keywords for declaring variables. Variables declared withvar
,let
, orconst
are local to the script in which they are declared. This means they are not accessible outside of the script they are defined in. They are useful for temporary data that only needs to exist within the context of a single script execution.Example:
Using
setVar()
: This is an ARC-specific function that sets a global variable's value. Global variables in ARC are accessible across different scripts, robot skills, and even between ARC sessions if saved. UsesetVar()
when you need to store data that must be accessible globally across your ARC project.Example:
Regarding your question about the
UART.hardwareUartAvailable(0,2)
returning 8 bytes when you only see 6 characters, this discrepancy is likely due to the inclusion of non-visible control characters in the UART buffer. As DJ_Sures mentioned, the response includes\r
(carriage return) and\n
(newline) characters, which are counted as bytes but are not visible as characters when printed or displayed. These control characters are commonly used to mark the end of a line of text or a command in communication protocols.For handling variable-length responses from devices like the Kangaroo, reading the entire buffer and processing the received string, as suggested by DJ_Sures, is a practical approach. This method allows you to dynamically handle responses of different lengths and extract the necessary information regardless of whether there are negative signs or additional characters.
Remember, when working with global variables in ARC, using
getVar()
andsetVar()
allows you to access and modify these variables from any script within your project, providing a powerful way to share data across your robot's behaviors.I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
Ah, ok. Athena mentions the response includes \r (a carriage return) that I can't see but is there. I had forgotten that.
Now, for reading everything in the buffer and process that, I'll give it a try. Thanks for the guidance! I'll be back.
@Athena ,
I'm struggling a bit trying to find a Javascript command that will read a one, two tor three digit number stored in a variable, extract each digit separately, then turn them into different variables so each can be used to trigger different events.
For example, I have been able to do an internet weather API call from an internet weather service and use the downloaded information to get my local weather conditions. I've extracted the temperature from that download and loaded it into a variable that I'm using in an ARC Javascript script. The variable's name is "numberIs", For example sake the temp stored in this variable is the number 82. I have been able to confirm the two digit number is indeed stored in the Variable by doing a JS print command. Next, the peace of the puzzle I need to find; I need to split that number (82) and use the 8 digit (or what ever number is returned) to trigger my robot to say that number and the 2 digit (or what ever number is returned) to say it's number.
I just need to know how to get each number out of the variable, store it in it's own variable so I can use each one separately. From there I should be able to use each number's variable and finish the script as needed.
It seems there must be a simple answer. I've looked and can't seem to find the answer. Thanks in advance.