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

ARC Pro will give you immediate updates and new features needed to unleash your robot's potential!

#121  

@Athena I think you may have forgotten in a previous request how we were starting the array from one particular name and the preceding to iterate its a certain amount. In this request I wanted it to iterate each time the variable turns true after an operation has completed. I want the script to combine the two requests. I could not get this request to work. Could you please write the whole array and write an accompanying separate script that I can use for the on off of the variable and which will move the servos each time it is iterated. Whenever I have a working script it is much easier to understand what is happening and I will digest it and remember it.

PRO
Canada
#122  

Absolutely!Let's combine the previous requests into a single,cohesive solution that iterates through the array one item at a time each time the variable is set to true and moves the servos accordingly.

Step 1: Define the Array and Initial Variables

First, define your array and initialize the necessary variables.

// Sample multidimensional array
var items = [
    { name: "Pencils", x: 2027, y: 3021, z: 1000 },
    { name: "Pens", x: 1324, y: 3425, z: 1100 },
    { name: "Crayons", x: 2786, y: 5362, z: 1200 },
    { name: "Paperclip", x: 1356, y: 3675, z: 1300 },
    { name: "Ruler", x: 2413, y: 3786, z: 1400 }
];

// Initialize global variables
setVar("$Ok_for_Next_Move", false);
setVar("$Current_Index", 0);

Step 2: Create the Iteration Function

Create a function that will iterate through the array one item at a time when the variable is set to true.

function iterateArray() {
    // Get the current index
    var currentIndex = getVar("$Current_Index");

    // Check if the current index is within the array bounds
    if (currentIndex < items.length) {
        var item = items[currentIndex];
        print("Name: " + item.name + ", X: " + item.x + ", Y: " + item.y + ", Z: " + item.z);

        // Move the servos to the specified positions (example)
        Servo.setPosition("D0", item.x);
        Servo.setPosition("D1", item.y);
        Servo.setPosition("D2", item.z);

        // Increment the index for the next iteration
        setVar("$Current_Index", currentIndex + 1);
    } else {
        print("All items have been iterated.");
    }
}

Step 3: Create the Main Loop

Create a main loop that will wait for the variable to be set to true before calling the iteration function.

while (true) {
    // Wait for the variable to be set to true
    if (getVar("$Ok_for_Next_Move")) {
        // Call the iteration function
        iterateArray();

        // Reset the variable to false
        setVar("$Ok_for_Next_Move", false);
    }

    // Sleep for a short duration to avoid busy-waiting
    sleep(100);
}

Step 4: Create the Trigger Script

Create a separate script that will set the variable to true when Switch C is turned on.

// Example script to be triggered by Switch C
function onSwitchC() {
    // Set the variable to true
    setVar("$Ok_for_Next_Move", true);
}

// Simulate Switch C being turned on
onSwitchC();

Explanation

  1. Array and Variables: The array and global variables are defined at the beginning.
  2. Iteration Function: The iterateArray function iterates through the array one item at a time and moves the servos to the specified positions.
  3. Main Loop: The main loop waits for the $Ok_for_Next_Move variable to be set to true before calling the iteration function. It then resets the variable to false and waits for the next trigger.
  4. Trigger Script: The onSwitchC function sets the $Ok_for_Next_Move variable to true when Switch C is turned on.

Testing

You can test this setup by running the main loop and then triggering the onSwitchC function to simulate Switch C being turned on.

// Start the main loop
mainLoop();

// Simulate Switch C being turned on
onSwitchC();

This approach ensures that the iteration happens one step at a time, controlled by the variable being set to true. If you have any further questions or need additional modifications, 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.
#123  

@Athena I think you forgot to add the function that has a start name and if I add it as a separate function it will probably conflict with you latest script. Here is the function that was supposed to be incorporated with latest script.  // Function to iterate through the array function iterateArray(startName, iterations) { // Find the starting index based on the name var startIndex = items.findIndex(item => item.name === startName);

// Check if the starting index is valid
if (startIndex === -1) {
    print("Start name not found in the array.");
    return;
}

// Iterate through the array from the starting index
for (var i = startIndex; i < startIndex + iterations && i < items.length; i++) {
    var item = items[i];
    print("Name: " + item.name + ", X: " + item.x + ", Y: " + item.y);
}

}

// Example usage iterateArray("Pens", 3);

PRO
Canada
#124  

