2 Replies Latest reply on Feb 6, 2020 12:00 PM by rcayot

    Back again with button problem

    rcayot

      Well, I got my traffic light model working well with help from this forum.  Now I am trying something a little bit different.  I have several scripts that flash a set of LEDs with different patterns.what I would like to do is use a single button to switch between patterns.

       

      i have the callback function working, but even if I record and increment button pushes, I don't seem to be able to use an additional button push to jump to the next block.

       

      if anyone knows of an example, please point me to it.  Or, if straight forward, please post.

       

      roger

        • Re: Back again with button problem
          dougw

          Use the button to increment a counter.

          Use If statements to execute different routines for each count value.

          2 of 2 people found this helpful
          • Re: Back again with button problem
            rcayot

            okay, got it done.  simplified two patterns, and focused on the loops.  really had to trial and error this one, but I AM persistent.

             

            Roger

             

            import RPi.GPIO as GPIO
            from gpiozero import Button, LED, Buzzer
            from time import sleep
            from signal import pause
            
            
            red1 = LED(25)
            red2 = LED(6)
            yel1 = LED(12)
            yel2 = LED(19)
            grn1 = LED(20)
            grn2 = LED(13)
            GPIO.setup(18, GPIO.OUT, initial=GPIO.LOW)  # buzzer
            GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
            GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
            global ontime
            global offtime
            ontime = [.1, .2, .3, .4, .5]
            offtime = [.5, .4, .3, .2, .1]
            # Buzz = Buzzer(18)
            # btn1 = Button(26)
            # btn2 = Button(21)
            # ont = 0.2
            # offt = 0.2
            btncount = "global"
            btncount = 0
            
            
            def btn1(channel):
                global btncount
                btncount += 1
                print(btncount)
                if btncount == 3:
                    btncount = 0
                
            GPIO.add_event_detect(26, GPIO.FALLING, callback=btn1, bouncetime=300) 
            
            
            seq = [red1, red2, yel1, yel2, grn1, grn2]
            
            
            while True:
                if btncount == 0:
                    for x in seq:
                        x.blink(.1, .1, 3)
                        sleep(.2)
                        pause
                elif btncount == 1:
                        red1.blink(.5, .5)
                        sleep(1.1)
                        if btncount == 2:
                            red1.off()
                        pause
                else:
                    red2.blink(.5, .5)
                    sleep(1.1)
                    pause