# Toggle image changes when button is pressed

**URL:** <https://forum.cabbageaudio.com/t/toggle-image-changes-when-button-is-pressed/4016>\
**Category:** Csound Noobs\
**Created:** [17 December 2023 04:10 UTC](https://forum.cabbageaudio.com/t/toggle-image-changes-when-button-is-pressed/4016 "2023-12-17T04:10:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cswen29](https://forum.cabbageaudio.com/letter_avatar_proxy/v4/letter/c/fbc32d/32.png) [@cswen29](https://forum.cabbageaudio.com/u/cswen29)\
**Post date:** [17 December 2023 04:10 UTC](https://forum.cabbageaudio.com/t/toggle-image-changes-when-button-is-pressed/4016/1 "2023-12-17T04:10:46Z")

</div>

Hello,

What is the best way to switch between 2 images when a(ny) button is pressed. Assuming the original png image “wifi3.png” is already on the widget, I want to cabbageSet “wifi”, “file(“wifi1.png”)” when kToggle == 1 or whatever button is pressed

aOutL = aOutL \* (kToggle == 1 ? kGating : 1)  
aOutR = aOutR \* (kToggle == 1 ? kGating : 1)

Thank you  
Wen

---

<div class="post-metadata">

**Author:** ![circuitdust](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/circuitdust/32/5283_2.png) [@circuitdust](https://forum.cabbageaudio.com/u/circuitdust)\
**Post date:** [17 December 2023 04:37 UTC](https://forum.cabbageaudio.com/t/toggle-image-changes-when-button-is-pressed/4016/2 "2023-12-17T04:37:06Z")

</div>

You can try toggling the visibility between two images when you meet a certain condition.

```
kUpdateOnMetronome metro 10 ; or however many Hz you want
if kToggle == 1 && kUpdateOnMetronome == 1 then
    cabbageSet 1, "image_of_wifi3", "visible", 1
    cabbageSet 1, "image_of_wifi1", "visible", 0
elseif kUpdateOnMetronome == 1 then
    cabbageSet 1, "image_of_wifi3", "visible", 0
    cabbageSet 1, "image_of_wifi1", "visible", 1
endif

```

Try to keep `cabbageSet` behind an if-statement or something that is not triggered at every step of the instrument, because the opcode will lower performance and eat resources. That is why I added `kUpdateOnMetronome`, so that you can define how fast you want the images to update.

EDIT: my bad, I thought you wanted a constant change, like a light indicator. Go ahead and check out the [cabbageSet](https://cabbageaudio.com/docs/cabbage_set/) opcode provided in Cabbage.

---

<div class="post-metadata">

**Author:** ![circuitdust](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/circuitdust/32/5283_2.png) [@circuitdust](https://forum.cabbageaudio.com/u/circuitdust)\
**Post date:** [18 December 2023 05:17 UTC](https://forum.cabbageaudio.com/t/toggle-image-changes-when-button-is-pressed/4016/3 "2023-12-18T05:17:42Z")

</div>

I ought to leave a better answer. You can handle functionality on a button press if the value of its channel changes. Like so:

```
; get button channel
kWifi cabbageGetValue "btnWifi"

; update image on button press - check if value of channel changes
if changed(kWifi) == 1 then
    ; check if button is on
    if kWifi == 1 then
        cabbageSet 1, "image_of_wifi3", "visible", 1
        cabbageSet 1, "image_of_wifi1", "visible", 0
    else ; button is off
        cabbageSet 1, "image_of_wifi3", "visible", 0
        cabbageSet 1, "image_of_wifi1", "visible", 1
    endif
endif

```

This way `cabbageSet` won’t murder your runtime. You can further improve this by multiplying the value of 1 or 0 you pass for “visible” by `kWifi` so you don’t need another if-else check.

```
if changed(kWifi) == 1 then
    cabbageSet 1, "image_of_wifi3", "visible", 1 * kWifi
    cabbageSet 1, "image_of_wifi1", "visible", 1 - 1 * kWifi
endif

```

In hindsight, this can also work if you make `kWifi` a sort of “light indicator” for if your effect is functioning or not (bad Wi-Fi signal-gate triggered).
