Newbie Led sequential blink

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
mahnonsaprei
Posts: 12
Joined: Sun Sep 03, 2017 1:43 pm

Newbie Led sequential blink

Post by mahnonsaprei » Sun Oct 14, 2018 12:01 pm

Hi at all, my name's Marcello from italy, i'm looking for example hot to blink sequential led with my pyboard.

This is my code but i don' t understand how. I would like whhen i press the button sled's start blink in sequential an then when i press again led's blink off.
Thanks for your support!

# main.py -- put your code here!
import time
from pyb import Pin

# OUTPUT PINS - LEDs
led1 = Pin('Y1', Pin.OUT_PP)
led2 = Pin('Y3', Pin.OUT_PP)
led3 = Pin('Y4', Pin.OUT_PP)
led4 = Pin('Y5', Pin.OUT_PP)
button = Pin('Y2', Pin.IN)

def leds():
led1.high()
time.sleep(0.1)
#led1.low()
led2.high()
time.sleep(0.1)
led3.high()
time.sleep(0.1)
led4.high()
time.sleep(0.1)
led1.low()
time.sleep(0.1)
led2.low()
time.sleep(0.1)
led3.low()
time.sleep(0.1)
led4.low()
time.sleep(0.1)
print (button.value())

def ledsOff():
led1.low()
led2.low()
led3.low()
led4.low()

while True:
if button.value() == 1 :
leds()
elif button.value() == 0:
ledsOff()

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Newbie Led sequential blink

Post by pythoncoder » Mon Oct 15, 2018 7:35 am

You haven't told us what happens when you run the code or how your switch is wired. If it's a single pole switch wired to ground you need a pullup:

Code: Select all

button = Pin('Y2', Pin.IN, pull=Pin.PULL_UP)
To make the button toggle, there are two approaches depending on what you want to learn.

To code it from scratch you need to find out about switch debouncing. You'll also need to write an event loop so that the LED's keep running (or not) while testing the switch state.

Alternatively you can build on existing solutions by learning uasyncio and using existing pushbutton classes. This resource provides guidance on this approach.

The uasyncio approach is "better" in that it is scalable to much more complicated applications. But if you want to learn the details of hardware interfacing, there is a case for coding it from scratch. Good luck ;)
Peter Hinch
Index to my micropython libraries.

mahnonsaprei
Posts: 12
Joined: Sun Sep 03, 2017 1:43 pm

Re: Newbie Led sequential blink

Post by mahnonsaprei » Mon Oct 15, 2018 11:37 am

First, Think you for your support. I have a resistor 10k connect to a button.

But i don t understand how, when i press the button, the leds blink on sequentially, and then, when i press the button again the leds stop.
I don t know how i store in a variable the state of button, 1 or 0.
Another question, i have pin y2, y3,ecc...how i store those pin in a list or other.
For example i would like to create a loop for those pins

;)

mahnonsaprei
Posts: 12
Joined: Sun Sep 03, 2017 1:43 pm

Re: Newbie Led sequential blink

Post by mahnonsaprei » Mon Oct 15, 2018 11:41 am

In this script, the leds starting when i plug the usb in pyboard when hold the button the leds stop and starting when i realesed. I would when i pressed start and when i pressed agail stopped.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Newbie Led sequential blink

Post by pythoncoder » Mon Oct 15, 2018 4:47 pm

I doubt anyone here is likely to offer you personal tuition in basic Python programming. I suggest you get a book or find an online course on Python 3.x, and work through it on a PC before attempting embedded programming.
Peter Hinch
Index to my micropython libraries.

mahnonsaprei
Posts: 12
Joined: Sun Sep 03, 2017 1:43 pm

Re: Newbie Led sequential blink

Post by mahnonsaprei » Mon Oct 15, 2018 5:42 pm

Ok, i understand, now i'm looking to buy a book ;)
Thanks for all

Post Reply