0

HI i am new tp pi and python and wanted to create something that simply turns on and off a led.

It does not seem to work and not sure why?

here is the python script:

    from gpiozero import LED, Button

from time import sleep

led = LED(2)
led.on()


while True:
    range(100) and None
    led.on()
    sleep(0.5)
    led.off()
    sleep(0.5)

Here is a pic of my pi 3 wiring:

enter image description here

I followed this guide

https://gpiozero.readthedocs.io/en/stable/recipes.html

  • 4
    Have you tried turning your LED around, they will only work in one direction. – CoderMike Jun 18 at 10:59
  • Awsome turning around the led fixed it! – Jonathan Jun 18 at 11:08
  • @CoderMike Are you going to make that an answer to get credit for it? Otherwise someone else might run with the glory. – Mast Jun 18 at 16:53
  • How do I mark a comment as the answer? – Jonathan Jun 18 at 16:59
  • You can't mark a comment as an answer. – Seamus Jun 19 at 1:08
1

The problem is caused by the line range(100) and None which is causing your loop to exit. You need to remove it. Where did it come from? It's not in the docs.

If you want to loop 100 times, change the while True: to for i in range(100):

Also it's probably just the way you pasted in the code here but your first line is indented, just make sure it isn't indented in your program.

| improve this answer | |
  • HI. tried that and it did not work, the led light does not turn on. i added a print(i) to check if its hitting atht bit of the code and it does – Jonathan Jun 18 at 8:39
  • 1
    "The problem is caused by the line range(100) and None" No, it isn't. That expression is well-formed so it's not throwing an exception to terminate the loop. It's not part of the while condition so its result does not control the while. It's also so trivial that its execution time should not be significantly affecting the rest of the loop operations. It is indeed pointless here, but it is certainly not the cause of the failure. – nobody Jun 18 at 16:51
  • Oh yes, my mistake. – ben_nuttall Jun 18 at 18:32

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.