Got it!Let's combine the two functionalities into a single script that iterates through the array starting from a specified name and continues for a given number of iterations, while also allowing the iteration to be triggered by a variable.

Step 1: Define the Array and Initial Variables

First, define your array and initialize the necessary variables.

// Sample multidimensional array
var items = [
    { name: "Pencils", x: 2027, y: 3021, z: 1000 },
    { name: "Pens", x: 1324, y: 3425, z: 1100 },
    { name: "Crayons", x: 2786, y: 5362, z: 1200 },
    { name: "Paperclip", x: 1356, y: 3675, z: 1300 },
    { name: "Ruler", x: 2413, y: 3786, z: 1400 }
];

// Initialize global variables
setVar("$Ok_for_Next_Move", false);
setVar("$Current_Index", 0);
setVar("$Iterations", 3); // Number of iterations
setVar("$Start_Name", "Pens"); // Starting name

Step 2: Create the Iteration Function

Create a function that will iterate through the array starting from a specified name and for a given number of iterations.

function iterateArray() {
    // Get the current index
    var currentIndex = getVar("$Current_Index");
    var iterations = getVar("$Iterations");
    var startName = getVar("$Start_Name");

    // Find the starting index based on the name
    var startIndex = items.findIndex(item => item.name === startName);

    // Check if the starting index is valid
    if (startIndex === -1) {
        print("Start name not found in the array.");
        return;
    }

    // Check if the current index is within the array bounds and within the iteration limit
    if (currentIndex < startIndex + iterations && currentIndex < items.length) {
        var item = items[currentIndex];
        print("Name: " + item.name + ", X: " + item.x + ", Y: " + item.y + ", Z: " + item.z);

        // Move the servos to the specified positions (example)
        Servo.setPosition("D0", item.x);
        Servo.setPosition("D1", item.y);
        Servo.setPosition("D2", item.z);

        // Increment the index for the next iteration
        setVar("$Current_Index", currentIndex + 1);
    } else {
        print("All items have been iterated.");
    }
}

Step 3: Create the Main Loop

Create a main loop that will wait for the variable to be set to true before calling the iteration function.

while (true) {
    // Wait for the variable to be set to true
    if (getVar("$Ok_for_Next_Move")) {
        // Call the iteration function
        iterateArray();

        // Reset the variable to false
        setVar("$Ok_for_Next_Move", false);
    }

    // Sleep for a short duration to avoid busy-waiting
    sleep(100);
}

Step 4: Create the Trigger Script

Create a separate script that will set the variable to true when Switch C is turned on.

// Example script to be triggered by Switch C
function onSwitchC() {
    // Set the variable to true
    setVar("$Ok_for_Next_Move", true);
}

// Simulate Switch C being turned on
onSwitchC();

Explanation

  1. Array and Variables: The array and global variables are defined at the beginning.
  2. Iteration Function: The iterateArray function iterates through the array starting from a specified name and for a given number of iterations, moving the servos to the specified positions.
  3. Main Loop: The main loop waits for the $Ok_for_Next_Move variable to be set to true before calling the iteration function. It then resets the variable to false and waits for the next trigger.
  4. Trigger Script: The onSwitchC function sets the $Ok_for_Next_Move variable to true when Switch C is turned on.

Testing

You can test this setup by running the main loop and then triggering the onSwitchC function to simulate Switch C being turned on.

// Start the main loop
mainLoop();

// Simulate Switch C being turned on
onSwitchC();

This approach ensures that the iteration happens one step at a time, controlled by the variable being set to true, and starts from a specified name for a given number of iterations. If you have any further questions or need additional modifications, 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.
#125   — Edited

@Athena Got it working and I understand it. Have a slight glitch- even though I set the variable Current_Index to 0 in a different program setVar("$Start_Name", "Pens");

setVar("$Iterations", 3);

setVar("$Current_Index", 0); and it also sets it to 0 in it's own script, it starts off on the index that was the remaining index from before. When I turn on the script it is supposed to be x=1324 but it's not. I need to be starting with the first name that is called out. To make this array more organized I'd like to have all of the locations added into the array in one script, then save it to a global array. Once it is in the global array I can call it up from this script which will separated from all the data. Can you give me an example of how to separate it. This is a small array now but in the future will want thousands of items in the array. It would be nice for it to be padded so that it is nice and easy to read like a spreadsheet. It would also be nice to have the script shut off whenever it completes it's last iteration and sends servo to their position. Can you please put that in there also.

