Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
EduKit 1 problems
#1
Hi, I'm relatively new to Python but have some experience coding on the EduKit 1. I have been set a challenge of writing code so that when I press the button "x" amount of times the LEDs flash that many times as well. The code I have written sets a countdown that you can press the button however many times you want in but the LEDs always flash during that countdown and not after it like I want them to. Apparently there are no syntax errors or anything of the sort. I have done some research and looked in some coding manuals and EduKit worksheets but can't find anything helpful. I am using Python 3(IDLE) by the way and run my code with Thonny. Here is my code:

from gpiozero import Button
from gpiozero import LED
import time
counter = 0
red = LED(18)
yellow = LED(23)
green = LED(24)
red.off()
green.off()
yellow.off()
button = Button(25)
print ("Starting")
time.sleep(1)

for n in range (500):
        n + 1
        print(n)
        
print ("I'm off")
for number in range (2):
    red.on()
    green.on()
    yellow.on()
    time.sleep(0.2) 
    red.off()
    green.off()
    yellow.off()
    time.sleep(0.2)
Thanks,
Birdwatcher.
Reply
#2
the EduKit is just a breadboarding system. What is the processor that you are using (assume Raspberry-pi, but which model)?
Reply
#3
Hi Larz60+, sorry for the mistake. I am running a model B+.
Reply
#4
This article shows you how to create an interrupt on the pi when a button is pushed.
This is the way to go with your circuit as polling won't be needed.
An event (interrupt) will be created when button is pushed, and the code associated with that interrupt won't execute until that heppens/
article is here: https://www.raspberrypi.org/forums/viewt...p?t=146369
Reply
#5
Hi Larz60+,
Thanks for all your help - it really came in handy. My code works perfectly now:
from time import gmtime, strftime, sleep
import time, datetime
from gpiozero import Button
from gpiozero import LED
import time
n = 0 
button=[1]
red = LED(18)
yellow = LED(23)
green = LED(24)
red.off()
green.off()
yellow.off()
button = Button(25)

starttime = time.time()
print ("starting")
try:
   while True:
      currenttime = time.time()
      elapsed = currenttime - starttime
      # print elapsed
      if elapsed >= 7:
          break
      if button.is_pressed:
          n = n + 1
          print(n)
      sleep(0.2)
except:
    pass
print ("flashing", n, "times")
for number in range(n):
    red.on()
    yellow.on()
    green.on()
    time.sleep(0.5)
    red.off()
    yellow.off()
    green.off()
    time.sleep(0.5)
Thanks,
Birdwatcher
Reply
#6
Glad to hear that.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020