Notifications
Clear all

From normally closed to normally open.

6 Posts
5 Users
0 Likes
820 Views
(@mevero)
Member
Joined: 2 years ago
Posts: 5
Topic starter  

I found a nice program so I can use two buttons, only now I looking for the answer if your button goes from normally closed to open and the program react.

Voor NC

 Hope you can read the code it is very small


   
Quote
(@markasread)
Member
Joined: 3 years ago
Posts: 12
 

Hi, 

Can you maybe quickly explain what it is you're trying to do? What are you controlling and why it needs two buttons? 


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1682
 

Hi @mevero,

   Extending @markasread 's question.

  • Are you asking how to modify the program to use a 'Normally closed' switch ... that is a switch which connects the contacts when it is NOT being pressed, and opens to disconnect between the contacts when it is pressed?
  • If the answer to my question is yes, then it is possible for someone to explain how to reverse the logic as required, but it would be better if you also showed a diagram of your circuit, so that a more complete answer could be provided.

Best wishes, Dave


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

@mevero
Hope you can read the code it is very small

To enlarge small images right click the image and chose open link in new window.

The instruction steps given in How to Add Code to Your Post didn't work for me.

Instead I used these steps.

1. In the editor for your source code Select All and then Copy.
2. In the forum post click the <> in the top bar
3. Copy to the resulting code window and click OK.

Unfortunately the colour highlighting is lost.

Not sure if I copied the code correctly!!

 

from machine import Pin

led = Pin(5, Pin.OUT)
but1 = Pin(4, Pin.IN, Pin.PULL_UP)
but2 = Pin(14,Pin.IN, Pin.PULL_UP)

def debounce(pin):
    prev = None
    for _ in range(32):
        current_value = pin.value()
        if prev != None and prev != current_value:
            return None
        prev = current_value
    return prev

def but1_callback(pin):
    d = debounce(pin)

    if d == None:
        return
    elif not d:
        led.value(not led.value())
        print('WIT')

def but1_callback(pin):
    d = debounce(pin)

    if d == None:
        return
    elif not d:
        led.value(not led.value())
        print('zwart')

but1.irq(trigger=Pin.IRQ_FALLING, handler=but1_callback)
but2.irq(trigger=Pin.IRQ_FALLING, handler=but2_callback)


    

   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2531
 
Posted by: @mevero

I found a nice program so I can use two buttons, only now I looking for the answer if your button goes from normally closed to open and the program react.

I don't do Python but it appears that the debounce routine just checks to see if the button state is stable and different, so it appears to be indifferent to HIGH/LOW states per se.

You'll need to change both callback routines from

elif not d:

to

elif d:

since you're now checking for the opposite condition.

Also you should probably change the IRQ's to IRQ_RISING to indicate interest in the opposite direction.

 

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@mevero)
Member
Joined: 2 years ago
Posts: 5
Topic starter  

The problem is that there is not one print "wit" or "Zwart". Debounce is not working correct.

It is a better idea to use asyncio but my program skills do not know how.

Could/want you write the complete code so that i can try it/use it


   
ReplyQuote