# Mode switching in XOD

**URL:** <https://forum.xod.io/t/mode-switching-in-xod/5176>\
**Category:** XOD Programming Questions\
**Created:** [May 12, 2021, 11:19am UTC](https://forum.xod.io/t/mode-switching-in-xod/5176 "2021-05-12T11:19:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jappi](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@jappi](https://forum.xod.io/u/jappi)\
**Post date:** [May 12, 2021, 11:19am UTC](https://forum.xod.io/t/mode-switching-in-xod/5176/1 "2021-05-12T11:19:08Z")

</div>

Hello,

I would like to ask how to do the switching of three modes with one button.

Thanks.

---

<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:** [May 12, 2021, 3:28pm UTC](https://forum.xod.io/t/mode-switching-in-xod/5176/2 "2021-05-12T15:28:43Z")

</div>

There are many options for this. The best one might depend on the rest of your code; how do you need to signal to your code what the mode is? Probably one of the easiest solutions that is also expandable is to use a count node to keep track of which mode you are in.  
 ![image](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/4/49b25baa1a89adc83a9fb1718ebf41a1df7d375a.png)  
This uses a tweak-pulse to simulate a button. ‘count’ node initializes and resets to zero, so your “modes” are 0, 1, & 2. The greater-or-equal and defer nodes are to reset to mode 0 when you leave mode 2. The output of the count node indicates your mode.

What to do with the count depends on the rest of your program. Is mode just a speed that requires a different number to feed some other part of the code? nth-input can be used to select the appropriate value. Do you need 3 true/false signals with only the “active” mode being true? You can use 3 ‘equal’ nodes to compare count output to 0, 1, & 2. The same can also be used to send a pulse to activate a specific mode.

You could also use flip-flops to remember which mode you are in, but you will need to use ‘and’ or ‘gate’ nodes to determine which nodes should be set/reset with each button press, you also need a way to initialize the 1st mode on boot, so it ends up quite a bit more complicated.

 ![image](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/2X/9/9eb4425a67298fd7a62287b3111e56e636bfaaef.png)  
One possible advantage of this method: you can change how you cycle through modes. For example, you might code the gates to go from mode A to B to C to B to A and repeat (like a transmission shifting gears) rather than just going A B C, then repeat.

---

<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:** [June 11, 2021, 3:29pm UTC](https://forum.xod.io/t/mode-switching-in-xod/5176/3 "2021-06-11T15:29:15Z")

</div>

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