Asked

How To Create Multidimensional Arrays

Could you please show me how to create an array with 20 names, each associated with three values (x, y, z)? Additionally, I would like to generate a chart on the desktop that displays these names and their corresponding values.

Furthermore, I need to extract the values for a specific name and use them to control servo movements.

This assistance would be greatly appreciated.

Thank you, Don


ARC Pro

Upgrade to ARC Pro

With Synthiam ARC Pro, you're not just programming a robot; you're shaping the future of automation, one innovative idea at a time.

#145   — Edited

@Athena In regards to Add to Array we have been using  // Save the updated array back to the global variable setVar("$Items_Array", items);  which is inconsistant with the "$ItemsArray" that you just mentioned, but when I changed them to "$Items_Array" it says existing variable is not an array. Can you explain why this isn't working properly when I change it to it's proper name and how to fix it

PRO
Canada
#146  

Certainly! The issue you're encountering is likely due to a mismatch in how the global variable is being referenced and stored. Let's ensure consistency and correct usage of the global variable.

Step-by-Step Fix

  1. Define the Array and Save to Global Variable: Ensure that the array is defined and saved to the global variable correctly.
// Define the multidimensional array
var items = [
    { name: "Name1", x: 10, y: 20, z: 30 },
    { name: "Name2", x: 40, y: 50, z: 60 },
    { name: "Name3", x: 70, y: 80, z: 90 },
    // Add more entries as needed
];

// Save the array to a global variable
setVar("$Items_Array", items);

// Print the array for debugging
print("Array defined and saved to global variable:");
print(JSON.stringify(items));
  1. Add to Array Function: Ensure that the function to add new entries to the array uses the correct global variable name.
// Function to add a new entry to the array
function addToArray() {
    var addLocations = getVar("$Add_Locations");
    if (addLocations) {
        // Retrieve the array from the global variable
        var items = getVar("$Items_Array");

        // Check if the retrieved variable is an array
        if (Array.isArray(items)) {
            // Read the values from the specified virtual servo ports
            var x = Servo.getPosition("v26");
            var y = Servo.getPosition("v27");
            var z = Servo.getPosition("v1");

            // Generate a unique name for the new entry
            var newName = "Name" + (items.length + 1);

            // Add the new entry to the array
            items.push({ name: newName, x: x, y: y, z: z });

            // Save the updated array back to the global variable
            setVar("$Items_Array", items);

            // Print the new entry for debugging
            print("Added new entry: " + newName + " - x=" + x + ", y=" + y + ", z=" + z);

            // Reset the Add_Locations variable
            setVar("$Add_Locations", false);
        } else {
            print("Error: The global variable $Items_Array is not an array.");
        }
    }
}
  1. Trigger Addition Script: Ensure that the script to continuously check for the Add_Locations variable uses the correct function.
// Continuously check for the Add_Locations variable and add new entries
while (true) {
    addToArray();
    sleep(1000); // Check every second
}
  1. Print Array Function: Ensure that the function to print the entire array uses the correct global variable name.
// Function to print the entire array
function printArray() {
    // Retrieve the array from the global variable
    var items = getVar("$Items_Array");

    // Check if the array is not empty
    if (Array.isArray(items) && items.length > 0) {
        print("Printing the entire array:");
        // Iterate through the array and print each entry
        for (var i = 0; i < items.length; i++) {
            var entry = items[i];
            print("Name: " + entry.name + ", x: " + entry.x + ", y: " + entry.y + ", z: " + entry.z);
        }
    } else {
        print("The array is empty or not defined.");
    }
}
  1. Script to Call the Print Function: Use this script to call the printArray function whenever you want to print the entire array.
// Call the printArray function to print the entire array
printArray();

Summary

  • Ensure that the global variable name is consistent across all scripts ($Items_Array).
  • Check if the retrieved variable is an array using Array.isArray(items).
  • Use the correct global variable name in all functions and scripts.

This approach should resolve the issue and ensure that the array is correctly referenced and manipulated. If you have any further questions or need additional assistance, feel free to ask!


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

@Athena Yes that was the problem you told me to // Save the updated array back to the global variable setVar("$ItemsArray", items); but mine was setVarObject when I originally saved the array to global variable. All fixed now. I actually had to totally backtrack on arrays this morning because ARC unexpectedly shut down yesterday and lost some of the scripts that I had not saved yet. Is there a way to automatically save work every 15 minutes and also when you close laptop that it automatically saves it?

PRO
Canada
#148  

