# Issues with shared resources in multiple states

**URL:** https://forum.xod.io/t/issues-with-shared-resources-in-multiple-states/301
**Category:** Uncategorized
**Created:** [December 26, 2017, 7:24pm UTC](https://forum.xod.io/t/issues-with-shared-resources-in-multiple-states/301 "2017-12-26T19:24:13Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![warner](https://avatars.discourse-cdn.com/v4/letter/w/0ea827/32.png) [@warner](https://forum.xod.io/u/warner)
#### Post date: [December 26, 2017, 7:24pm UTC](https://forum.xod.io/t/issues-with-shared-resources-in-multiple-states/301/1 "2017-12-26T19:24:13Z")

</div>

[I’ve run into an issue before trying to control one specific motor from multiple states](https://forum.xod.io/t/help-debugging-states-with-dc-motors/259) and I’m now having the same problem controlling a servo so I figured this same concept may apply to many different types of hardware…thus the new thread. For the motor I worked with nkrkv to make a new library and work some hacks to make it work and it did. For the servo I’m trying to do it myself using what I learned from the [creating nodes in c++ documentation](https://xod.io/docs/guide/nodes-for-xod-in-cpp/). I think I’m close but it is still a little buggy. The issue comes from having multiple states trying to control the same hardware and I’ve ended up with conflicting values and unpredictable results. My solution is to have each state attach the servo on “SET” and then detach the servo on “DONE”. I modified the standard servo hardware c++ code to have a pulse input for “ATTACH” and another for “DETACH”. I’ve uploaded a picture showing the stock servo code on the left and my modified version on the right.

 ![modifiedServo](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/1X/b765cd7b17996eae9febb35bdec01f257def952f.jpg)

This seems to work well for a program that has no feedback loops. The issue is when I try to implement a feedback loop using the “defer-pulse” node the program only runs one time and never repeats. In the 2nd picture the left main function works properly but the right one never repeats. The rightmost picture shows the structure of my “lookleft”, “lookright”, and “lookstraight” patches with the only difference being the number bound to the gate.

 ![mainServo](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/1X/a718a8485f3f0ffba30b7d978a16c038def61f15.jpg)

I’ve also attached the .xodball file.

[sharedResourceServo.xodball](https://forum.xod.io/uploads/xod/original/1X/d6d68726e60fa59c34b2fdf187357ce782fdf436.xodball) (52.3 KB)

Please help me to understand why this program is not repeating and how to fix it.

---

<div class="post-metadata">

### Author: ![nkrkv](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/nkrkv/32/8_2.png) [@nkrkv](https://forum.xod.io/u/nkrkv)
#### Post date: [December 29, 2017, 9:38am UTC](https://forum.xod.io/t/issues-with-shared-resources-in-multiple-states/301/2 "2017-12-29T09:38:56Z")

</div>

Hello, sorry for the reply delay. I’m pretty sure the problem is in code:

```cpp
    if (isInputDirty<input_ATTACH>(ctx)) {
        if (port != state->configuredPort) {
        	state->servo.attach(port);
        	state->configuredPort = port;
        }
    }

```

It gives no chance to call `servo.attach` for the second time, when the second iteration begins. The inner `if` will always return `false` after the first call. I suggest dropping `configuredPort` check, and in your modification just look at `ATTACH` and `DETACH` signals. Try to remove `configuredPort` completely.

BTW, on the bottom right patch, I think the `flip-flop` is not required. You could link `delay.ACT` to `gate-number.EN` directly to achieve the equivalent result.

---

<div class="post-metadata">

### Author: ![warner](https://avatars.discourse-cdn.com/v4/letter/w/0ea827/32.png) [@warner](https://forum.xod.io/u/warner)
#### Post date: [December 30, 2017, 1:35am UTC](https://forum.xod.io/t/issues-with-shared-resources-in-multiple-states/301/3 "2017-12-30T01:35:30Z")

</div>

Perfect. Works great now. Here’s the new code.

```
State* state = getState(ctx);

auto port = (int)getValue<input_PORT>(ctx);

if (isInputDirty<input_ATTACH>(ctx)) {
    state->servo.attach(port);
    state->configuredPort = port;
}

state->servo.write(getValue<input_VAL>(ctx) * 180);

if (isInputDirty<input_DETACH>(ctx)) {
   	state->servo.detach();
}
```
