3

My program moves an ultrasonic sensor around 260 degrees and the values are recorded down in an array variable every 2 degrees it turns. It moves back and measure it 3 more times, storing each measurement in a different array variable (in total there are 4 array variables).

I want to get the average value from the four array variable thus I use the 'read at index' mode of the array operations. The program

It uses the loop index to get the index of the arrays. Then it takes the values and calculates the average before putting them in another variable array. However, the brick shows an error message right after the ultrasonic sensor is finished measuring.

I removed some parts of the program in order to find the cause of the error and I found that it might have something to do with the loop index. It might also have been because the array is not found. I tried using another variable that adds one every loop and then uses it to get the index but the message would appear.

I am not sure why there is the error, nothing contradicts the program, there is no math error, it should have worked. I hope you guys can tell me why this happens and how to solve it. Thank you!

Here is the complete program: enter image description here Clarifications It loops 130 times as the ultrasonic sensor measures every 2 degrees it turns, so if it turns 260 degrees, there will be 130 measurements, 130 values in the arrays.

11
  • FYI: You don't need to "remove" parts of the program when you can hold CTRL and left-click certain groups of code, then you can press the "Run Selected" button, which looks like a play button with parentheses. It will do the same thing as removing certain parts of the program: run the selected parts. Commented May 10, 2020 at 10:51
  • Also, I see another code block hiding on the bottom of the first image, does it serve any purpose in your code? Commented May 10, 2020 at 11:16
  • 1
    Could it be that you are trying to read the #0 or the #261 element in the array? That would definitely lead to such errors.
    – zovits
    Commented May 11, 2020 at 7:47
  • 1
    @linzh You might be attempting to read a variable that doesn't exist. For example, You can use the advanced math block and attempt to add a+h. H does not exist. FYI, only A-D exist since they are the motor ports (i think). You can't write a sensor name into the block, however you can write a motor port letter there. Commented May 11, 2020 at 12:47
  • 1
    Yeah both @Zovitis and mindstormsboi is right, just figured out the problem and have posted a answer to it. Thank both you so much!
    – lin zh
    Commented May 11, 2020 at 13:07

1 Answer 1

2

After testing and researching for a while, I found out that the first lines of code that controls the moving of the ultrasonic sensor is the cause of the error. Previously, the code will make the motor move 2 degrees, then get the measurement of the ultrasonic sensor and put it inside the numerical array. It then loops 130 times so there is 130 measurements and 260 degrees of rotation. enter image description here What I am doing is to replace the entire array with that one value. Thus, when I get the value of the array, I am getting an non-existent value. Also, the array starts from zero, not one, so using the loop index for the array will not work as the loop index will not display a zero value index.

Instead, I should use the 'write at index' mode of array operations. This way, it will add values to the array, not replace the entire array with that one value. enter image description here Now this is correct. Before that, I thought that the loop index was the cause of error.

First time answering my own question, Im so proud lol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.