1

I have a trouble with programming Raspberry Pi Pico. I am using Thonny IDE and micropython. I am just a beginner so just download code from their website (https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/6) and install it to microcontroller. But when I save this code:

from machine import Pin
import time
led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
while True:
if button.value():
    led.toggle()
    time.sleep(0.5)

I receive this message:

Traceback (most recent call last): File "", line 10 IndentationError: unindent doesn't match any outer indent level Could you help me please?

Marek
  • 13
  • 2
  • 2
    I wonder if you're mixing spaces and tabs. You should be using 4 spaces consistently. See here - https://stackoverflow.com/questions/14979224/indentation-error-in-python – waterloomatt Feb 17 '21 at 14:59
  • 1
    Your error message mentions line 10 but you only have 8 lines shown above – Steve Robillard Feb 17 '21 at 22:18
  • 3
    I think you should indent after the while statement. PS - Thonny usually detects tabs and suggests you to change them to 4 spaces. – tlfong01 Feb 18 '21 at 00:37

2 Answers2

1

You need to indent your code properly:

from machine import Pin
import time
led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
while True:
    if button.value():
        led.toggle()
        time.sleep(0.5)
the busybee
  • 10,755
  • 3
  • 13
  • 30
0

Maybe try this:

from machine import Pin
import time
led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
while True:
    if button.value():
        led.toggle()
        time.sleep(0.5)

I just changed if button.value(): four more spaces in so its inside the while true: loop.

aldboss12
  • 1
  • 3
  • wrong: no indentation after ``if`` sentence and unnecessary indentation for first tatement – Lixas Feb 25 '21 at 06:56