# Studio error or wrong code

**URL:** <https://community.br-automation.com/t/studio-error-or-wrong-code/2328>\
**Category:** B&R Software & Runtime\
**Created:** [February 23, 2024, 8:25am UTC](https://community.br-automation.com/t/studio-error-or-wrong-code/2328 "2024-02-23T08:25:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![limhyeonsoo](https://avatars.discourse-cdn.com/v4/letter/l/FF8800/32.png) [@limhyeonsoo](https://community.br-automation.com/u/limhyeonsoo)\
**Post date:** [February 23, 2024, 8:25am UTC](https://community.br-automation.com/t/studio-error-or-wrong-code/2328/1 "2024-02-23T08:25:27Z")

</div>

[https://youtu.be/A6LPeWhtTjI](https://youtu.be/A6LPeWhtTjI)  
What you see in the back is a keyboard input test window  
And I’m debugging the elevator  
If you press the button on the building, the studio will move at will  
I pressed the button, but I wonder why it’s going offline  
Am I doing this because I wrote the wrong code?  
Previously, however, the behavior was different when debugging and monitoring  
I couldn’t film it, but if I were to tell you the situation  
When you turn on the monitor, press the building up-down button to go up to the second floor, and when debugging, it goes up to the desired floor  
Do you have any idea what kind of error it is…

---

<div class="post-metadata">

**Author:** ![kovarj](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/kovarj/32/537_2.png) [@kovarj](https://community.br-automation.com/u/kovarj)\
**Post date:** [February 23, 2024, 9:10am UTC](https://community.br-automation.com/t/studio-error-or-wrong-code/2328/2 "2024-02-23T09:10:48Z")

</div>

Hi @limhyeonsoo, I watched your video and I’m not sure how others see that. But I have no idea what is your problem. Can you maybe describe it a little bit better?

---

<div class="post-metadata">

**Author:** ![marcusbnr](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/marcusbnr/32/256_2.png) [@marcusbnr](https://community.br-automation.com/u/marcusbnr)\
**Post date:** [February 23, 2024, 7:23pm UTC](https://community.br-automation.com/t/studio-error-or-wrong-code/2328/3 "2024-02-23T19:23:58Z")

</div>

I’m also not entirely sure what you’re asking. However I do know that Monitor Mode can sometimes have weird behavior. If you’re concerned about your Watch window not updating properly, try this:

1. _Disable_ Monitor Mode using the magnifying glass in the toolbar
2. Close all Automation Studio tabs (Window → Close All)  
2a. Don’t save any of the Watch window configurations
3. Go to Project → Clean Configuration  
3a. Check all 4 boxes in the popup that appears  
3b. This will delete all of your saved Watch + Trace window settings, as well as delete all project binaries
4. Perform a Rebuild and transfer your project to ARSim

If the problem persists, then it’s related to your code and something is resetting the values you’re entering. Otherwise, it may have been some sort of glitch with the Watch window. If that ever happens, you can usually fix it by closing and re-opening your Watch window (and exiting Monitor Mode) but a rebuild will ensure that everything is cleaned out fully.

---

<div class="post-metadata">

**Author:** ![limhyeonsoo](https://avatars.discourse-cdn.com/v4/letter/l/FF8800/32.png) [@limhyeonsoo](https://community.br-automation.com/u/limhyeonsoo)\
**Post date:** [February 25, 2024, 1:17am UTC](https://community.br-automation.com/t/studio-error-or-wrong-code/2328/4 "2024-02-25T01:17:26Z")

</div>

It’s been resolved because someone I know helped  
Thank you for your answer

---

<div class="post-metadata">

**Author:** ![limhyeonsoo](https://avatars.discourse-cdn.com/v4/letter/l/FF8800/32.png) [@limhyeonsoo](https://community.br-automation.com/u/limhyeonsoo)\
**Post date:** [February 25, 2024, 1:51am UTC](https://community.br-automation.com/t/studio-error-or-wrong-code/2328/5 "2024-02-25T01:51:04Z")

</div>

It’s been resolved because someone I know helped  
Thank you for your reply

---

<div class="post-metadata">

**Author:** ![kovarj](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/kovarj/32/537_2.png) [@kovarj](https://community.br-automation.com/u/kovarj)\
**Post date:** [February 25, 2024, 6:02pm UTC](https://community.br-automation.com/t/studio-error-or-wrong-code/2328/6 "2024-02-25T18:02:37Z")

</div>

Ok can you share with is what was the solution? if it was problem with AS Watch or something similar, then it would be worth it for others. Please mark it as a solution. 🙂

---

<div class="post-metadata">

**Author:** ![limhyeonsoo](https://avatars.discourse-cdn.com/v4/letter/l/FF8800/32.png) [@limhyeonsoo](https://community.br-automation.com/u/limhyeonsoo)\
**Post date:** [February 26, 2024, 5:26am UTC](https://community.br-automation.com/t/studio-error-or-wrong-code/2328/7 "2024-02-26T05:26:02Z")

</div>

I set ‘i’ as a global variable and made the operation of pressing the number of floors into a for loop. But it’s a loop where you mark 1 to 6 and add 1 to see if there are pressed buttons on each floor  
However, the for loop was set incorrectly here  
If you write it like a picture and the for loop ends, 7 is derived, and 7 is an error that occurred because I did not recognize it in the code I wrote  
 ![KakaoTalk_20240226_141950891](https://europe1.discourse-cdn.com/brcommunity/original/2X/f/f22643c8370ee236ab814ed42a6156098c568737.png)

1. When setting a variable such as i, designate it as a local variable
2. When using the array function, it would be better to force the range of values using the for loop to limit the range of i

---

<div class="post-metadata">

**Author:** ![marcusbnr](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/marcusbnr/32/256_2.png) [@marcusbnr](https://community.br-automation.com/u/marcusbnr)\
**Post date:** [February 26, 2024, 1:14pm UTC](https://community.br-automation.com/t/studio-error-or-wrong-code/2328/8 "2024-02-26T13:14:06Z")

</div>

I’m glad you figured it out! In case anyone else stumbles upon this thread, I wanted to give one more piece of advice to add onto your notes.

You can define the size of an array using a constant variable like so:

 ![image](https://europe1.discourse-cdn.com/brcommunity/original/2X/3/33011ed5e90fd4ecb896836b9166cc626097f841.png)

This allows you to use the same constant in your code to limit the range of any FOR loops:  
 ![image](https://europe1.discourse-cdn.com/brcommunity/original/2X/4/4d42d48ab3cfe58dbebcf8cd76afbc4cf3bec6be.png)

Then, if you ever need to change the size of your array, all you have to change is the value of this constant. The array declaration and your code will automatically adapt.

---

<div class="post-metadata">

**Author:** ![limhyeonsoo](https://avatars.discourse-cdn.com/v4/letter/l/FF8800/32.png) [@limhyeonsoo](https://community.br-automation.com/u/limhyeonsoo)\
**Post date:** [February 27, 2024, 6:58am UTC](https://community.br-automation.com/t/studio-error-or-wrong-code/2328/9 "2024-02-27T06:58:34Z")

</div>

Thx for your tip ^^ 😄

---

<div class="post-metadata">

**Author:** ![alexander.hefner](https://dub1.discourse-cdn.com/brcommunity/user_avatar/community.br-automation.com/alexander.hefner/32/111_2.png) [@alexander.hefner](https://community.br-automation.com/u/alexander.hefner)\
**Post date:** [February 27, 2024, 7:22am UTC](https://community.br-automation.com/t/studio-error-or-wrong-code/2328/10 "2024-02-27T07:22:41Z")

</div>

Hi all,

great, that you found a solution! 👏

Another tip to loop safely through arrays is calculating the array size in the code, for example if you can’t use a constant (for whatever reason).

You can do this calculation by determine the size of the whole array, and divide it by the size of one array element → the result is the number of array elements.

```iecst
// If you're starting at array index 0, don't forget to subtract 1 at the loop end index after the calculation of the number of array elements
for i := 0 TO SIZEOF(MyArray)/SIZEOF(MyArray[0]) -1 DO
   MyArray[i] := i + 1;
END_FOR

```
