Python Forum
Difference and meaning in this scrips
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Difference and meaning in this scrips
#1
Hi guys.
Just started out with python, programming a small ESP343, where I would like to create a python script for running.
But I'm no shark in programming - so be gentle

I've bought some time ago - Lego Millinium Falcon - and would like to spice this a little op.
I want to create my own python script - for lightning up the Falcon in the dark.

So here I just wondering the difference between:

from machine import Pin from time import sleep_ms led1=Pin(1,Pin.OUT) led2=Pin(2,Pin.OUT) try: while True: led1.value(1) led2.value(1) sleep_ms(6000) led1.value(0) led2.value(0) sleep_ms(6000) except: pass
And the same code in my head are not working
from machine import Pin from time import sleep_ms led1=Pin(1,Pin.OUT) led2=Pin(2,Pin.OUT) def cockpit(): led1.value(1) led2.value(1) sleep_ms(6000) led1.value(0) led2.value(0) sleep_ms(6000) try: while True: cockpitt() except: pass
I'm not getting any error when trying the last - but was hoping creating the light as individuel light for cockpitt, the backend - where i need som small LED strips for - but as all - I need to learn to crawl before I can walk.
But my Thonny - does not give any error and just for this I do not know why ? So I hoping on One of You to help me a little bit here

My high hopes is - creating this Python script to run a script, where my definitions will be given a number - so try loop will choose a different next time.
For my point of view - and hopefully learn something about python - I'm hoping this little project - creating a script for the ESP343 - making the Falcon fly in the bedroom.
Reply
#2
(Jun-29-2025, 10:49 AM)Udbytossen Wrote: I'm not getting any error when trying the last
There is an error because the function is defined as cockpit() and called as cockpitt(). The reason you don't see any error is the presence of
except: pass
Such a statement hides errors because you are telling Python: if there was an error, ignore it!

DONT HIDE ERRORS in Python. Error messages are your most useful tool for debugging the code. So instead of
try: while True: cockpitt() except: pass
simply write
while True: cockpit()
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Hi Udbytossen,

To control an LED using the GPIO of an ESP32-S3 with MicroPython, here's a simple code example to blink an LED connected to a GPIO pin (e.g., GPIO 2). This code was provided by Grok, created by xAI, and I'm sharing it with their permission and full credit.

from machine import Pin import time # Initialize GPIO pin (e.g., GPIO 2) as output led = Pin(2, Pin.OUT) # Replace 2 with your GPIO pin number # Blink the LED while True: led.value(1) # Turn LED on time.sleep(0.5) # Wait for 0.5 seconds led.value(0) # Turn LED off time.sleep(0.5) # Wait for 0.5 seconds
Setup Instructions:
  • Hardware: Connect an LED with its anode to the chosen GPIO pin (e.g., GPIO 2) and cathode to GND on the ESP32-S3, using a current-limiting resistor (220-330 ohms) in series.

  • Software: Ensure MicroPython firmware is installed on your ESP32-S3. Use a tool like Thonny or uPyCraft to upload the code.

  • GPIO Selection: GPIO 2 is generally safe for most ESP32-S3 boards, but confirm your board's pinout. Avoid pins used for boot or flash modes (refer to the ESP32-S3 datasheet for details: https://www.espressif.com/en/support/doc...s/esp32-s3)

  • Running the Code: Upload the code to your ESP32-S3. The LED should blink every 0.5 seconds. Press Ctrl+C in the MicroPython REPL to stop it.

Note: If you meant a specific GPIO pin or have a different setup (e.g., using Arduino IDE), let me know, and I can adjust the code. Also, I couldn't find any reference to an "ESP343" chip from Espressif—did you mean ESP32-S3 or another model like the ESP32P4?
Credit: This solution was generated by Grok, created by xAI. For more details on ESP32-S3, check Espressif's official site: https://www.espressif.com/en/products/so.../esp32-s3)
Hope this helps! Let me know if you have further questions.
Reply
#4
Hi Guys - thanks for the answers.
For the answers. - Its a ESP32-S3.
I'm having the manual to follow but this is properly out of their scope with lightning the Falcon up - But must start somewhere to learn this.

I want 2leds turning on - at the same time using led1+2 (which are used in manual) for the light in the cockpitt
So I created a 2PIN simular setup instead of JUST 1PIN, which actually works in the first example I'm giving but not in the second, but would like to move out calling def instead of having all code under While True.

Ending up with having 4different def - i wanting to call as cockpitt, guns over,guns above, lightspeed - with really 4 different sets of Pins to light
So I'm needing to have to use severals Pins when I'm done with it - starting out with testing whats possible with this controller etc. (As I can be done when you can use its as a running softflow with a LEDbar.

So starting out with this little try - as an example - learning forwards hopefully. Step 2 in the Manual is en Flowing LED
Removing lines only gives an invalid syntax somewhere - but using the code from the manual - just trying to have the 2Pins on Pin1&2 to light up the cockpitt as the first light.
except: pass
Reply
#5
(Jun-29-2025, 02:38 PM)Udbytossen Wrote: Removing lines only gives an invalid syntax somewhere
If you remove the except: clause, you must also remove the line containing try:

Also the error is not "somewhere". Python is very precise and verbose as to where errors are located. Use this information to correct your code.
Larz60+ likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to understand the meaning of the line of code. jahuja73 0 1,410 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Csv writer meaning of quoting mg24 2 2,673 Oct-01-2022, 02:16 PM
Last Post: Gribouillis
  meaning of -> syntax in function definition DrakeSoft 5 4,725 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft
  Operator meaning explanation Sherine 3 3,509 Jul-31-2021, 11:05 AM
Last Post: Sherine
  parser.parse_args() meaning vinci 2 5,563 Oct-26-2020, 04:13 PM
Last Post: vinci
  What is the meaning of k in this function? giladal 3 4,954 Aug-15-2020, 12:32 PM
Last Post: buran
  Here what is the meaning of span=(1,2) ,match='1'? srisrinu 1 3,201 Apr-27-2020, 10:22 AM
Last Post: anbu23
  What is the meaning of mutable data type? qliu 3 4,961 Apr-17-2020, 07:20 PM
Last Post: deanhystad
  Upper-Bound Exclusive Meaning Johnny1998 1 5,370 Aug-02-2019, 08:32 PM
Last Post: ichabod801
  Finding phrases of one semantic meaning VladislavMz 2 42,425 Dec-20-2018, 03:29 AM
Last Post: VladislavMz

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.