Notifications
Clear all

What's on your bench?

43 Posts
10 Users
39 Reactions
6,012 Views
byron
(@byron)
No Title
Joined: 6 years ago
Posts: 1212
 
Posted by: @sean451

how would you flash the pins backwards

for the two examples I gave see the following:

import machine
import utime


LED_pin_number = [1,2,3,4,5,25]

for pin in LED_pin_number:
    machine.Pin(pin, machine.Pin.OUT)


def flash_all_my_pins():
    #flash the pins from 1 to 6
    for x in range(0,6):
        LED = machine.Pin(LED_pin_number[x])
        LED.toggle()
        utime.sleep(0.2)
    #flash the pins from 6 to 1
    for x in range(5,-1,-1):
        LED = machine.Pin(LED_pin_number[x])
        LED.toggle()
        utime.sleep(0.2)


while True:
    flash_all_my_pins()
    # and then loop again.

OR

import machine
import utime

LED_pins = []

for x in range(1,7):
    LED_pins.append(machine.Pin(x, machine.Pin.OUT))
 

def flash_all_my_pins():
    # flash each pin from 1 to 6
    for pin in LED_pins:
        pin.toggle()
        utime.sleep(0.2)
    # flash each pin from 6 to 1
    for pin in reversed(LED_pins):
        pin.toggle()
        utime.sleep(0.2)

while True:
    flash_all_my_pins()
    # and then loop again.


   
ReplyQuote
byron
(@byron)
No Title
Joined: 6 years ago
Posts: 1212
 

@sean451

You might like to look at a small mini introduction to micropython by the maker of the TinyPico who came to micropython from Arduino code.  I goes over a few of the basics in a series of 4 or 5 videos.  Here is a link to the first one.


   
ReplyQuote
Sean451
(@sean451)
Member
Joined: 4 years ago
Posts: 62
 

@byron

Okay, thanks so much for the help Byron. I think I've got it now and I'm finally happy with the result. Here's the final code:

 

# LED Single Dice on Tiny2040

# Circuit:
#    Six LEDs +ve connected to six 220Ω Resistors to Pins 1-6 -ve to GND.
#    Touch switch connected to Pin 26 (Analog A0) and PWR rail.
#    Tiny2040 5v (0) and Ground (1) to PWR and GND rails.


# Imports

import machine, utime, random

# Declarations

timer = .05                              # Delay timer to flash LEDs
hold = 1                                 # Delay timer after roll. Change to adjust.

LED_pips = [1,2,3,4,5,6]

for pin in LED_pips:
    machine.Pin(pin, machine.Pin.OUT)

button = machine.Pin(26, machine.Pin.IN, machine.Pin.PULL_DOWN)

# Functions

def flashPips():                           # Function to turn LEDs on and off.
    for x in range(0,6):
        LED = machine.Pin(LED_pips[x])
        LED.toggle()
        utime.sleep(timer)
    for x in range(5,-1,-1):
        LED = machine.Pin(LED_pips[x])
        LED.toggle()
        utime.sleep(timer)

def flashRoll():                           # Function to display random die roll.
    print("Roll =",ran)                    # Debug/Comment out to stop Serial Print.

    for x in range(2):                     # Loop runs twice to toggle off LEDs.
        if (ran >= 1):
            LED = machine.Pin(LED_pips[0])
            LED.toggle()
        if (ran >= 2):
            LED = machine.Pin(LED_pips[1])
            LED.toggle()
        if (ran >= 3):
            LED = machine.Pin(LED_pips[2])
            LED.toggle()
        if (ran >= 4):
            LED = machine.Pin(LED_pips[3])
            LED.toggle()
        if (ran >= 5):
            LED = machine.Pin(LED_pips[4])
            LED.toggle()
        if (ran >= 6):
            LED = machine.Pin(LED_pips[5])
            LED.toggle()
        utime.sleep(hold)
    
# Loop

while True:
    ran = random.randint(1, 6)
    if button.value() == 1:
        flashPips()
        flashRoll()

Cheers!

--->Sean

(◕(' 人 ') ◕)


   
ReplyQuote
byron
(@byron)
No Title
Joined: 6 years ago
Posts: 1212
 

@sean451

Its looking good 😀 

You might like to consider the following snippet, or some variation of. (remove the random number to the function). Just a thought and one can keep at this coding infinitum, and it also good to stop modifying and move on to the next exciting project.

def flashRoll():
ran = random.randint(1, 6)
print("Roll =",ran)

for x in range(1,(ran +1)):
LED = machine.Pin(x)
LED.toggle()
utime.sleep(hold)

   
ReplyQuote
Sean451
(@sean451)
Member
Joined: 4 years ago
Posts: 62
 

