On 2015-08-20 16:12, John McKenzie wrote:

  Thanks for the reply. Also, thanks to Laura who replied via email.

  Tried a bunch of things based off these comments and I always ended up
with one of two situations, the channel conflict error, or an instant run
and quit issue. This new version of the code runs but is unresponsive. I
removed loops then put in a short sleep loop. while True:
     time.sleep(0.1) It could be my hardware is done up wrong, but it
looks OK. Perhaps it is always sleeping.

  Anyone at all know about GPIO and the Pi under the Python library
RPi.GPIO please feel free to advise as to what the problem is most likely
to be.


import atexit
import time
from blinkstick import blinkstick
import RPi.GPIO as GPIO

led = blinkstick.find_first()
colour = 0
timered = 0
timeyellow = 0
timeblue = 0
timestamp = time.strftime("%H:%M:%S")



GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)



def red_button(channel):
     colour = 1
     while colour == 1:
         print "Red Button pressed"
         timered += 1


def yellow_button(channel):
     colour = 2
     while colour == 2:
         print "Yellow Button pressed"
         timeyellow += 1


def blue_button(channel):
     colour = 3
     while colour == 3:
         print "Blue Button pressed"
         timeblue += 1

GPIO.add_event_detect(23, GPIO.RISING, callback=yellow_button,
bouncetime=200)
GPIO.add_event_detect(22, GPIO.RISING, callback=red_button,
bouncetime=200)
GPIO.add_event_detect(24, GPIO.RISING, callback=blue_button,
bouncetime=200)

while True:
     time.sleep(0.1)

def exit_handler():
     print '\033[0;41;37mRed Team:\033[0m ', timered
     print '\033[0;103;30mYellow Team:\033[0m ', timeyellow
     print '\033[0;44;37mBlue Team:\033[0m ', timeblue
     flog = open('flag1log.text', 'a')
     flog.write(timestamp + '\n' + 'Red Team: ' + str(timered) + '\n' +
'Yellow Team: ' + str(timeyellow) + '\n' + 'Blue Team: ' + str(timeblue)
+ '\n')
     flog.close()
atexit.register(exit_handler)
GPIO.cleanup()


The function 'red_button' will be called when a rising edge is detected.

In that function, you're assigning to 'colour' but not changing it in
the loop, so it's basically just a busy loop. However, you're trying to
change 'timered', which is a global variable, but you're not declaring
it as global, so that will raise UnboundLocalError.

Similar remarks apply to the other two callbacks.

I think you'd be better off detecting both the rising and falling
edges, with a callback for each, recording when each edge occurred
(duration of press = time of falling edge - time of rising edge).

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to