Asked — Edited
Resolved Resolved by Athena AI!

Help With Sorting Array Implementation

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:

  1. 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.
  2. Repeat the first step for nine consecutive servo positions.
  3. 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

ARC Pro

Upgrade to ARC Pro

Subscribe to ARC Pro, and your robot will become a canvas for your imagination, limited only by your creativity.

PRO
Canada
#9  

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:

  1. 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.

  2. 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.

  3. 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.

  4. Experiment and Debug:

    • Use the print() function in your ARC JavaScript scripts to log array contents or individual elements.
    • Create small scripts solely to experiment with array operations like sorting, slicing, or looping. Hands-on coding is one of the best ways to learn!

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.
#10  

Hi @ Athena, Please review the following code from above:

 // For each position, take 3 IR sensor samples and record the lowest value
    var lowValue = Number.MAX_VALUE;
    for (var sample = 0; sample < 3; sample++) {
        var reading = ADC.get(irSensorPort);
        print("  Pos " + pos + " sample " + (sample + 1) + ": " + reading);
        if (reading < lowValue) {
            lowValue = reading;
        }
        sleep(100); // Delay between samples
    }

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

PRO
Canada
#11  

Hi Jack,

Great question! Let me explain what that line does:

What is Number.MAX_VALUE?

  • Number.MAX_VALUE is a built-in JavaScript constant that represents the largest numeric value possible in JavaScript.
  • It’s essentially used as a starting point when you need to find a minimum value in a set of numbers. By initializing with a value that is extremely high, any valid IR reading (which will be much lower) will replace it.

How It Works in Your Code

In your code snippet:

// For each position, take 3 IR sensor samples and record the lowest value
var lowValue = Number.MAX_VALUE;  
for (var sample = 0; sample < 3; sample++) {  
    var reading = ADC.get(irSensorPort);  
    print("Pos " + pos + " sample " + (sample + 1) + ": " + reading);  
    if (reading < lowValue) {  
        lowValue = reading;  
    }  
    sleep(100); // Delay between samples  
}
  1. Initialization:

    • Before the loop that takes 3 samples, you set lowValue to Number.MAX_VALUE.
    • This ensures that the first sensor reading will always be lower than Number.MAX_VALUE and thus will update lowValue.
  2. Within the Loop:

    • Each time the loop runs, the current sensor reading is compared to lowValue.
    • If the reading is lower, lowValue is updated to that reading.
  3. For Each Degree:

    • This process is repeated for every new degree (each new iteration of the outer loop).
    • Yes, the variable lowValue is reinitialized to Number.MAX_VALUE again for each new degree scan.
    • That way, you always start fresh for the new set of 3 IR sensor readings.

Summary

  • Number.MAX_VALUE provides a very high starting threshold.
  • For each degree scan (each 1 increment/decrement), lowValue is reset to Number.MAX_VALUE so that previous readings do not affect the current measurement.
  • This ensures that the smallest of the three readings at each degree step is correctly captured.

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.
#12  

Hi @ Athena, What other Methods can be called on the "NUMBER" Class? Thanks Jack

PRO
Canada
#13  

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:

    print(Number.isFinite(123)); // true
    print(Number.isFinite(Infinity)); // false
    
  • Number.isInteger(value)
    Checks whether the passed value is an integer.
    Example:

    print(Number.isInteger(10)); // true
    print(Number.isInteger(10.5)); // false
    
  • Number.isNaN(value)
    Determines whether the passed value is NaN (Not-a-Number).
    Example:

    print(Number.isNaN(NaN)); // true
    print(Number.isNaN("Hello")); // false
    
  • Number.parseFloat(string) (alias: parseFloat())
    Parses a string argument and returns a floating point number.
    Example:

    print(Number.parseFloat("3.14")); // 3.14
    
  • Number.parseInt(string, [radix]) (alias: parseInt())
    Parses a string and returns an integer of the specified radix (base).
    Example:

    print(Number.parseInt("100", 10)); // 100
    

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:

    var value = 3.14159;
    print(value.toFixed(2)); // "3.14"
    
  • toExponential(fractionDigits)
    Returns a string representing the number in exponential notation.
    Example:

    print((123456).toExponential(2)); // "1.23e+5"
    
  • toPrecision(precision)
    Returns a string representing the number to the specified precision.
    Example:

    print((123.456).toPrecision(5)); // "123.46"
    
  • toLocaleString()
    Returns a string with a language-sensitive representation of the number.
    Example:

    print((123456.789).toLocaleString()); // Might output "123,456.789" based on locale
    

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.