0

I am coding this lamp circuit in which an LED is switched on/off on alternate press of switch

enter image description here

I have written two versions of the code, one using polling and other using interrupts. I found that the poll version works more reliably (i.e the alternate on/off of led works well on alternate switch press) than the interrupt version. Why is the interrupt version missing some button presses?

poll version

import RPi.GPIO as GPIO
import time

is_lamp_on = False
try:
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(11,GPIO.OUT)
    GPIO.setup(12,GPIO.IN,pull_up_down=GPIO.PUD_UP)
    GPIO.output(11,GPIO.LOW) #lamp is initially off
    #print ('initial lamp is ', is_lamp_on)
    #print ('initial input 12 is ', GPIO.input(12))

    while True:
        current_pressed_state = GPIO.input(12)
        if GPIO.input(12)==GPIO.LOW: #button pressed down but not released yet
            #print(' button pressed, will wait for release ')
            time.sleep(0.01) #wait for button to be released
            new_pressed_state = GPIO.input(12)
            #print('new pressed state ',new_pressed_state)
            if (current_pressed_state != new_pressed_state): #button released
                #print(' button released, old lamp state is ',is_lamp_on)
                if(is_lamp_on == True): # lamp was ON when button is released 
                    GPIO.output(11,GPIO.LOW) #lamp off
                    is_lamp_on = False;
                    #print('new lamp state before break ', is_lamp_on)
                else: #lamp is not ON
                    GPIO.output(11,GPIO.HIGH)
                    is_lamp_on = True;
                    #print('new lamp state before break ', is_lamp_on)
        else:
            time.sleep(0.01) #wait before trying again
except KeyboardInterrupt:
    GPIO.cleanup()

interrupt version

import RPi.GPIO as GPIO
import time

is_lamp_on = False

def handle_button_press_event(button_pin):
    global is_lamp_on
    print('old lamp state ',is_lamp_on)
    if(is_lamp_on == True): # lamp was ON when button is released 
        GPIO.output(11,GPIO.LOW) #lamp off
        is_lamp_on = False;
        print('new lamp state before break ', is_lamp_on)
    else: #lamp is not ON
        GPIO.output(11,GPIO.HIGH)
        is_lamp_on = True;
        print('new lamp state before break ', is_lamp_on)

try:
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(11,GPIO.OUT)
    GPIO.setup(12,GPIO.IN,pull_up_down=GPIO.PUD_UP)
    GPIO.output(11,GPIO.LOW) #lamp is initially off
    print ('initial lamp is ', is_lamp_on)
    print ('initial input 12 is ', GPIO.input(12))
    GPIO.add_event_detect(12,GPIO.FALLING,callback=handle_button_press_event,bouncetime=10)
    while True:
        pass
except KeyboardInterrupt:
    print('cleaning up')
    GPIO.cleanup()
2
2

Possibly because the interrupt version is mainly doing a busy spin.

Change

while True:
   pass

to

while True:
   time.sleep(1)
6
  • bang on. I was about to add a comment that when I ran 'top' command, I notice that the interrupt version takes 100% CPU while the poll version doesn't. After running the two versions, it seems to me that both are similar in performance while I read that interrupts are more efficient. Are the two methods similar? Aug 12 at 20:31
  • Not much in it as far as performance. I'd probably use the interrupt version if I was doing this.
    – joan
    Aug 12 at 20:36
  • So interrupt and polling have similar performance but interrupt makes a better readable code? Aug 12 at 20:38
  • 1
    Which is better is down to personal taste, there is no objective right or wrong in matters of style. The interrupt solution is likely to have better performance but polling will only be slightly worse in your application.
    – joan
    Aug 12 at 21:48
  • @ManuChadha Frankly using a quad core processor as a switch is overkill - whether one is "more efficient" i.e. using ±0.001% of the CPU capability is inconsequential - you could do the same job with a simple switch.
    – Milliways
    Aug 13 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.