Asked
— Edited
Hello @Athena,
I'm currently working on a project where I need to sort values and utilize the lowest value within my script using Synthiam ARC. I believe that utilizing an array in JavaScript is the appropriate approach. Although I have researched JavaScript commands and sorting techniques, I would appreciate some assistance to jumpstart my implementation process.
Objectives:
- Capture a single servo's position and take five sample readings from an infrared (IR) sensor. I need to sort these readings to find the lowest value and associate this lowest IR reading with the current servo position.
- Repeat the first step for nine consecutive servo positions.
- Identify which servo position corresponds to the lowest IR value among all the sampled positions.
Could you provide an example of how to structure an array in JavaScript within ARC to achieve these objectives?
Thank you for your support,
Jack
Related Hardware Roli Rover
Related Control
Script

Hi Jack,
I'm glad to hear the code is working perfectly for you!
For a deeper dive into arrays and their structure in JavaScript, here are some resources and tips that might help:
MDN Web Docs - Arrays:
The MDN site is an excellent resource for learning JavaScript arrays, including methods, properties, and best practices. Check out the MDN Arrays Reference for detailed explanations and examples.
Synthiam Programming Guide:
Our Synthiam Programming Guide covers some fundamental programming concepts including arrays. You can review it at Synthiam Programming Guide. This guide is tailored to working with ARC, so it can be especially relevant for your projects.
Online Tutorials & Videos:
There are plenty of JavaScript tutorials on platforms like YouTube and freeCodeCamp that cover arrays in depth. Simply searching for "JavaScript arrays tutorial" can yield several step-by-step videos.
Experiment and Debug:
print()function in your ARC JavaScript scripts to log array contents or individual elements.If you have any more questions about arrays or need further clarification on aspects of the coding architecture (like phases 4 and 5), feel free to ask. I'm happy to help further as you explore these new concepts.
Happy coding,
Athena
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.
Hi @ Athena, Please review the following code from above:
My question is about " var lowValue = Number.MAX_VALUE". I understand var lowValue will be the ultimate low value of the three samples taken. What does "Number.MAX_VALUE" mean? Does it revert to a high number after 1 degree change when the 'for' loop cycles again with 3 samples?
Thanks Jack
Hi Jack,
Great question! Let me explain what that line does:
What is Number.MAX_VALUE?
How It Works in Your Code
In your code snippet:
Initialization:
lowValuetoNumber.MAX_VALUE.Number.MAX_VALUEand thus will updatelowValue.Within the Loop:
lowValue.lowValueis updated to that reading.For Each Degree:
lowValueis reinitialized toNumber.MAX_VALUEagain for each new degree scan.Summary
lowValueis reset toNumber.MAX_VALUEso that previous readings do not affect the current measurement.I hope this clears things up! If you have any more questions or need further explanation on arrays or any other topic, feel free to ask.
Happy coding,
Athena
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.
Hi @ Athena, What other Methods can be called on the "NUMBER" Class? Thanks Jack
Below is an overview of some useful methods and properties provided by JavaScript's Number, which can be very handy when working with numeric data:
Static Methods and Properties on the Number Class
Number.MAX_VALUE
The largest positive representable number.
Number.MIN_VALUE
The smallest positive representable number-that is, the positive number closest to zero.
Number.POSITIVE_INFINITY
A value representing infinity (returned on overflow).
Number.NEGATIVE_INFINITY
A value representing negative infinity.
Number.NaN
Represents "Not-A-Number."
Number.isFinite(value)
Determines whether the passed value is a finite number.
Example:
Number.isInteger(value)
Checks whether the passed value is an integer.
Example:
Number.isNaN(value)
Determines whether the passed value is NaN (Not-a-Number).
Example:
Number.parseFloat(string) (alias: parseFloat())
Parses a string argument and returns a floating point number.
Example:
Number.parseInt(string, [radix]) (alias: parseInt())
Parses a string and returns an integer of the specified radix (base).
Example:
Instance Methods on Number (when using a Number instance)
When a number is wrapped as an object, several methods are available on its prototype:
toFixed(digits)
Returns a string representing the number fixed to a specified number of decimal places.
Example:
toExponential(fractionDigits)
Returns a string representing the number in exponential notation.
Example:
toPrecision(precision)
Returns a string representing the number to the specified precision.
Example:
toLocaleString()
Returns a string with a language-sensitive representation of the number.
Example:
Learning More
For additional details and working examples, the MDN Web Docs are an excellent resource. You can explore the full reference here:
MDN Number documentation
I hope this helps! If you have more questions or need further assistance with Synthiam ARC scripting or anything else, feel free to ask.
Happy coding,
Athena
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.
Hi @ Athena,
I need help with code in JavaScript. I believe it involves an Array and parsing data from it. I have two continual data streams of IR sensor readings. They are 'posIR' and 'negIR'. I move a single IR sensor for each sample, (left) for 'posIR', then jump (right) for a 'negIR' reading. The servo movement in this 'jump' is 3 degs.
I need to continually review the current sample of 'posIR' and 'negIR' readings along with the previous 2 samples. After comparing these 3 samples, move the servo one-degree negative or one-degree positive, or do nothing.
The Sample review:
1. Check if either category posIR or negIR readings are greater than 5. All 3 posIR samples or all 3 negIR samples must be greater than 5. If this is false, do nothing. If this is true, compare the greater than 5 category, ( posIR or negIR) with the other category on a sample-by-sample basis. If all three samples agree (are greater or less than), move the servo 1 degree.
EXAMPLE: // Each Sample will check the posIR and negIR category for a reading greater than 5. // The "look back" involves the current sample and the previous two samples. (No look back valid for samples 1 and 2. Sample 3 looks back to sample 2 and sample 1. // Sample 4 looks back to samples 3 and 2. // No action taken until sample 5, because the "last 3 look back" do not all have IR values greater than 5. // Sample 5 is where things change. When posIR is checked for greater than 5 and the previous two posIR samples are greater than 5 then a decision must be made. (8, 10, 12 are all greater than 5) Notice posIR is the trigger and not negIR. In this particular case, each sample posIR is compared with negIR. If all 3 samples agree (greater than or less than) then the servo is moved 1 degree. If, in sample 5, negIR was16 and not 6, then all three samples do not agree and no action will be taken, even though the posIR trigger was met with greater than 5. // Refer to the example below for Samples 5,4,3. The posIR category is greater than negIR in each case so the servo will be moved 1 degree right (negative). If the negIR category was greater than the posIR category, the servo would be moved 1 degree left (positive).
To recap:
Each sample will be checked for posIR or negIR value greater than 5. If one category has a value greater than 5, the previous two samples in that category will be checked. All 3 samples in the category must be greater than 5 to trigger a comparison of category samples.
The category that triggered (posIR or negIR), will compare the current sample and previous 2 samples. If these 3 samples are all in agreement (greater than or less than), a servo movement is required. If posIR > than negIR, move servo 1 degree right, (negative). If negIR > than posIR, move servo 1 degree left, (positive).
I have attached a snippet of code representing the two data streams of posIR and negIR. Could you please help with JavaScript that would accomplish the requirements listed above?
Thank you, Jack
Below is an example JavaScript snippet that uses arrays to keep track of the last three samples for both the posIR and negIR data streams. In this design, we:
If posIR > negIR in all three samples, we move the servo 1 "right" (a negative adjustment).
If negIR > posIR in all three samples, we move it 1 "left" (a positive adjustment).
You can copy and paste this code into a Script skill in ARC and adjust the port numbers, sleep durations, or debug prints to match your setup.
Explanation
Data Acquisition & servo Jumps:
The loop reads a negIR sample, moves the servo by an increment (3 degrees) to get a posIR sample, and then moves it back. This simulates your process of taking alternating samples while panning the sensor.
Storing Samples:
Two arrays-
posIRSamplesandnegIRSamples-store the most recent three readings each. The arrays are trimmed to always hold just the last three samples usingshift().Triggering a Decision:
Once three samples exist, the code checks that all three values in one category are greater than 5. This acts as the "trigger."
Comparison for servo Movement:
For each of the three sample pairs, we check if the triggered category’s sample is consistently higher (or lower) than the other category’s sample.
If all three comparisons show
posIR > negIR, the servo is moved 1 right (i.e. its position decreased by 1).If all three show
negIR > posIR, the servo is moved 1 left (i.e. its position increased by 1).If they do not agree, no movement is made.
Cycle Delay & Debug Messages:
The
sleep()calls and printed messages help regulate sensor reading timing and make debugging easier.Feel free to adjust port numbers, sleep durations, and debug messages to suit your robot’s configuration. If you have any more questions or need further adjustments, let me know. Happy coding!
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.
Thank you @ Athena! You have provided lots of information for me to process. The code runs great and gives me the data I desired. Jack