Your Turn: Add More Data

Let’s put ease of expanding the application to the test by adding a feature.  Instead of just counting down seconds, the expanded application will ask you for the millisecond (ms) time between counts.  You can still choose 1000 to have it count in seconds, but you can also specify a value like 100 to have it count in tenths of seconds.  

  • Make the changes to the sender and receiver scripts shown below.
  • Flash the updated code to the sender and receiver micro:bit modules and then Open Serial terminals.

Sender changes for countdown_sender

    text = input("Enter countdown start: ")
    value = int(text)
    text = input("Enter ms time between counts: ")         # add
    ms = int(text)                                         # add
    message = input("Enter message after countdown: ")
    dictionary = {  }
    dictionary['start'] = value
    dictionary['time'] = ms                                # add
    dictionary['after'] = message

Receiver Changes for countdown_receiver

        value = dictionary['start']
        ms = dictionary['time']              # add
        message = dictionary['after']
        print("value = ", value)
        print("ms = ", ms)                   # add
        print("message = ", message, "\n")
        while value >= 0:
            sleep(ms)                       # change
            value = value - 1
  • Try setting the countdown start to 30, the ms time between counts to 100, and the end message to ‘That was faster!’.  
  • Verify that the receiver result resembles the one shown below.

Only one line had to be added to the sender to send the ms value, and another to the receiver to recover it.  The rest of the changes would have been made, even if it was just one script on a single micro:bit.