Real Starry Sky | UNIHIKER Tutorial: Learn Python with Graphical Programming for Beginners 06

0 2573 Medium

As the sun sets and the city illuminates, it's likely been a while since you've witnessed a glittering starry night sky. The increasing brightness of the nighttime environment due to our ever-growing urban lifestyle has made it harder to see the real stars. In this lesson, we will use the UNIHIKER board to simulate a starry sky and control its appearance based on ambient brightness.

projectImage

Goal

Display a simulated starry sky on the screen, which can dynamically change according to the ambient brightness. When the environment is bright, the stars will not be visible, but as the darkness descends, the stars will appear and twinkle continuously.

projectImage

 

Get to Know

1. The review of how to use variables

2. The if-then-else statement in the program

3. The principle of three-primary color(RGB) mixing

4. How to use the light sensor on the UNIHIKER board.

 

 

HARDWARE LIST
1 UNIHIKER
1 Type-C&Micro 2-in-1 USB Cable

Software: Mind+

Download address: https://www.mindplus.cc

projectImage

Hands-on Practice

To create a simulated starry sky, we first need to acquire the ambient brightness and then control the twinkling of the stars accordingly. Therefore, we will divide this project into two tasks:

Task 1: Acquire and display ambient light intensity

In task 1, first, we will learn how to obtain the ambient brightness and convert the numbers to string characters, then we can display the light intensity on the UNIHIKER screen.

Task 2: Control the twinkling of the stars

In task 2, we will learn how to mix colors using RGB values and utilize variables to control the twinkling of the stars based on ambient brightness.

Task 1: Acquire and display ambient light intensity

1. Hardware Connection

Connect UNIHIKER to the computer via a USB cable.

projectImage
2. Software Preparation

Open Mind+, and complete the software preparation according to the figure below.

projectImage

3. Write a Program

We'll use the built-in light sensor on the UNIHIKER board to obtain the ambient light intensity first, which can be done by using the command Read ambient light intensity. You can find the block in the "Onboard Sensors" under the "UNIHIKER" category in the command area.

projectImage

Drag the Read ambient light intensity command and put it into the text display command.

projectImage

To make it easier to understand the displayed value, we can add an explanatory text to the ambient light intensity value, such as "Ambient Light Intensity: 233". Then, we just need to adjust the position and size of the text properly.

projectImage

To obtain the ambient light intensity value in real time, we need to continuously update the content of the displayed text object in a forever block. The complete reference code is shown below:

projectImage
4. Run the Program

Click Run, try covering the light sensor with your finger, and then observe the ambient light intensity value displayed on the UNIHIKER screen (as shown in the figure below).

When the sensor is covered, the environment is dark, and the light intensity is low.

When the sensor is not covered, the environment is bright, and the light intensity is high.

Note: For more information about the light sensor, please refer to the "Knowledge Base".

projectImage

Task 2: Control the twinkling of the stars

1. Run the Program

We have obtained and displayed ambient light intensity in task 1. Next, we will gradually complete the simulation of the real starry sky by making the stars light up, twinkle, and be controlled by ambient light intensity.

(1) Light up the stars

To light up the stars, we use an image with hollowed-out stars image that allows a different color to shine through. So, we need a solid white rectangle that fills the screen, an image with hollowed-out stars, and text, and their layer relation is shown below:

projectImage

Next, we just need to display the content on the UNIHIKER board in sequence according to the layer relationship. The images are in the "Lesson 6 > Image Resources" folder, and don't forget to adjust the image size when using them.

projectImage

(2) Twinkling stars

The twinkling of stars is the process of stars gradually lighting up and then dimming. Now let's see how it is implemented.

(a) Change the brightness of the stars

The brightness of the stars is modified by changing the color of the bottom solid rectangle between black and white. How to do that? There are two methods:

Method 1: Use the The color of the update object name is blue command to change the color of the bottom solid rectangle multiple times. However, the limited available colors cannot enable the brightness of the stars to change continuously.

projectImage

Method 2: Use the "RGB color" command to change the color of the rectangle "object background". You can find the The color of the update object name is red 255 green 255 blue 255 block by searching for the keyword "red".

Note: For knowledge about RGB color mixing, refer to the "Knowledge Base".

