element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Back again with button problem
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 2 replies
  • Answers 1 answer
  • Subscribers 663 subscribers
  • Views 441 views
  • Users 0 members are here
  • raspberry_pi
Related

Back again with button problem

rcayot
rcayot over 5 years ago

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

  • Sign in to reply
  • Cancel
  • dougw
    0 dougw over 5 years ago

    Use the button to increment a counter.

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

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • rcayot
    0 rcayot over 5 years ago

    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

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube