Play LEGO Spike Prime – An Enhanced Version of Rain and Shine Model and Code Analysis

In today’s post, I would like to introduce a demo project contained in LEGO Spike Prime App. The project is located within Unit Plans -> Life Hacks -> Rain or Shine. You could find the step-by-step building instruction of the model there. I will focus on introducing the enhanced code and how to utilize LEGO Spike block-based language to make your robot versatile.

Initial Project

The initial project just finishes one simple function: Make the robot hold up the umbrella and wear glasses when the weather forcast indicates sunny day, otherwise, put down umbrella and glasses.

In the above code segment, the program sets the place of weather forcast (the initial value is empty), and sets motors’ initial position. It then broadcasts a message called “Forecast”.

Please note that in Scratch 3.0, we use “broadcast message” to transfer information before different sprites. Since each sprite has different code area, broadcasting message is the most convenient way to notify other sprites to make actions. However, in LEGO Spike, there is only one code area, called program canvas. Therefore, the broadcasting block here could be replaced with self-designed block, which I will introduce later.

In this second code segment (or called “stack”), the program plays beep twice and checks if the weather forcast is sunny or not. If it is sunny, the motor at port F will turn to the position of 0 and then return to position 300 after a short stay. The light matrix will be turned on with two different patterns.

Code Upgrading

In the enhanced version, the robot not only could hold up/put down umbrella and wear/take off glasses, but also speaks out the names of several cities, and judge if it is sunny, cloudy, raining or snowing. It will report the temperature in a clear voice. Let’s check how to implement those functions.

Sample Code

“when program starts” Block

In the above code segment “when program starts”, the program sets motors’ current position to relative position 0. It then itenerates the items of “City list” and calls “forcast” block to begin weather forecasting. As we could see from “City list”, it contains four cities. User could add/edit any city to this list if they would like to.

“forcast” Block

In this self-designed block, you need to add Weather Manager extension module to use those weather forcasting blocks. Please note that you have to be online to use those blocks because they fetch real-time data.

When the weather is sunny, cloudy or partly cloudy, motors at port A and E will go to relative position of -45 and 90, respectively. This behavior represents putting down umbrella and taking off the glasses. When the weather is raining or snowing, the robot will just take down glasses, but stil hold umbrella up. In this process, different voices and light matrix patterns will be played to indicate weather status.

At the end of the block, it will call another block “read_temperature” to convert the temperature value into voice.

“read_temperature” Block

This block uses many functions in Operators tab. Basically, it gets each letter from the string “temperature”, finds the corresponding sound file and speaks it out. If we create the sound file for “.” and “-” signs, we could make the robot identify minus and decimal point in temperature.

That is all for the code analysis of the upgraded project. If you would like to check the code by yourself, here is the project:

Play with your robot and write code to give it new skills. Don’t forget to enjoy the coding and have fun!

Note: All the analysis articles are copyright products of http://www.thecodingfun.com. Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.

One thought on “Play LEGO Spike Prime – An Enhanced Version of Rain and Shine Model and Code Analysis

  1. Pingback: Play LEGO Spike Prime – Upgrade the program of “Brain Game” Model – The Coding Fun

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.