Timer with micro:bit? [SOLVED]

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
csanyipal
Posts: 6
Joined: Sat May 11, 2019 10:43 am
Location: Serbia
Contact:

Timer with micro:bit? [SOLVED]

Post by csanyipal » Sat May 11, 2019 10:57 am

Hi,

recently we got micro:bits at school.
We are working on a project that uses two micro:bit's: mb1 and mb2.
One micro:bit - the mb2 - should do the followings:

when it receives a message from the other micro:bit - namely 'mb1',
then the 'mb2' must to start a timer which count from 1 to 10.

During this time, it has to count the steps taken by the competitor with the micro: bit 'mb2' attached to the leg.
When mb2 reaches 10 seconds, it must to stop counting steps.

Can this be done with just two micro:bit's at all?

Best, Pal
Last edited by csanyipal on Sun May 12, 2019 4:43 am, edited 1 time in total.

lujo
Posts: 3
Joined: Sat May 11, 2019 2:30 pm

Re: Timer with micro:bit?

Post by lujo » Sat May 11, 2019 2:34 pm

Yes, this can be done with just two micro:bits. One plus battery pack
in your tennis sock (mb1) and one connected to your laptop (mb2).

This is a typical micro:bit task. So, should be easy.

The microbit has an increasing millisecond counter for measuring
time intervals and an accelerometer for detecting movements.

From https://microbit-micropython.readthedocs.io/en/latest/
you need to read up on the following modules:

- microbit : for buttons, display and movements
- radio : send and receive messages
- utime : count milliseconds

On mb1:
when button A pressed send message "OK"

On mb2:
when message "OK" received
set step counter to 0
start counting milliseconds
when step detected increment counter
show counter
stop after 10 seconds
wait for next "OK" message

csanyipal
Posts: 6
Joined: Sat May 11, 2019 10:43 am
Location: Serbia
Contact:

Re: Timer with micro:bit?

Post by csanyipal » Sat May 11, 2019 7:05 pm

Thank you very much for pointing me into the right direction!

I learn from the documentation how to use utime, radio and microbit : for buttons, display and movements.
mb1 and mb2 will be used booth with battery pack.
The code snippet is this:

Code: Select all

step = 0

now = int(utime.ticks_ms() / 1000)
while True:
    if int(utime.ticks_ms() / 1000) > now + 10:
        break
    else:
        if accelerometer.was_gesture("3g"):
            step += 1
This is on mb2 which is attached to the leg of competitor.
( Do not know how to switch ON BBCode? )

rhubarbdog
Posts: 166
Joined: Tue Nov 07, 2017 11:45 pm

Re: Timer with micro:bit? [SOLVED]

Post by rhubarbdog » Thu May 16, 2019 3:19 pm

I have a clock class which updates the number of seconds kind of asynchronously. See clock.py in this repo

Post Reply