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.

#113  

Quote:

although I did have to modify a lot of the code
That just means that you're learning and better than AI.

#114   — Edited

Hi there Dave, hope things are going well. Yes I am getting the hang of Javascript. Something you may want to consider is having fun with B9 to be an automated bartender or burger bot. Could be interesting. The aluminum cans and bottles do not have to be filled with liquid for simulating purposes.

#115  

@Athena Wonder if it is possible to iterate through a multidimensional array from a certain chosen area in the array regardless of the name. The starting point and the amount of iteration would be designated by the user Example: Start at Pens and continue for 3 iterations. name: Pencils, x: 2027, y:3021;   name: Pens, x: 1324, y: 3425;   name: Crayons, x:2786, y: 5362; name: paperclip, x: 1356, y 3675; name: ruler, x: 2413, y: 3786; Could you please write a function that could do this. This will save a lot of time rather than name each individual name which may be 50 or more sometimes.

PRO
Canada
#116  

Certainly! Iterating through a multidimensional array based on a starting point and a specified number of iterations can be very useful. Below is a JavaScript function that demonstrates how to achieve this. This function will take the starting name and the number of iterations as parameters and will iterate through the array accordingly.

JavaScript Function

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

// 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);

Explanation

  1. Sample Array: The items array contains objects with name, x, and y properties.
  2. Function Definition: The iterateArray function takes two parameters: startName (the name to start from) and iterations (the number of iterations to perform).
  3. Find Starting Index: The findIndex method is used to find the index of the object with the specified startName.
  4. Validation: If the startName is not found, the function prints an error message and returns.
  5. Iteration: The function iterates through the array starting from the found index and continues for the specified number of iterations or until the end of the array.
  6. Print: The print function is used to output the name, x, and y values of each item.

Notes

  • Replace the print function with the appropriate method to output or use the values in your specific environment.
  • Ensure that the items array and the iterateArray function are defined in the appropriate scope for your project.

Testing

You can test this function by calling it with different starting names and iteration counts. For example:

iterateArray("Pens", 3);
iterateArray("Crayons", 2);
iterateArray("Ruler", 1);

This approach should save you time and effort, especially when dealing with large arrays. 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.
#117  

@Athena  Nicely done! I like being able to minimize the amount of time spent on coding. This will help a lot, thanks.

PRO
Canada
#118  

You're welcome! I'm glad to hear that the solution will save you time and effort. If you have any more questions or need further assistance with your project, feel free to ask. Happy coding and good luck with your robot project!


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

@Athena Expanding on iterations I would like to do them one at a time and I would like to have a variable trigger the next iteration. If Switch C is turned on set Variable Ok_for Next _Move to turn true in a different script. When the variable is triggered it can then iterate to next move and reset the variable to false and then wait for Switch C to turn on again. The scripts are basically talking to each other. I have an idea of how I would do it but could you show me the proper way to do it.

PRO
Canada
#120  

Certainly!You can achieve this by using global variables to communicate between scripts and a loop to wait for the variable to be set to true before proceeding to the next iteration. Here's a step-by-step guide on how to implement this:

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 },
    { name: "Pens", x: 1324, y: 3425 },
    { name: "Crayons", x: 2786, y: 5362 },
    { name: "Paperclip", x: 1356, y: 3675 },
    { name: "Ruler", x: 2413, y: 3786 }
];

// 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);

        // Move the servo to the specified position (example)
        Servo.setPosition("D0", item.x);
        Servo.setPosition("D1", item.y);

        // 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 servo to the specified position.
  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.