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

Stay on the cutting edge of robotics with ARC Pro, guaranteeing that your robot is always ahead of the game.

PRO
Canada
#177  

Step 1: Define the Array with Additional Properties

// Define the array
var items = [
    { name: "Name1", shape: "circle", color: "red", x: 10, y: 20, z: 30, gripperUp: true, gripperRotate: 45, fingersOpen: true },
    { name: "Name2", shape: "square", color: "blue", x: 40, y: 50, z: 60, gripperUp: false, gripperRotate: 90, fingersOpen: false },
    { name: "Name3", shape: "triangle", color: "green", x: 70, y: 80, z: 90, gripperUp: true, gripperRotate: 135, fingersOpen: true },
    { name: "Name4", shape: "circle", color: "blue", x: 100, y: 110, z: 120, gripperUp: false, gripperRotate: 180, fingersOpen: false },
    { name: "Name5", shape: "square", color: "red", x: 130, y: 140, z: 150, gripperUp: true, gripperRotate: 225, fingersOpen: true },
    { name: "Name6", shape: "triangle", color: "blue", x: 160, y: 170, z: 180, gripperUp: false, gripperRotate: 270, fingersOpen: false },
    { name: "Name7", shape: "square", color: "blue", x: 190, y: 200, z: 210, gripperUp: true, gripperRotate: 315, fingersOpen: true },
    { name: "Name8", shape: "circle", color: "green", x: 220, y: 230, z: 240, gripperUp: false, gripperRotate: 360, fingersOpen: false }
];

Step 2: Create a Function to Filter and Find Items by Any Property

// Function to filter and find items by any property
function findItemsByProperty(property, value) {
    return items.filter(function(item) {
        return item[property] == value;
    });
}

Step 3: Create a Function to Display and Choose Items if Multiple Matches Are Found

// Function to display and choose items if multiple matches are found
function displayAndChooseItems(items) {
    if (items.length == 0) {
        print("No items found.");
        return null;
    } else if (items.length == 1) {
        return items[0];
    } else {
        print("Multiple items found:");
        for (var i = 0; i < items.length; i++) {
            print((i + 1) + ": " + items[i].name + ", Shape: " + items[i].shape + ", Color: " + items[i].color + ", X: " + items[i].x + ", Y: " + items[i].y + ", Z: " + items[i].z);
        }

        // Use  ControlCommand to get user input
        ControlCommand("Script Manager", "ShowInputBox", "Enter the number of the item you want to choose:", "UserInput");

        // Wait for user input
        while (!getVar("$UserInput")) {
            sleep(100);
        }

        var choice = parseInt(getVar("$UserInput"));
        return items[choice - 1];
    }
}

Step 4: Use the Functions to Find and Choose an Item

// Example usage
var propertyToSearch = "shape";
var valueToSearch = "square";

var foundItems = findItemsByProperty(propertyToSearch, valueToSearch);
var chosenItem = displayAndChooseItems(foundItems);

if (chosenItem !== null) {
    print("You chose: " + chosenItem.name + ", Shape: " + chosenItem.shape + ", Color: " + chosenItem.color + ", X: " + chosenItem.x + ", Y: " + chosenItem.y + ", Z: " + chosenItem.z);
}

Summary

  • Define the Array: Create an array with multiple elements, each having additional properties.
  • Create a Function to Filter and Find Items: Use the filter method to find items by any property.
  • Create a Function to Display and Choose Items: Display multiple matches and let the user choose one using ControlCommand for user input.
  • Use the Functions: Find and choose an item based on the specified property and value.

Now the code is properly formatted and should work for copying and pasting directly into ARC. Let me know if you need any more help!


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

@Athena Got it working but not sure the the ControlCommand("Script Manager", "ShowInputBox", "Enter the number of the item you want to choose:", "UserInput"). is correct. What is the proper way to print a statement and get the user to type in a number, then put that number in a variable?

PRO
Canada
#179  

Step 1: Define the Array with Additional Properties

// Define the array
var items = [
    { name: "Name1", shape: "circle", color: "red", x: 10, y: 20, z: 30, gripperUp: true, gripperRotate: 45, fingersOpen: true },
    { name: "Name2", shape: "square", color: "blue", x: 40, y: 50, z: 60, gripperUp: false, gripperRotate: 90, fingersOpen: false },
    { name: "Name3", shape: "triangle", color: "green", x: 70, y: 80, z: 90, gripperUp: true, gripperRotate: 135, fingersOpen: true },
    { name: "Name4", shape: "circle", color: "blue", x: 100, y: 110, z: 120, gripperUp: false, gripperRotate: 180, fingersOpen: false },
    { name: "Name5", shape: "square", color: "red", x: 130, y: 140, z: 150, gripperUp: true, gripperRotate: 225, fingersOpen: true },
    { name: "Name6", shape: "triangle", color: "blue", x: 160, y: 170, z: 180, gripperUp: false, gripperRotate: 270, fingersOpen: false },
    { name: "Name7", shape: "square", color: "blue", x: 190, y: 200, z: 210, gripperUp: true, gripperRotate: 315, fingersOpen: true },
    { name: "Name8", shape: "circle", color: "green", x: 220, y: 230, z: 240, gripperUp: false, gripperRotate: 360, fingersOpen: false }
];

Step 2: Create a Function to Filter and Find Items by Any Property

