how to debounce these interrupts

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
mahmoud yassin
Posts: 1
Joined: Fri Jan 22, 2021 5:33 am

how to debounce these interrupts

Post by mahmoud yassin » Fri Jan 22, 2021 5:38 am

Code: Select all

from machine import Pin
from time import sleep
try:
  import usocket as socket
except:
  import socket
import network
import esp
esp.osdebug(None)
import gc
gc.collect()
ssid = 'ESP32-AP-WebServer'
password = '123456789'

ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid=ssid, password=password)

while not ap.active():
    pass
print('network config:', ap.ifconfig())

# output pins
segA = Pin(23, Pin.OUT) 
segB = Pin(22, Pin.OUT)
segC = Pin(33, Pin.OUT)
segD = Pin(13, Pin.OUT)
segE = Pin(21, Pin.OUT)
segF = Pin(19, Pin.OUT)
segG = Pin(18, Pin.OUT)

# input pins
button1 = Pin(36, Pin.IN)  # increment
button2 = Pin(34, Pin.IN)  # decrement
button3 = Pin(35, Pin.IN)  # reset

segments = [segA, segB, segC, segD, segE, segF, segG]  # assigning pins to arrary to ease addressing in loops
seven = [0b0111111, 0b0000110, 0b1011011, 0b1001111, 0b1100110, 0b1101101, 0b1111101, 0b0000111, 0b1111111, 0b1101111] #Binary values of seven segment numbers
global current_number
current_number = 0  # Default 7-segment value

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # server socket
s.bind(('',80)) # specifies that the socket is reachable by any address the machine happens to have
s.listen(5)     # max of 5 socket connections

# functions
def increment(self):
  global current_number
  sleep(.3)
  if current_number == 9 :
    current_number=0
  else :
    current_number = current_number+1
  for i in range(7):                                                  
     segments[i].value(seven[current_number]>>i & 1)  
def decrement(self):
  global current_number
  sleep(.3)
  if current_number == 0 :
    current_number=9
  else :
    current_number = current_number-1
  for i in range(7):                                                 
     segments[i].value(seven[current_number]>>i & 1)   
def reset(self):
  global current_number
  sleep(.1)
  current_number = 0
  for i in range(7):                                                  
     segments[i].value(seven[current_number]>>i & 1)   
    
def seven_seg(current_number, button):
    if button == 1:
        sleep(.3)
        if current_number == 9 :
          current_number=0
        else :
          current_number = current_number+1
    elif button == 2 :
        sleep(.3)
        if current_number == 0 :
          current_number=9
        else :
          current_number = current_number-1    
    elif button == 0:
        sleep(.1)
        current_number = 0
    else:
        current_number = current_number
        
    return current_number
                
                
def web_page(current_number):
    print('7-segment is at ' + str(current_number))
    
    html = """<!DOCTYPE HTML>  
        <html>  
        <head>  
          <meta name="viewport" content="width=device-width, initial-scale=1">  
        </head>  
        <body>  
           <center><h2>ESP32 Web Server in MicroPython </h2></center>  
           <center>  
             <form>  
               <button type='submit' name="SEVEN" value='1'> INCREASE</button>  
               <button type='submit' name="SEVEN" value='2'> DECREASE</button>
               <button type='submit' name="SEVEN" value='0'> RESET</button>  
             </form>  
           </center>
           <center><p>seven segment is at <strong>""" + str(current_number) + """</strong>.</p></center>
           <center><p>PRESS ANY BUTTON <strong></strong>.</p></center>  
        </body>  
        </html>"""  
    return html
    
#Defining external interrupt pins
button1.irq(trigger=Pin.IRQ_FALLING , handler = increment)
button2.irq(trigger=Pin.IRQ_RISING , handler = decrement)
button3.irq(trigger=Pin.IRQ_RISING , handler = reset)
   

while True:  
    for i in range(7):                                              
        segments[i].value(seven[current_number]>>i & 1) 
    conn, addr = s.accept()
    print("Got connection from %s" % str(addr))
    # Socket accept() 
    # Socket receive()
    request=conn.recv(1024)
    print("")
    print("")
    print("Content %s" % str(request))

    # Socket send()
    request = str(request)
    check = request.find('/?SEVEN')
    
    if check == 6:
        print(str(check))
        current_number = seven_seg(current_number,int(request[14]))
    
    response = web_page(current_number)
    conn.send('HTTP/1.1 200 OK\n')
    conn.send('Content-Type: text/html\n')
    conn.send('Connection: close\n\n')
    conn.sendall(response)
    
    # Socket close()
    conn.close()

User avatar
pythoncoder
Posts: 4921
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: how to debounce these interrupts

Post by pythoncoder » Fri Jan 22, 2021 9:25 am

I use uasyncio with this debouncer rather than using interrupts.
Peter Hinch

Post Reply