Skip to content

Latest commit

 

History

History
39 lines (18 loc) · 3.18 KB

Reganh4 - Acceleration along Axis and Translation to Python.md

File metadata and controls

39 lines (18 loc) · 3.18 KB

Chapter 2:

In chapter 2, we explore how the CPx reads acceleration along the Z and Y axis. To help, the chapter includes pictures of the command in MakeCode as a point of reference.

Let’s get started!

Make Code:

To begin, we place a “set item to” block, found in the variables tab, within the “on start” block. In order to differentiate between the two axis, Y and Z, we must rename the “item” portion of this block to “accel_y_reading” as it will be used to read the acceleration along the Y axis. Similarly, we repeat the above step and rename the “item” portion to “accel_z_reading”.

Once we have the “on start” block complete, we move on to the “forever” block. It is here that we tell the CPx what we want to be done in specific situations. Seen below, this program tells the CPx to read the acceleration along the y axis. The CPx is then told that if there is an acceleration along the y axis, to set all pixels to red. Similarly, if there is no acceleration, it is told to set all pixels to green.

This step is then repeated using acceleration along the z axis where a recorded acceleration will turn the pixels blue and no acceleration will keep the pixels green.

Taking this to Python!

While I originally had trouble translating my programming in MakeCode to Python, the process became easier as I continued to spend time reading articles and watching tutorials on youtube. After a couple readings and 2 or 3 videos I was ready to go. Just as pictures from the MakeCode program were provided above, this section will provide screenshots of the Python code to serve as a point of reference.

Here we go!

To begin, we need to tell Python where we’re getting our commands from. To do so, we type, “from dcs import * ”. With this, Python understands where variables and commands are being stored. The inclusion of his line of code simplifies our translation immensely.

Just as our MakeCode program started with an “on start” command where variables were defined, we begin our Python program with defining our variables.

Moving forward, we look to begin coding our “Forever” block. In Python, this is designated with the code, “while True:” . Once these basic codes are established, we are ready to translate the code to Python. While this portion of the process may seem daunting, it is actually extremely similar to the process used in MakeCode.

Click here for my video tutorial