# Problems with joystick/potentiometer analog ports

**URL:** <https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835>\
**Category:** XOD Programming Questions\
**Created:** [April 22, 2020, 10:33pm UTC](https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835 "2020-04-22T22:33:37Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![cruntos](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/cruntos/32/2265_2.png) [@cruntos](https://forum.xod.io/u/cruntos)\
**Post date:** [April 22, 2020, 10:33pm UTC](https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835/1 "2020-04-22T22:33:37Z")

</div>

Greetings!

Trying to create a menu on the OLED display. But, I ran into a problem when changing the name of the ports (active/inactive). Data continues to be read despite the fact that the port is incorrect. The joystick is connected to ports A0 and A1.

To change the name of the ports, you must press the joystick / button. For the joystick, I use the library 315821 / joystick-x-y-button /

How can I solve the problem?

 ![screen1](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/d/db44710670f457d07cebba00d50d6bf59f54ca26.jpeg)

---

<div class="post-metadata">

**Author:** ![cesars](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@cesars](https://forum.xod.io/u/cesars)\
**Post date:** [April 22, 2020, 11:50pm UTC](https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835/2 "2020-04-22T23:50:43Z")

</div>

Try using `gate(pulse)` to control, instead of using if-else, and use `joystick-x-y-button-all`

![image](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/f/f631de2f4819f7b09395da628dfde9fb7475ddc8.png)

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [April 23, 2020, 12:17am UTC](https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835/3 "2020-04-23T00:17:26Z")

</div>

NOTE: Some nodes initialize on startup and won’t update port/etc. after that, so code like yours above would never switch ports. This is probably rare and is not the case with the joystick node (but you have to drill down to the C++ code in analog-read to know that for sure).

Changing to an “invalid” port isn’t going to keep data from being read. It is just going to read the data from a different port. If that port isn’t connected to anything, the value is going to float. Internal circuits might drain or charge the port; any static electricity might affect it, etc. and it can change dramatically between each read (or it might stay the same value—there is no way to know and it might work differently the next time you use it).

I think in XOD, the closest you can come to “no value” is to stop changing the value. That can be done as cesars showed above by only telling the joystick to update when it is active (the node you are using just assumes it should always update; the -all node exposes the update pins so you can control that). It can also be done by using gate or buffer nodes on the output. There are probably other options as well, but these are the most “conventional” methods in XOD. Which option is “better” might depend on your other code. If there was no option to use the UPD pins, then it obviously would not be the “right” answer. Other code might already be solving most of the problem for you using one solution, but not the other. In general, if not allowing update (like cesars’ solution) is an option, it is probably the better solution than adding gate or buffer nodes to the output; it generally means the Arduino will have less work to do since some of the code (in this case what is in the joystick node) doesn’t have to execute if UPD pin doesn’t pulse.

---

<div class="post-metadata">

**Author:** ![cesars](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@cesars](https://forum.xod.io/u/cesars)\
**Post date:** [April 23, 2020, 12:46am UTC](https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835/4 "2020-04-23T00:46:30Z")

</div>

In the case of using A15, it gives me the same error as if I were using D255 (pin not valid). I do not know why the error is not reflected there, perhaps it is another board.

Here is an example of using D255 and ignoring the error, sending a default value (0).

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/9/9f6a51c5c3ade2b989967a59817ad900a8a0e67c.png)

but this could well be done, with the data obtained, and send a default value without touching the port.

![image](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/c/c46e53433dc61ee156a64701f785efb18a69fa17.png)

and keeping the last value something like that

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/f/f962c22c7206a93e79a5ab2fa734ca28bc284dde.png)

---

<div class="post-metadata">

**Author:** ![cruntos](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/cruntos/32/2265_2.png) [@cruntos](https://forum.xod.io/u/cruntos)\
**Post date:** [April 23, 2020, 6:36am UTC](https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835/5 "2020-04-23T06:36:03Z")

</div>

Hi!

Thank you for your responses. I will definitely try your suggestions.

 ![screen3](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/5/5bdce7e9a38977e73a9e22f238910cff72137b6b.jpeg)  
Yesterday I tried to use the base nodes, but the problem persisted. I also tried to change the board, from MEGA 2560 to UNO, and this also did not solve the problem.

---

<div class="post-metadata">

**Author:** ![cruntos](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/cruntos/32/2265_2.png) [@cruntos](https://forum.xod.io/u/cruntos)\
**Post date:** [April 23, 2020, 7:41am UTC](https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835/6 "2020-04-23T07:41:30Z")

</div>

Perhaps the problem is also in the board. I tried UNO again. When the port was blocked, the value did not rise above 0.68.

 ![screen4](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/3/37166462b26aa8bde9e868f3c32c98d38bfa6971.jpeg)  
What is also interesting, when using MEGA, when the port is blocked, only one direction works (horizontal/vertical) , but the calculation is carried out for two ports.

---

<div class="post-metadata">

**Author:** ![cesars](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@cesars](https://forum.xod.io/u/cesars)\
**Post date:** [April 23, 2020, 12:13pm UTC](https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835/7 "2020-04-23T12:13:13Z")

</div>

But there is using A5, A5 surely when not using this non-floating mode and will take a random value.  
As I said before recommend handling the data instead of ports.

---

<div class="post-metadata">

**Author:** ![cruntos](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/cruntos/32/2265_2.png) [@cruntos](https://forum.xod.io/u/cruntos)\
**Post date:** [April 23, 2020, 12:56pm UTC](https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835/8 "2020-04-23T12:56:40Z")

</div>

Thanks for your reply.

All I’m trying to do: start a menu - allow the joystick to move only vertically - press a button - go to a submenu - the joystick moves only horizontally - press a button again - save data and return to the main menu. And so my problem was that after pressing the button the vertically direction of the joystick is not blocked.

To move around the menu, I use the node count, and to determine the position by the equal node.

 ![screen5](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/8/8e8abff639733167043f169da9ae829b945e3672.jpeg) ![screen6](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/e/e56f6c9eb49802d3e47320c848129023df401d20.jpeg)  
Perhaps all this can be implemented easier, but I have just started using XOD and have not yet come up with a different option.

---

<div class="post-metadata">

**Author:** ![cesars](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@cesars](https://forum.xod.io/u/cesars)\
**Post date:** [April 23, 2020, 2:02pm UTC](https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835/9 "2020-04-23T14:02:02Z")

</div>

You can try something like this, the counter increases and decreases, if it rises or falls of a certain value.

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/c/c997d2c59bb9ac12d1ab6e20fdecd279024d279b.png)

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [April 23, 2020, 3:32pm UTC](https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835/10 "2020-04-23T15:32:43Z")

</div>

Rather than duplicating your menu display code for each display option, you might be able to use one copy of display and change what is being displayed. Since you already have X/Y counters to determine what to display, nth-input can probably be used:

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/6/6a370a4b0d893c8d36ce4130b61a58c0e472846f.png)

This assumes you are at the main menu when CNT\_X=0. If CNT\_X\>0, then you are on a sub-menu. If different sub-menus are different sizes, you can use another nth-input to set MAX\_X for each value of CNT\_Y and resize each nth-input appropriately.

---

<div class="post-metadata">

**Author:** ![cruntos](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/cruntos/32/2265_2.png) [@cruntos](https://forum.xod.io/u/cruntos)\
**Post date:** [April 23, 2020, 3:37pm UTC](https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835/11 "2020-04-23T15:37:16Z")

</div>

Hi!  
Thanks for your answer, I will definitely try this option.

---

<div class="post-metadata">

**Author:** ![cruntos](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/cruntos/32/2265_2.png) [@cruntos](https://forum.xod.io/u/cruntos)\
**Post date:** [April 23, 2020, 6:49pm UTC](https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835/12 "2020-04-23T18:49:54Z")

</div>

Thanks again for the help. Now everything works as needed.

 ![screen7](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/b/b7f8240a35595823db8cc5d3dbf0ad256265581f.jpeg)

---

<div class="post-metadata">

**Author:** ![cruntos](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/cruntos/32/2265_2.png) [@cruntos](https://forum.xod.io/u/cruntos)\
**Post date:** [April 23, 2020, 7:49pm UTC](https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835/13 "2020-04-23T19:49:35Z")

</div>

Started testing nth-input. I don’t know if it will work but it looks beautiful. 😀

 ![screen8](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/3/3bba95a096cf1f4339d99eb67462c03325e62fea.jpeg)

---

<div class="post-metadata">

**Author:** ![cesars](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@cesars](https://forum.xod.io/u/cesars)\
**Post date:** [April 23, 2020, 8:20pm UTC](https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835/14 "2020-04-23T20:20:27Z")

</div>

nth-input works very well, only if you use too much the consumption of resources is very large.

That is why I modified in my library so that it consumes less, the integration in the development generated some errors and abandoned the idea since I do not handle my github well for that.

The menu system has constants that you can edit to use in conjunction with nth-input. Those values ​​use [flash-string-1](https://xod.io/libs/bitrex/xod-menu-system-11/flash-string-1/), which is explained by @bitrex

ps: It can be somewhat complex, because each menu requires a flash-string.  
Then you have to clone it as many times as necessary. without modifying the library.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/1X/eb7348baa04192c2ef932d1db54d44f09f8a7888.png) [@system](https://forum.xod.io/u/system)\
**Post date:** [April 25, 2020, 8:20pm UTC](https://forum.xod.io/t/problems-with-joystick-potentiometer-analog-ports/3835/15 "2020-04-25T20:20:29Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
