Randomization, experimenting with my FIRST :) servo - trying to randomize an infinite loop

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
PixelShady
Posts: 9
Joined: Mon Sep 02, 2019 4:36 pm

Randomization, experimenting with my FIRST :) servo - trying to randomize an infinite loop

Post by PixelShady » Tue Oct 22, 2019 4:56 pm

Hi,

Please have look at my code below and tell what is wrong.

Code description - An infinite loop which turns a 180 degree servo all the way to the left, and then back 180 degrees all the way to the right. Left to right, forever, but with some additional randomization. What I want it to do is turn at a 'random' speed (from a list of 5) for each new loop of my while-loop - the sleep(delay) controls this - but what actually happens is that the random function gets called only once at the start, and that same value gets used over and over again. Is it possible, in micropython, to re-trigger a randomization from within a while-loop so that my servo changes speed while the loop is running? If not then how do I go about achieving this.

Thanks.

Code: Select all

from microbit import *
import random

pin0.set_analog_period(10)

step_delay_times = [5, 20, 40, 60, 80]

x = random.choice(step_delay_times)

while True:
    
    for position in range(50, 250, 1):
        pin0.write_analog(position)
        sleep(x) # time in milliseconds between each 1 degree step
    
    sleep(500) # a half-second pause between direction changes

    for position in range(250, 50, -1):
        pin0.write_analog(position)
        sleep(x) # time in milliseconds between each 1 degree step
        
    sleep(500) # a half-second pause between direction changes

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: Randomization, experimenting with my FIRST :) servo - trying to randomize an infinite loop

Post by cefn » Tue Oct 22, 2019 5:58 pm

Hi, PixelShady

A while loop is just a set of steps which are repeated in sequence ( see https://github.com/cefn/group9/blob/mas ... eating.pdf ).

The steps which are repeated are 'inside' the while block. The lines 'inside' the while block are immediately after the while line and are indented one level deeper ( see https://github.com/cefn/group9/blob/mas ... blocks.pdf ).

Currently your selection of a random speed is executed just once, before the while loop begins.

If you want to repeat the random.choice(...) step, you should move the whole of that line inside the while loop so that it's one of the repeated steps. Make sure you indent the line in the same way as the other lines inside the while block.

Then each time the loop runs it will also re-run that step and get a (potentially) different value of x.

PixelShady
Posts: 9
Joined: Mon Sep 02, 2019 4:36 pm

Re: Randomization, experimenting with my FIRST :) servo - trying to randomize an infinite loop

Post by PixelShady » Tue Oct 22, 2019 7:00 pm

Thanks for that :) - have it working now.

A few changes: got rid of the list and replaced it with a standard 'randrange' - also, added randomization to the pause between direction changes.

Can my code be tidied-up or shortened in any way?

Code: Select all

from microbit import *
import random

pin0.set_analog_period(10)

while True:
    x = random.randrange(2, 21) # variable declared within loop, to -
                                # - re-trigger each time loop runs
    y = random.randrange(20, 3000) # ditto (as x above)
    
    for position in range(50, 250, 1):
        pin0.write_analog(position)
        sleep(x) # (speed) time in milliseconds between each 1 degree step
    
    sleep(y) # a brief pause between direction changes
    
    x = random.randrange(2, 21) # re-declared a SECOND time to re-randomize -
                                # - speed between direction changes
    y = random.randrange(20, 3000) # ditto (as x above)
    
    for position in range(250, 50, -1):
        pin0.write_analog(position)
        sleep(x) # (speed) time in milliseconds between each 1 degree step
        
    sleep(y) # a brief pause between direction changes
NOTE: this is for a small low-voltage micro servo, running on the 3V supplied directly from my microbit.

Post Reply