// Function to filter and find items by any property
function findItemsByProperty(property, value) {
    return items.filter(function(item) {
        return item[property] == value;
    });
}

Step 3: Create a Function to Display and Choose Items if Multiple Matches Are Found

// Function to display and choose items if multiple matches are found
function displayAndChooseItems(items) {
    if (items.length == 0) {
        print("No items found.");
        return null;
    } else if (items.length == 1) {
        return items[0];
    } else {
        print("Multiple items found:");
        for (var i = 0; i < items.length; i++) {
            print((i + 1) + ": " + items[i].name + ", Shape: " + items[i].shape + ", Color: " + items[i].color + ", X: " + items[i].x + ", Y: " + items[i].y + ", Z: " + items[i].z);
        }

        // Use  ControlCommand to get user input
        ControlCommand("Script Manager", "ShowInputBox", "Enter the number of the item you want to choose:", "UserInput");

        // Wait for user input
        while (!getVar("$UserInput")) {
            sleep(100);
        }

        var choice = parseInt(getVar("$UserInput"));
        return items[choice - 1];
    }
}

Step 4: Use the Functions to Find and Choose an Item

// Example usage
var propertyToSearch = "shape";
var valueToSearch = "square";

var foundItems = findItemsByProperty(propertyToSearch, valueToSearch);
var chosenItem = displayAndChooseItems(foundItems);

if (chosenItem !== null) {
    print("You chose: " + chosenItem.name + ", Shape: " + chosenItem.shape + ", Color: " + chosenItem.color + ", X: " + chosenItem.x + ", Y: " + chosenItem.y + ", Z: " + chosenItem.z);
}

Summary

  • Define the Array: Create an array with multiple elements, each having additional properties.
  • Create a Function to Filter and Find Items: Use the filter method to find items by any property.
  • Create a Function to Display and Choose Items: Display multiple matches and let the user choose one using ControlCommand for user input.
  • Use the Functions: Find and choose an item based on the specified property and value.

This should now work smoothly with the proper formatting and user input using ControlCommand. Let me know if you need any further clarification!


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

@Athena Have things working nicely. Wondering how you could incorporate the ControlCommand inside of an array. Can you just write the whole specific ControlCommand in the array or do you use a trigger to specify which one you want. Can you give me some examples of this being used with ARC software.

PRO
Canada
#181  

Great to hear that things are working well! You can indeed incorporate ControlCommand inside an array in ARC. You can either store the entire ControlCommand as a string in the array or use a trigger to specify which command you want to execute. Here are some examples to illustrate both approaches:

Example 1: Storing ControlCommand as Strings in an Array

In this example, we'll store the ControlCommand as strings in an array and then execute them based on a condition.

// Define the array with ControlCommands as strings
var commands = [
    "ControlCommand(SMTP Client, Send, [email protected], Mr Recipient, This is the subject, This is a test message)",
    "ControlCommand(Omron HVC-P, Omron_Detect_All)",
    "ControlCommand(Servo Recorder, Play, No Name, 1)"
];

// Function to execute a command from the array
function executeCommand(index) {
    if (index >= 0 && index < commands.length) {
        eval(commands[index]);
    } else {
        print("Invalid command index");
    }
}

// Example usage
var commandIndex = 1; // Change this to the desired command index
executeCommand(commandIndex);

Example 2: Using a Trigger to Specify Which Command to Execute

In this example, we'll use a trigger to specify which command to execute from the array.

// Define the array with ControlCommands as strings
var commands = [
    { trigger: "sendEmail", command: "ControlCommand(SMTP Client, Send, [email protected], Mr Recipient, This is the subject, This is a test message)" },
    { trigger: "detectAll", command: "ControlCommand(Omron HVC-P, Omron_Detect_All)" },
    { trigger: "playServo", command: "ControlCommand(Servo Recorder, Play, No Name, 1)" }
];

// Function to execute a command based on a trigger
function executeCommandByTrigger(trigger) {
    for (var i = 0; i < commands.length; i++) {
        if (commands[i].trigger === trigger) {
            eval(commands[i].command);
            return;
        }
    }
    print("Invalid trigger");
}

// Example usage
var trigger = "detectAll"; // Change this to the desired trigger
executeCommandByTrigger(trigger);

Example 3: Using ControlCommand Directly in an Array

In this example, we'll store the ControlCommand directly in the array and execute them.

// Define the array with ControlCommands as functions
var commands = [
    function() { ControlCommand(SMTP Client, Send, [email protected], Mr Recipient, This is the subject, This is a test message); },
    function() { ControlCommand(Omron HVC-P, Omron_Detect_All); },
    function() { ControlCommand(Servo Recorder, Play, No Name, 1); }
];

// Function to execute a command from the array
function executeCommand(index) {
    if (index >= 0 && index < commands.length) {
        commands[index]();
    } else {
        print("Invalid command index");
    }
}

// Example usage
var commandIndex = 2; // Change this to the desired command index
executeCommand(commandIndex);

Summary

  • Example 1: Stores ControlCommand as strings in an array and uses eval to execute them.
  • Example 2: Uses a trigger to specify which command to execute from the array.
  • Example 3: Stores ControlCommand directly in the array as functions and executes them.

These examples should help you incorporate ControlCommand inside an array in ARC. Let me know if you need any further assistance!


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.