Unable to ctr the button properties (in Python)

Hi,

I’ve two virtual buttons, btn0 and btn1. I want to change the properties of btn1 when I’m pressing btn0 and when releasing it, bringing the original settings back to btn1.

The following code is not working and cannot find the reason.

Any help is appreciated.
TIA

    btn0status = '0'

    @blynk.VIRTUAL_WRITE('0')
    def btn0_handler(value):
      global btn0status 
      btn0status = value[0]
      if btn0status == '1':
        print('Btn pressed')
        blynk.set_property('1', 'offColor', '#FFFFFF')
        blynk.set_property('1', 'offBackColor', '#000000')
        blynk.set_property('1',' offLabel', 'READY')
      else:
        print('Btn released')
        blynk.set_property('1', 'onColor', '#000000')
        blynk.set_property('1', 'onBackColor', '#FFFFFF')
        blynk.set_property('1', 'onLabel', 'NOT READY')

    @blynk.VIRTUAL_WRITE(1)
    def btn1_handler(value):
      #security check
      #btn1 is activated only when btn0 is being pressed
      if value[0] == '1' and btn0status == '1':
          print('2n btn pressed')

These don’t look like vPins… in fact you have ’ ’ around all sorts of numbers in your code??? I thought it might be because you posted it unformatted (normally a no0B mistake :wink: ) but nope, I fixed the formatting and that is the way you posted it.

Because the numbers are not int but strings.

Oh, wait… you are posting Python code… might want to mention that in the post, those subtitles don’t always show up properly :stuck_out_tongue_winking_eye:

I don’t know if setProperty works yet in Python

From library:

def VIRTUAL_WRITE(blynk, pin):
        class Decorator():
            def __init__(self, func):
                self.func = func
                blynk.callbacks["V"+str(pin)] = func
            def __call__(self):
                return self.func()
        return Decorator

I guess the ‘’ can be eliminated :wink:

Documentation for 0.2.0 says it should… try the vPins without the ’ ’

Actually the single quotes don’t make a difference and the set_propery does work. What doesn’t work is the way I’m coding, I guess.

I have found in my Blynk Python 0.2.0 code that I needed to add this in my if() checks

if value[0] == str(‘1’);

converting a string to a string will not affect the code

I didn’t claim to understand the whys… I just had to change all my code around once in order to make it work, once I updated the library. It has been awhile since I played around with it so may even need further updating.

EDIT - well, I can finally revert my code back to “normal”… don’t know how or why, but it took me a lot of trial and error to come up with the “solution” awhile back. But doesn’t seem needed now.

Anyhow, @Emilio since I had not used set_property() in any of my Python coding yet, I tested your code as well… I don’t know your exact expected results, but think you are mixing up your onColor, onBackColor, offColor, offBackColor commands. I juggled them around a bit (AKA set them all to off) and everything is changing as expected.

What do you mean by that?

I mean that after the initial change you were not toggling anything, just sending the same state again and again.

On stand-by, btn1 should display ‘NOT READY’. When btn0 is pressed, btn1 should change to ‘READY’. When released, the text for btn1 should go back to ‘NOT READY’

Got ya… I was mainly reacting to the colours… multi-tasking… :stuck_out_tongue:

You are correct, the labeling doesn’t seem to change after the initial setting :thinking:

Heh… your issue was a uncaught syntax error… go fig…

image

Maybe it’s a typo when I wrote it here.
This’ what I’ve now and still fails…

    button0 = '0'

    @blynk.VIRTUAL_WRITE(0)
    def btn0_handler(value):
      button0 = value[0]
      if button0 == '1':
        print('Btn pressed')
        #blynk.set_property(1, 'offColor', '#FFFFFF')
        #blynk.set_property(1, 'offBackColor', '#000000')
        blynk.set_property(1, 'offLabel', 'READY')
      else:
        print('Btn released')
        #blynk.set_property(1, 'onColor', '#000000')
        #blynk.set_property(1, 'onBackColor', '#FFFFFF')
        blynk.set_property(1, 'onLabel', 'NOT READY')

    @blynk.VIRTUAL_WRITE(1)
    def btn1_handler(value):
      if value[0] == '1' and button0 == '1':
            print('2n btn pressed')

Possibly… but it was what was causing the same issue on my side… as opposed to offLabel an _offLabel would be an unknown parameter, thus ignored.

Anyhow, it works for me now (again, I juggled, so the order/settings are probably not your original intent)

btn0status = '0'


@blynk.VIRTUAL_WRITE(12)
def btn0_handler(value):
  global btn0status 
  btn0status = value[0]
  if btn0status == '1':
    print('Btn pressed')
    blynk.set_property(13, 'offLabel', 'READY')
    blynk.set_property(13, 'offColor', '#FFFFFF')
    blynk.set_property(13, 'offBackColor', '#000000')
  else: 
    print('Btn released')
    blynk.set_property(13, 'offLabel', 'NOT READY')
    blynk.set_property(13, 'offColor', '#000000')
    blynk.set_property(13, 'offBackColor', '#FFFFFF')



@blynk.VIRTUAL_WRITE(13)
def btn1_handler(value):
  #security check
  #btn1 is activated only when btn0 is being pressed
  if value[0] == '1' and btn0status == '1':
          print('2n btn pressed')

Amazing! Maybe the code doesn’t like me :roll_eyes:

@Gunner are you using the app on iOS or on Android?

I am predominantly Android. I have an old iPhone 4s but it is painfully slow, so rarely used, but I can check on it…

Ah, quicker then I thought, I already had it still running while plugged in.

Yep, works fine on iOS (beta) as well.