projectImage

The "RGB color mixing" command can be used to control the color to change evenly from black to gray to white by continuously changing values. The figure below shows the command settings and corresponding color changes.

projectImage

(b) Implement twinkling of stars

We can use the repeat block under the "Control" category to control the multiple changes of the variable Brightness so as to implement the continuous color changes of the bottom solid rectangle. First, set the variable brightness to 0, then increase it by 51 each time, which can make the color change from black to gray gradually. After increasing it 5 times, the Brightness variable becomes 255, and the color will be displayed as white. You can set a waiting time for every change to make it change slower. The reference code is shown below:

projectImage

(3) Control the twinkling of stars with ambient light intensity

In the project, if the environment is bright, the stars will not be displayed, and if it is dark, the stars will twinkle. Here we need to judge it according to the current ambient light intensity. In task 1, we have already known that the higher the light intensity value, the brighter the environment, that is, if the light intensity value is greater than a certain value, then the stars will not be displayed, and the bottom rectangle will be in black, or else the stars will twinkle. Find the if then else block under the "Control" category and complete the code according to the description.

Note: For a detailed introduction to the if then else command, refer to the "Knowledge Base".

projectImage

Finally, to ensure that the UNIHIKER screen can be used to control the twinkling of stars in real-time based on the ambient light intensity, we need to put the star-related codes into the forever block. Change the font color to white to make it easier for us to see the ambient light intensity value. The complete code is shown in the figure below.

projectImage
2. Run the program

Click Run and observe the display effect on the UNIHIKER screen (as shown below).

projectImage

Knowledge Base

1. If-then-else statement

The if-then-else statement is one of the conditional statements. During the execution process, it will evaluate whether the given condition is met, If it is met, the statement in the "if" branch will be executed; otherwise, the statement in the "else" branch will be executed. The code execution flow can be described in the figure below.

projectImage

2. Principle of RGB color mixing

The three primary colors(RGB) refer to the three basic colors that cannot be decomposed in color, and in this lesson, we use the three primary colors of light, namely red, green, and blue, collectively referred to as RGB, to make different colors by mixing them.

projectImage

In the RGB color command, the colors red, green, and blue can all be changed through values, and different combinations of values will produce different colors. You can adjust RGB values to get the desired color as needed. It should be noted that the valid range of the RGB values is 0-255, which represents the brightness of each color. For example, in the case of red, 0 represents the darkest red, which is black, while 255 represents pure red.

projectImage

A special mixed color is white, which is a mixture of red, green, and blue with a value of 255. In this lesson, the transition from black to gray and back to white requires the simultaneous variation of red, green, and blue values from 0 to 255.

projectImage

3. Onboard light sensor of UNIHIKER

The UNIHIKER board has an onboard light sensor on the upper right, which adopts a PT0603 photoresistor. You can use the read ambient light intensity block to obtain the current ambient light intensity. The light intensity is a continuously changing value. A smaller value indicates a darker environment, while a larger value indicates a brighter one.

projectImage

In addition, it should be noted that if an onboard sensor of UNIHIKER, such as the light sensor, is used in the code when the UNIHIKER is disconnected, a program error message will occur and the pop-up window will not appear after running the code.

projectImage

At this point, if you need to display the program effect on the computer screen, you can use the "Connect Remote Terminal" tool to display the UNIHIKER screen on the computer through the UNIHIKER IP. For detailed operating instructions, you can visit the "VNC Screen Sharing" page on the UNIHIKER official website.

4. Command Learning

In this project, we mainly use commands such as light sensor and RGB color mixing commands. Now, let’s get to know them in detail.

projectImage

Challenge

There is a black cat in a pitch-black room. Can you find it? Using the "hidden black cat image" in the image resources as the background, design a small game called "Find the Black Cat" using what you learned from this lesson.

Tips:

1. Use the light sensor to control the brightness of the UNIHIKER screen. If the environment is bright, and the screen is dark, you can only see the cat's eyes;

2. When the environment is dark or the light sensor is covered by a finger, the screen lights up and the black cat in the room immediately appears.

projectImage
icon Real Starry Sky.rar 712KB Download(11)
License
All Rights
Reserved
licensBg
0