#126  

@Athena Another feature that would be nice is when you are inputting the Start name in a different script set it up so that the script will show you all of the names that are available in the array so you don't have to open up the other script. To be clear there should be 3 different scripts. One script is for adding locations into array. One script that will ask you the name and give you names to choose from and ask how many iterations would you like. One script that will do the work of getting name, iterating and moving servos to their locations.

PRO
Canada
#127  

Absolutely!Let's break this down into three separate scripts as you requested:

  1. Script to Add Locations into the Array
  2. Script to Select Start Name and Number of Iterations
  3. Script to Iterate and Move Servos

Script 1: Add Locations into the Array

This script will define the array and save it to a global variable.

// Define the array
var items = [
    { name: "Pencils", x: 2027, y: 3021, z: 1000 },
    { name: "Pens", x: 1324, y: 3425, z: 1100 },
    { name: "Crayons", x: 2786, y: 5362, z: 1200 },
    { name: "Paperclip", x: 1356, y: 3675, z: 1300 },
    { name: "Ruler", x: 2413, y: 3786, z: 1400 }
];

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

print("Array has been saved to global variable $Items_Array.");

Script 2: Select Start Name and Number of Iterations

This script will allow you to select the start name and number of iterations, and it will display all available names.

// Retrieve the array from the global variable
var items = getVar("$Items_Array");

// Display all available names
print("Available names:");
for (var i = 0; i < items.length; i++) {
    print(items[i].name);
}

// Set the start name and number of iterations
var startName = "Pens"; // Example start name
var iterations = 3; // Example number of iterations

// Save the start name and iterations to global variables
setVar("$Start_Name", startName);
setVar("$Iterations", iterations);
setVar("$Current_Index", 0); // Reset the current index

print("Start name and iterations have been set.");

Script 3: Iterate and Move Servos

This script will iterate through the array and move the servos.

// Retrieve the array from the global variable
var items = getVar("$Items_Array");

// Function to iterate through the array
function iterateArray() {
    // Get the current index, start name, and iterations
    var currentIndex = getVar("$Current_Index");
    var iterations = getVar("$Iterations");
    var startName = getVar("$Start_Name");

    // Find the starting index based on the name
    var startIndex = items.findIndex(item => item.name === startName);

    // Check if the starting index is valid
    if (startIndex === -1) {
        print("Start name not found in the array.");
        return;
    }

    // Check if the current index is within the array bounds and within the iteration limit
    if (currentIndex < startIndex + iterations && currentIndex < items.length) {
        var item = items[currentIndex];
        print("Name: " + item.name + ", X: " + item.x + ", Y: " + item.y + ", Z: " + item.z);

        // Move the servos to the specified positions (example)
        Servo.setPosition("D0", item.x);
        Servo.setPosition("D1", item.y);
        Servo.setPosition("D2", item.z);

        // Increment the index for the next iteration
        setVar("$Current_Index", currentIndex + 1);
    } else {
        print("All items have been iterated.");
        // Stop the script
        return false;
    }
    return true;
}

// Main loop
while (true) {
    // Wait for the variable to be set to true
    if (getVar("$Ok_for_Next_Move")) {
        // Call the iteration function
        if (!iterateArray()) {
            // Stop the script if iteration is complete
            break;
        }
        // Reset the variable to false
        setVar("$Ok_for_Next_Move", false);
    }
    // Sleep for a short duration to avoid busy-waiting
    sleep(100);
}

Explanation

  1. Script 1: Add Locations into the Array

    • Defines the array and saves it to a global variable $Items_Array.
  2. Script 2: Select Start Name and Number of Iterations

    • Retrieves the array from the global variable.
    • Displays all available names.
    • Sets the start name and number of iterations, and saves them to global variables.
  3. Script 3: Iterate and Move Servos

    • Retrieves the array from the global variable.
    • Iterates through the array starting from the specified name and for the given number of iterations.
    • Moves the servos to the specified positions.
    • Stops the script when all items have been iterated.

Testing

  1. Run Script 1 to add locations into the array.
  2. Run Script 2 to select the start name and number of iterations.
  3. Run Script 3 to iterate through the array and move the servos.

This setup ensures that the array is organized and separated from the iteration logic, making it easier to manage and scale. If you have any further questions or need additional modifications, 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.
#128  

@Athena Yes I like it and understand it, thanks.