Seite 1 von 1

LED Blinker - Syntax Problem

Verfasst: Freitag 2. Februar 2024, 08:48
von de-coder
Vorab: Habe kaum Ahnung von Python und fuchse mich gerade ein wenig ein durch Modifikation von bestehenden Programmen. Konkret will ich einen LED-Blinker programmieren. Die Anzahl Blinker soll in eine Subroutine übernommen werden, dort erfolgt die Ausgabe (LED an einem Raspi Pico, https://wokwi.com/projects/375033640113754113).

Leider gibt es ein Syntaxproblem in Zeile 11 der Subroutine. Diverse Syntax-Checker zeigen aber kein Problem.

Code: Alles auswählen

import machine
import time
# Define the GPIO pins for the LED and push button
LED_PIN = 0  # GP0

# Initialize the LED pin as an output and the button pin as an input with a pull-up resistor
led = machine.Pin(LED_PIN, machine.Pin.OUT)

# Blinken
blink(anz)
for x in range(anz):
    led.on()  # Turn on the LED
    time.sleep(1)
    led.off()  # Turn off the LED
    time.sleep(1)
       
#Start
blink (3)

Re: LED Blinker - Syntax Problem

Verfasst: Freitag 2. Februar 2024, 08:51
von __deets__
Da ist keine Funktion, weil da kein def steht. Und die Einrückungen fehlen. Ein minimales durcharbeiten des offiziellen Tutorials empfiehlt sich: https://docs.python.org/3/tutorial/cont ... -functions

Re: LED Blinker - Syntax Problem

Verfasst: Freitag 2. Februar 2024, 08:53
von de-coder
ja eben gesehen, versuche es mal

Re: LED Blinker - Syntax Problem

Verfasst: Freitag 2. Februar 2024, 09:26
von de-coder
Gelöst, Textformatierungsproblem wars

Code: Alles auswählen

import machine
import time
# Define the GPIO pins for the LED and push button
LED_PIN = 0  # GP0

# Initialize the LED pin as an output and the button pin as an input with a pull-up resistor
led = machine.Pin(LED_PIN, machine.Pin.OUT)


def blink(anz):
  for x in range(anz):
    led.on()  # Turn on the LED
    time.sleep(1)
    led.off()  # Turn off the LED
    time.sleep(1)


blink(3)