Hello!
I'm going to use a remote control and wire the buttons, to detect when someone opens the refrigerator or the cabinets.

 
315MHZ 4 Channel Wireless RF Remote Control
315 MHZ remote control

 
I'm trying to detect different buttons, I could connect one, but I do not know how to detect different buttons for different actions.

This is my InMoov_custom.py

 


# -- coding: utf-8 --
# #############################################################################
#                           YOUR INMOOV CUSTOM
# Here you can add your own commands to play and test with inmoov
# If you udpate the whole script, don't worry, those commands are safe
# ##############################################################################

# -- already created
#left=Runtime.create("i01.left", "Arduino")
#left.setBoard("atmega2560")
#left = Runtime.start("i01.left", "Arduino")
#left.connect("COM4")
 
def publishPin(pins):
    for pin in range(0, len(pins)):
        print pins[pin].address, pins[pin].value
        if pins[pin].value>0:

          # How to identify pin 49, 47, etc?
          # if 49

          wantBeer()

          # if 47
          #wantCoffee()
 
left.addListener("publishPinArray","python","publishPin")
left.enablePin(49, 1)
left.enablePin(47, 1)


 

Thanks!

 

 

 

 

 

MaVo

6 years 3 months ago

It should be if pin == 49:

Take this with a grain of salt as I can't test this right now.

Your updated script would then look like:

# -- coding: utf-8 --
# #############################################################################
#                           YOUR INMOOV CUSTOM
# Here you can add your own commands to play and test with inmoov
# If you udpate the whole script, don't worry, those commands are safe
# ##############################################################################

# -- already created
#left=Runtime.create("i01.left", "Arduino")
#left.setBoard("atmega2560")
#left = Runtime.start("i01.left", "Arduino")
#left.connect("COM4")

def publishPin(pins):
  for pin in range(0, len(pins)):
    print pins[pin].address, pins[pin].value
      if pins[pin].value>0:

        # How to identify pin 49, 47, etc!
        if pin == 49:

          wantBeer()

        if pin == 47:
          wantCoffee()

left.addListener("publishPinArray","python","publishPin")
left.enablePin(49, 1)
left.enablePin(47, 1)

Thanks MaVo for the quick response, but it does not work for me.

I can't even print a text.

 


# -- coding: utf-8 --
# #############################################################################
#                           YOUR INMOOV CUSTOM
# Here you can add your own commands to play and test with inmoov
# If you udpate the whole script, don't worry, those commands are safe
# ##############################################################################

# -- already created
#left=Runtime.create("i01.left", "Arduino")
#left.setBoard("atmega2560")
#left = Runtime.start("i01.left", "Arduino")
#left.connect("COM4")
 
def publishPin(pins):
    for pin in range(0, len(pins)):
        print pins[pin].address, pins[pin].value
        if pins[pin].value>0:
         
          # This works
          #wantBeer()

          # nothing is printed out
          print "--------------------------"
          print "This text does not come out printed"
          print "Remote button: ",pin

         
          # No worky
          if pin == 49:
            wantBeer()

          # No worky
          if pin == 47:
            wantCoffee()

          # Nothing happens.
 
left.addListener("publishPinArray","python","publishPin")
left.enablePin(49, 1)
left.enablePin(47, 1)


 

I'd use default rate

e.g.

left.enablePin(49)

do you get "anything" printed out ?

might want to send a noWorky...

Ahoy Grog!

I just send noWorky.

I try
left.enablePin(49)
left.enablePin(47)

but SwinGui seems to freeze and not respond for seconds.

T-800 remote receiver

 

def publishPin(pins):
    for pin in range(0, len(pins)):
        print pins[pin].address, pins[pin].value
        if pins[pin].value>0:
         
          # This works
          #wantBeer()

          # but a I have noticed that some times
          # loops forever as if the button was still pressed
         

          # nothing is printed out
          print "--------------------------"
          print "This text does not come out printed"
          print "Remote pin: ",pin
         
          # No worky.
          if pin == 49:
            wantBeer()

          # No worky.
          if pin == 47:
            wantCoffee()

          # Nothing happens.
 