@byron I will give that code a try. Today I've taken what I learned from you yesterday and applied it to another LED blinking project, this one using the Pico (for more pins) and a Bar Graph LED with 10 lights. It was certainly an exercise of wiring to get the pins hooked up to the correct pins in exact order so not only was my programming skills challenged, but also my physical wiring aptitude as well.

I've been playing around with the functions to try to get the lights to blink the way I want them to. Right now, I've got it so it cycles through a chase loop, turning on and then off and then blink on and off a random number of times. I'll add more loops to do different things like blink every other light and so on. I find it's the best way to learn new things.

Cheers!

blink

 

--->Sean

# Bar Graph Module with 10 LEDs

# Circuit:
#    Bar Graph LEDs -ve connected to 10 330Ω Resistors to GND. +ve to Pins 1-10.
#    Raspberry Pi Pico Ground Pin to GND rail to Resistors.


# Imports

import machine, utime, random

# Declarations

timer = .05                              # Delay timer to flash LEDs
hold = 1                                 # Delay timer after roll. Change to adjust.

LED_pips = [1,2,3,4,5,6,7,8,9,10]

for pin in LED_pips:
    machine.Pin(pin, machine.Pin.OUT)

def flashPips():                           # Function to turn LEDs on and off.
    for x in range(2):
        for x in range(0,10):
            LED = machine.Pin(LED_pips[x])
            LED.toggle()
            utime.sleep(timer)
    for x in range(2):
        for x in range(9,-1,-1):
            LED = machine.Pin(LED_pips[x])
            LED.toggle()
            utime.sleep(timer)
            
def blinkPips():
    for x in range(ran):
        for x in range(0,10):
            LED = machine.Pin(LED_pips[x])
            LED.toggle()
        utime.sleep(timer)
        for x in range(9,-1,-1):
            LED = machine.Pin(LED_pips[x])
            LED.toggle()
        utime.sleep(timer)

    
while True:
    ran = random.randint(1, 6)
    flashPips()
    blinkPips()

(◕(' 人 ') ◕)


   
ReplyQuote
byron
(@byron)
No Title
Joined: 6 years ago
Posts: 1212
 

@sean451

I think the code in your last post should produce an error as the blinkPips() function does not know anything about the ran variable.   You could either move the whole random number generation to the function, or pass the generated random number to the function with blinkPips(ran) when calling the function.

Nice blinky breadboard set up I see. 😀 


   
ReplyQuote
Sean451
(@sean451)
Member
Joined: 4 years ago
Posts: 62
 

@byron I did take a look at that YouTube tutorial, but he lost me in #2 when he said to load rshell and go into the tiny pico repl. He just assumes the user can get there and if not, then the rest of the tut is useless because you can't follow along. I can get into Visual Studio Code and into the >>> terminal for the Pico, but the commands do not work the same as what he's doing.

--->Sean

ETA: I did manage to get a work around so I'm following along now. Sometimes I give up too easily and sometimes I don't give myself credit for what I do know

This post was modified 4 years ago by Sean451

(◕(' 人 ') ◕)


   
ReplyQuote
byron
(@byron)
No Title
Joined: 6 years ago
Posts: 1212
 

@sean451

I also don't use the rshell as I find that Thonny is better, both for the repl but aslo for the ease of creating and running programs saved on either the microcontroller or the main computer.  As you have no doubt found you can follow along those videos by using Thonny with the RPI Pico, or indeed with Visual Studio if you prefer.  Its worth following along that short sequence of videos to get a good flavour of MP from C++.  And if MP floats your boat then there are quite a few other tutorials to get properly up to speed with python.  I like these getting started tutorials from Cory Shafer


   
ReplyQuote
Sean451
(@sean451)
Member
Joined: 4 years ago
Posts: 62
 

@byron

Thanks again for the help. My micropython skills are definitely improving. I updated the program to wait for the roll to display and then turn on each of the unlit LEDs and then turn them off again in a chase sequence. It makes it look more flashy. It was an exercise in figuring out where within the loops and fors to put them and the holds. 

My next step is a more permanent circuit on a perfboard. I've got some 3mm LEDs coming in today's mail so I want to get out my soldering iron and do some practice with that. Eventually, the breadboard has to be dismantled since my roommate doesn't fancy having old ones plastered all over the house. A permanent perfboard with a couple of headers for the Tiny2040 module and a battery connection should do the trick.

Cheers!

--->Sean

# LED Single Dice on Tiny2040

# Circuit:
#    Six LEDs +ve connected to six 220Ω Resistors to Pins 1-6 -ve to GND.
#    Touch switch connected to Pin 26 (Analog A0) and PWR rail.
#    Tiny2040 5v (0) and Ground (1) to PWR and GND rails.

# Imports

import machine, utime, random

# Declarations