I'm glad to hear that the array issue is resolved! Regarding your request for automatic saving, ARC does not currently have a built-in feature for auto-saving projects at regular intervals or when closing the laptop. However, you can implement a workaround using scripting to periodically save your project.

Implementing Auto-Save in ARC

You can create a script that saves your project at regular intervals. Here's how you can do it:

  1. Create an Auto-Save Script:

    // Function to save the current project
    function autoSaveProject() {
        // Specify the path and filename for the project
        var projectPath = "C:\\Path\\To\\Your\\Project.EZB";
    
        // Save the project
        Utility.saveProject(projectPath);
    
        // Print a message to the console
        print("Project auto-saved at " + new Date().toLocaleTimeString());
    }
    
    // Continuously save the project every 15 minutes (900,000 milliseconds)
    while (true) {
        autoSaveProject();
        sleep(900000); // 15 minutes
    }
    
  2. Run the Auto-Save Script:

    • Add the script to your project and run it. This script will save your project every 15 minutes.

Handling Laptop Closure

Unfortunately, handling laptop closure events to trigger a save is more complex and typically requires operating system-level hooks, which are not directly supported in ARC. However, you can manually ensure that your work is saved before closing your laptop.

Additional Tips

  • Frequent manual Saves: Make it a habit to save your project frequently, especially after making significant changes.
  • Backup Copies: Periodically create backup copies of your project file to avoid data loss.

Summary

While ARC does not have built-in auto-save functionality, you can create a script to save your project at regular intervals. Handling laptop closure events is more complex and not directly supported, so frequent manual saves are recommended.

If you have any further questions or need additional assistance, feel free to ask!


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

@Athena, Have my arrays working nicely now and want to see if there is a way that I can print all of the locations in a script, and then directly change them as I want, if needed have the script interpret my changes and update the changes in the array with slice, push and pop. Maybe there is an easy way to make changes but I don't know of it currently.

PRO
Canada
#152  

Great to hear that your arrays are working well! You can certainly print all the locations in a script and then make changes to them using JavaScript methods like slice(), push(), and pop(). Here's a step-by-step guide on how to achieve this:

Step 1: Print All Locations

First, let's create a script to print all the locations stored in your multidimensional array.

// Function to print all locations
function printLocations() {
    // Retrieve the global array
    var items = getVar($ItemsArray, []);

    // Loop through the array and print each location
    for (var i = 0; i < items.length; i++) {
        var name = items[i][0];
        var x = items[i][1];
        var y = items[i][2];
        var z = items[i][3];
        print(Name: + name + , X: + x + , Y: + y + , Z: + z);
    }
}

// Call the function to print locations
printLocations();

Step 2: Update Locations

Next, let's create a function to update a specific location in the array. We'll use slice(), push(), and pop() to manipulate the array.

// Function to update a location
function updateLocation(name, newX, newY, newZ) {
    // Retrieve the global array
    var items = getVar($ItemsArray, []);

    // Find the index of the location to update
    var index = -1;
    for (var i = 0; i < items.length; i++) {
        if (items[i][0] === name) {
            index = i;
            break;
        }
    }

    // If the location is found, update it
    if (index !== -1) {
        items[index] = [name, newX, newY, newZ];
    } else {
        print(Location not found: + name);
    }

    // Save the updated array back to the global variable
    setVar($ItemsArray, items);
}

// Example usage: Update the location for a specific name
updateLocation(Name1, 10, 20, 30);

Step 3: Add and Remove Locations

You can also add new locations or remove existing ones using push() and pop().

// Function to add a new location
function addLocation(name, x, y, z) {
    // Retrieve the global array
    var items = getVar($ItemsArray, []);

    // Add the new location
    items.push([name, x, y, z]);

    // Save the updated array back to the global variable
    setVar($ItemsArray, items);
}

// Function to remove the last location
function removeLastLocation() {
    // Retrieve the global array
    var items = getVar($ItemsArray, []);

    // Remove the last location
    items.pop();

    // Save the updated array back to the global variable
    setVar($ItemsArray, items);
}

// Example usage: Add a new location
addLocation(Name21, 40, 50, 60);

// Example usage: Remove the last location
removeLastLocation();

Summary

  • Print Locations: Use a loop to print all locations in the array.
  • Update Locations: Use a function to find and update a specific location.
  • Add and Remove Locations: Use push() to add and pop() to remove locations.

By using these functions, you can easily manage and update your multidimensional array. If you have any further questions or need additional assistance, feel free to ask!


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.