left.addListener("publishPinArray","python","publishPin")
left.enablePin(49,1)
left.enablePin(47,1)

# SwinGui seems to freeze using this and takes too long for respond
#left.enablePin(49)


Is there any magic way to post here the code of a .py file with the syntax colored as in the examples of the services? Or attach the files?

 

 

I don't know if I can see de status of the pins in this screen, I click "read" and errors appear:

 

Cant start here too, gives a 

------SyntaxError: (encoding declaration in Unicode string, (string, 0, 0, )) at org.python.core.ParserFacade.fixParseError(ParserFacade.java:95) at org.python.core.ParserFacade.parse(ParserFacade.java:205) at org.python.core.Py.compile_flags(Py.java:1976) at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:267) at org.myrobotlab.service.Python$PIThread.run(Python.java:160)------

GroG

6 years 3 months ago

For Beer And Coffee !  - this is important...

I had to laugh at myself for not spotting a big problem sooner.

Astro, whenever you "subcribe" to a publishing method the callback method will follow certain conventions.

Whenver you see a publishPin being subcribed, the callback will be onPin

always change "publish" ==to==> "on"

so

def publishPin(pins):

    for pin in range(0, len(pins)):

        print pins[pin].address, pins[pin].value

        if pins[pin].value>0:

becomes

def onPin(pins):

    for pin in range(0, len(pins)):

        print pins[pin].address, pins[pin].value

        if pins[pin].value>0:

GroG

6 years 3 months ago

onPin looked a little flakey, but onPinArray is worky for me...
See the following :  publishPinArray is somewhat an optimization for communicating pin changes in a batch.

Thank you Grog!

Now I understand. I was waiting for the "print" to appear printed in the command window. That's why I did not see anything printed.

I was using publishPin because I was following this example:

http://myrobotlab.org/content/mrl-1742-analog-reader-correct-method

I translated your code to my InMoov_custom.py and add state change detection to trigger only once.
I'm not a good programmer, but I could figure out how to do it after many mistakes.

Now I don't have to be stuck only in beer, which is not a problem, but I also need coffee to keep working. :)

 


# -- coding: utf-8 --
# #############################################################################
#                           YOUR INMOOV CUSTOM
# Here you can add your own commands to play and test with inmoov
# If you udpate the whole script, don't worry, those commands are safe
# ##############################################################################

# -- already created
#left=Runtime.create("i01.left", "Arduino")
#left.setBoard("atmega2560")
#left = Runtime.start("i01.left", "Arduino")
#left.connect("COM4")

global pinBeerLastState
pinBeerLastState = 0
global pinCoffeeLastState
pinCoffeeLastState = 0

left.enablePin(49)
left.enablePin(47)
python.subscribe("i01.left", "publishPin")
python.subscribe("i01.left", "publishPinArray")

def onPinArray(pins):
    global pinBeerLastState
    global pinCoffeeLastState
    #print pin
    #print "Hola................"
    for pin in range(0, len(pins)):

        #print pins[pin].address, pins[pin].value

        if pins[pin].value>0:

          #print "Pressed button: ",pins[pin].address

          if pins[pin].address == 49:
            if pinBeerLastState == 0:
              pinBeerLastState = 1
              print "-> 49 Pressed    *"
              wantBeer()
            #ignore if it is still pressed
         

          if pins[pin].address == 47:
            if pinCoffeeLastState == 0:
              pinCoffeeLastState = 1
              print "-> 47 Pressed    *"
              wantCoffee()
            #ignore if it is still pressed

        else:
          if pins[pin].address == 49:
            if pinBeerLastState == 1:
              print "<- 49 Released "
            pinBeerLastState = 0

          if pins[pin].address == 47:
            if pinCoffeeLastState == 1:
              print "<- 47 Released "
            pinCoffeeLastState = 0

 


Thank you!