option or command for toggle

Give it a try, it costs you nothing !
Post Reply
Zim
Posts: 280
Joined: Mon Feb 08, 2021 9:15 pm
Has thanked: 251 times
Been thanked: 128 times

option or command for toggle

Post by Zim »

It would be so sweet to have a built in Toggle function that would check and alternate state, such as:

pin(D1) = 0
numb = 10
zim$ = "up"
wait

Toggle pin(D1) = 0, pin(D1) = 1

Toggle numb = 10, numb = 20

Toggle zim$ = "up", zim$ = "down"


Thanks Zim
User avatar
cicciocb
Site Admin
Posts: 1889
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 405 times
Been thanked: 1261 times
Contact:

Re: option or command for toggle

Post by cicciocb »

Hi Zim,
for toggling the pin you can simply do :

Code: [Local Link Removed for Guests]

pin(D1) = 1 - pin(D1)
For the variables, it could be useful to create some dedicated sub like this :

Code: [Local Link Removed for Guests]

numb = 10
zim$ = "up"
.......
.......
Toggle numb, 10, 20  ' toggle number
Toggles zim$, "up", "down" ' toggle string
.....
.....
SUB Toggle(a, b, c)
if (a = b) then a = c else a = b
end sub

SUB Toggles(a$, b$, c$)
if (a$ = b$) then a$ = c$ else a$ = b$
end sub

Zim
Posts: 280
Joined: Mon Feb 08, 2021 9:15 pm
Has thanked: 251 times
Been thanked: 128 times

Re: option or command for toggle

Post by Zim »

Thanks cicciocb! pin(D1) = 1 - pin(D1) worked perfect!

Zim
Post Reply