timer = .05                                # Delay timer to flash LEDs
hold = 2                                   # Delay timer after roll. Change to adjust.

LED_pips = [1,2,3,4,5,6]

for pin in LED_pips:
    machine.Pin(pin, machine.Pin.OUT)

button = machine.Pin(26, machine.Pin.IN, machine.Pin.PULL_DOWN)

# Functions

def flashPips():                           # Function to turn LEDs on and off.
    for x in range(0,6):
        LED = machine.Pin(LED_pips[x])
        LED.toggle()
        utime.sleep(timer)
    for x in range(5,-1,-1):
        LED = machine.Pin(LED_pips[x])
        LED.toggle()
        utime.sleep(timer)

def flashRoll():                           # Function to display random die roll.
    if (ran >= 1):
        LED = machine.Pin(LED_pips[0])
        LED.toggle()
        utime.sleep(timer)
    if (ran >= 2):
        LED = machine.Pin(LED_pips[1])
        LED.toggle()
        utime.sleep(timer)
    if (ran >= 3):
        LED = machine.Pin(LED_pips[2])
        LED.toggle()
        utime.sleep(timer)
    if (ran >= 4):
        LED = machine.Pin(LED_pips[3])
        LED.toggle()
        utime.sleep(timer)
    if (ran >= 5):
        LED = machine.Pin(LED_pips[4])
        LED.toggle()
        utime.sleep(timer)
    if (ran >= 6):
        LED = machine.Pin(LED_pips[5])
        LED.toggle()
    utime.sleep(hold)
    zipPips()

def zipPips():                             # Function to turn on remaning LEDs and then all off.
    for x in range(0,6):
        LED = machine.Pin(LED_pips[x])
        if LED.value() != 1:
            LED.toggle()
            utime.sleep(timer)
    utime.sleep(timer)
    for x in range(5,-1,-1):
        LED = machine.Pin(LED_pips[x])
        LED.toggle()
        utime.sleep(timer)

# Loop

while True:
    ran = random.randint(1, 6)
    if button.value() == 1:                # Wait for button press.
        print("Roll =",ran)                # Debug/Comment out to stop Serial Print.
        flashPips()                        # Flash LEDs on and off.
        flashRoll()                        # Display roll.
 

(◕(' 人 ') ◕)


   
ReplyQuote
byron
(@byron)
No Title
Joined: 6 years ago
Posts: 1212
 
Posted by: @sean451

My micropython skills are definitely improving

They sure are, thats looking good.  You probably know anyway, but just a tip to be wary of those global variables, especially when your coding expands to larger programs. You can use them as you have but its easy to create a local variable (to the function) with the same name as a global and get unexpected results.

As python does not have constants I get into the habit of creating global constant variables (those I don't want to modify) in all capital letters (so HOLD = 2) and name global variable (that are expected to change) with a 'g' in front (so g_ran = random... etc). 

I've got used to doing this so I was forgetting that your 'ran' variable was a global on a first glance at your code.  As this is your early days into python coding you may like to consider some such scheme like I use and it can help a lot when reading through your own code at some later date. (and maybe its useful program comments will be as lacking as mine 😎)  The use of capitals for constant variables is python programming convention.  My global naming convention is just my own habit as usually for the pro's the use global variables would get a smack on the wrist. 😀  (and in your program the random number generation could have been done within the flashRoll function)

But these are just minor points you may like to consider, and your program is looking so much better than the first one you published. 👍 

 


   
Sean451 reacted
ReplyQuote
jBo
 jBo
(@jbo)
Member
Joined: 4 years ago
Posts: 110
 

@sean451 Oh, that is really clever. I'm going to have to try it. I think it would also be worthwhile if you just had 4 LEDs.

In theory, theory and practice are the same.
In practice, they're different.


   
ReplyQuote
MechTech51
(@mechtech51)
Member
Joined: 4 years ago
Posts: 3
 

Just getting started... I have several projects in mind, but gonna start small and work my way through some simple SERVOS and LEDS with some Models and see what I can Animate, as a robotic desktop assistant!

Perhaps an animatronic, D&D Dungeon master, "animatronic puppet"? or

a lost inspace B-9Robot KareoKe BOT?

IMG 4375
IMG 4374
IMG 4373
IMG 4366
image

 


   
ReplyQuote
TheOutlander
(@theoutlander)
Member
Joined: 4 years ago
Posts: 83
 

@mechtech51 this looks interesting! I'd love to know more. 

image

My work area incorporates a number of similar concepts, and I have the ATX power supply with a similar break out board in my workshop. 

image
image

I like how you have incorporated everything together, in a suitably sized space!

Cheers!

"Hardware eventually fails. Software eventually works." - Michael Hartung


   
ReplyQuote
Page 3 / 3