Skills Workshop: mBlock 5 and Arduino Upon completion of this course, the participant will be able to: i. Explain the fundamental of visual programming. ii. Use algorithm and flowchart in basic programming. iii. Demonstrate visual programming using mBlock. iv. Identify feature of Arduino Uno. v. Construct a system using Arduino Uno. Demonstrate a project using Arduino Uno with mBlock.
SKILL MODULE: DIGITAL STEM 1 Contents 1.0 Introduction: Visual Programming ........................................................................................................2 2.0 The Algorithm, Flowchart and Programming........................................................................................7 3.0 Visual Programming: mBlock...............................................................................................................14 3.1 THE INTERFACE .....................................................................................................................................14 3.2 SPRITE PROGRAMMING........................................................................................................................16 i. Add a Sprite..........................................................................................................................................16 ii. Move the Sprite...................................................................................................................................18 iii. Add Sounds.........................................................................................................................................19 iv. Add a Costume ...................................................................................................................................22 Activity 1: Bouncing ball......................................................................................................................24 Activity 2: Bouncing ball with a static obstacle...................................................................................25 Activity 3: Bouncing ball with a moving obstacle ...............................................................................25 Activity 4: Complete bouncing ball game ...........................................................................................26 4.0 Programming Arduino..........................................................................................................................28 4.1 ARDUINO BOARD ..................................................................................................................................28 4.2 PROGRAM ARDUINO UNO USING MBLOCK 5.......................................................................................30 Activity 5: Blinking LED........................................................................................................................30 Activity 6: ON and OFF the LED with keyboard keys...........................................................................38 Exercise 1: Improve the program in Activity 6................................................................................42 Exercise 2: Create a mood lights based on the flowchart...............................................................43 Activity 7: Control LED using buttons in mBlock (virtual buttons)......................................................44 Activity 8: Integrate push button and buzzer .....................................................................................53 Exercise 3: Create a program to turn on/off an LED and buzzer using a single push button. ........54 Activity 9: Turn on LED with a clap (using microphone). ....................................................................54 4.3 INTEGRATE SENSORS AND ACTUATOR .................................................................................................56 Activity 10: Light sensor with LED.......................................................................................................56 Activity 11: Control Blinking LED with potentiometer........................................................................58 Exercise 4: Make some adjustment so that potentiometer able to control (ON/OFF) 3 LEDs.......59 Activity 12: Auto Awning System (using Light Dependent Resistor (LDR) and Servomotor)..............60
SKILL MODULE: DIGITAL STEM 2 1.0 Introduction: Visual Programming i. What is programming It is a field of skill that entails the creation of computer applications, websites, and software. Programming is the creation of digital language. It provides a way for human to interact with the computer through a set of instructions called code. ii. Types of Programming Text-based: programming language using script or text and think like computer Visual-based: programming language using illustration that make sense to human iii. Visual programming language In computing, a visual programming language (VPL) is any programming language that lets users create programs by manipulating program elements graphically rather than by specifying them textually. A VPL allows programming with visual expressions, spatial arrangements of text and graphic symbols, used either as elements of syntax or secondary notation. For example, many VPLs (known as dataflow or diagrammatic programming) are based on the idea of "boxes and arrows", where boxes or other screen objects are treated as entities, connected by arrows, lines or arcs which represent relations. iv. Example of visual programming It can be divided into a noted categories such as Educational, Multimedia, Video Games, System/simulation, Automation, Data warehousing/ business intelligence. Refer to https://en.wikipedia.org/wiki/Visual_programming_language v. mBlock Installation Step 1: Open the website: https://mblock.makeblock.com/en-us/download/ Figure 1: mBlock Download page NOTE: Win7 or Win10 (64-bit are recommended) Click this button to Download
SKILL MODULE: DIGITAL STEM 3 Step 2: Double-click the .exe file. Figure 2: mBlock .exe file Step 3: Click Yes to start the Setup. Figure 3: Download and setup windows
SKILL MODULE: DIGITAL STEM 4 Step 4: Click Finish to complete the Setup. Figure 4: mBlock Setup completion Step 5: Click INSTALL to install the driver so that the USB port can be used with external device. Click OK if succesfully installed, and Exit (X) DriverSetup(X64). Figure 5: Installation windows
SKILL MODULE: DIGITAL STEM 5 Step 6: The mBlock will upload with the complete interface. Figure 6: Loading mBlock software Figure 7: mBlock UI The mBlock is loading. The mBlock is ready.
SKILL MODULE: DIGITAL STEM 6 Step 7: Complete your profile by click the Login/Signup icon indicated. Figure 8: Profile setup Note: If you do not have the account, please setup your email in gmail. Login/Signup icon.
SKILL MODULE: DIGITAL STEM 7 2.0 The Algorithm, Flowchart and Programming Algorithm It is a logical step-by-step approach to solve the problem. This is the first step in the process to solve a mathematical or computer problem. An algorithm includes calculations, reasoning, and data processing. Algorithms can be presented by natural languages, pseudocode, and flowcharts, etc. According to Dr. Christoph Koutschan, a computer scientist working at the Research Institute for Symbolic Computation (RISC) in Austria, he has surveyed voting for the important types of algorithms. As a result, he has listed 32 crucial algorithms in computer science. Despite the complexity of algorithms, we can generally divide algorithms into six fundamental types based on their function. Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is a "text-based" detail (algorithmic) design tool. The rules of Pseudocode are reasonably straightforward. All statements showing "dependency" are to be indented. Flowchart A flowchart is the graphical or pictorial representation of an algorithm with the help of different symbols, shapes, and arrows to demonstrate a process or a program. With algorithms, we can easily understand a program. The main purpose of using a flowchart is to analyse different methods. Terminal Box - Start / End Input / Output Process / Instruction Decision Connector / Arrow https://www.edrawsoft.com/explain-algorithm-flowchart.html
SKILL MODULE: DIGITAL STEM 8 Basic Flow Control Behind all of the software we use on a daily basis, there's a code being run with all sorts of terms and symbols. Surprisingly, it can often be broken down into three simple programming structures called Sequential, Selections, and Loops. These come together to form the most basic instructions and algorithms for all types of software. There are three basic constructs in an algorithm. 1. Sequential - The simplest control statement. A sequence of sequential statements runs from the top to bottom (from the first to the last block of flowchart). Runs exactly once. Example: 1) unlock the door 2) open the door 3) enter the room 4) switch on the light 5) close the door behind you 2. Selections (also known as decisions) – Is used to make choices based on information. Instead of following a specific order of events, they ask a question in order to figure out which path to take next. An algorithm can be made more intelligent or jump to different parts of the program. if if-else switch (with cases) The previous example about entering the room could be changed to consider for different conditions. For instance, it could change to:
SKILL MODULE: DIGITAL STEM 9 1) IF the door is locked, then unlock the door, ELSE do nothing (go to next instruction) 2) IF the door is closed, then open the door, ELSE do nothing 3) Enter the room 4) IF the room is dark, then switch on the light, ELSE do nothing 5) Close the door behind you The sequence of actions is carried out is selected based upon the information provided. The only way for a computer to discover the circumstances is to collect inputs and compare it to known values. In this case, known values would be values such as 'locked' or 'unlocked', 'closed' or 'open'. The computer looks at the door and checks to see if its current state matches 'closed' and 'locked'. If it matches 'locked', the door needs to be unlocked. Otherwise, nothing should be done. *CASE The selection IF-THEN-ELSE is useful if the choices are binary, such as 'locked' or 'unlocked' ('yes' or 'no'). The alternative to the IF-THEN-ELSE structure is the CASE structure. Using CASE, the algorithm searches a list of options until it finds the correct condition. For example, on a multiple-choice quiz, the answers might be A, B or C - the correct condition is either A, B or C.
SKILL MODULE: DIGITAL STEM 10 Basically, IF-THEN-ELSE looks for the correct option from two choices where CASE looks at the correct option from multiple choices. 3. Loops (iteration) - The process of repeating sections of a program to achieve a particular target or goal. for for-each (also known as range-based) while do-while Computer programs can use different types of loops; infinite loops (repeat forever), countcontrolled loops (repeat a number of times) and condition-controlled loops (repeat until something happens). a. Infinite loops A sequence on an infinite loop keeps repeating until the user terminates the program or it crashes. For example, on a desktop computer, the operating system has a program that continually checks your USB ports to see if you have plugged in a device. Even after you have plugged in a device, it still continues to check for any new devices. b. Count-controlled loop Count-controlled loops are used to make a computer do the same thing a specific number of times. The count-controlled loop can be described as a FOR loop. The program repeats the action FOR a number of times. The count-controlled loop can be described as the FOR-NEXT structure. The program repeats the action between the FOR and NEXT number of times. c. Condition-controlled loops A program could be made more intelligent by programming it to avoid hazards. For example, if the robot vehicle is 3 cm from the edge of the table and you tell it to move forwards 5 cm, it will drive off the edge of the table. To stop this from happening, you might write a conditioncontrolled loop like this: move forward repeat until (touching table edge) Condition-controlled loops can be used to add a high degree of intelligence to a computer system.
SKILL MODULE: DIGITAL STEM 11 WHILE Condition-controlled loops are also called WHILE loops or WHILE-ENDWHILE statements. A WHILE loop code is repeated based on a certain condition. The condition could be 'true' or 'false'. The WHILE loop executes while a condition is true. Whether the condition is met or not is checked at the beginning of the loop. If the condition is 'true' it repeats, if not then the code is not executed. DO WHILE A similar condition-controlled loop is a DO WHILE loop. This method differs from a WHILE condition-controlled loop in that the condition is checked at the end of the loop. If the condition is ‘true’, the loop repeats. Thus, the code in the loop is executed at least once.
SKILL MODULE: DIGITAL STEM 12 Infinite loops Condition-controlled loops can result in intentional or unintentional infinite loops. If we wanted to loop indefinitely, we could set a condition that would never be met, thus iterating infinitely. The following examples would result in an infinite loop: WHILE: count = 1 while count <>0: print(count) count +=1 DO WHILE: count = 1 do print(count) count+=1 while count <>0 REPEAT UNTIL: count = 1 repeat print(count) count+=1 until count =0 It is important to check that the conditions we set can be met if we wish them to be. Differentiating between the four types of loops A count-controlled loop (FOR loop) iterates the number of times specified in the loop. With a WHILE condition-controlled loop the iteration occurs indefinitely as long as the specified condition is being met. If the condition is already met, the code in the loop is never executed. With a DO WHILE condition-controlled loop the iteration occurs indefinitely as long as (or until) the specified condition is met. The code in the loop is executed at least once.
SKILL MODULE: DIGITAL STEM 13 With a REPEAT UNTIL condition-controlled loop the iteration occurs indefinitely until the specified condition is met. The code in the loop is executed at least once. An infinite loop can occur with all condition-controlled loops but not with a count-controlled loop. https://www.bbc.co.uk/bitesize/guides/zrxncdm/revision/7 Exercise 1. Label the name of each flowchart structure. 2. Write pseudocode of SOP to enter the restaurant a. Based on Sequential structure b. Based on Selections structure (e.g. body temperature cannot exceed 37.5 ⁰C) c. Based on Loops structure (e.g. mysejahtera apps ) 3. Repeat Exercise 3 for: a. Manual/Smart Light. b. Making an omelette c. Boil water using Electronic Kettle d. Calculate character “a” in word “kebangsaan”. Algorithm: a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer Pseudocode: Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is a "text-based" detail (algorithmic) design tool. The rules of Pseudocode are reasonably straightforward. All statements showing "dependency" are to be indented. Flowchart: A flowchart is simply a graphical representation of steps. It shows steps in sequential order and is widely used in presenting the flow of algorithms, workflow or processes. Typically, a flowchart shows the steps as boxes of various kinds, and their order by connecting them with arrows. A B C
SKILL MODULE: DIGITAL STEM 14 3.0 Visual Programming: mBlock 3.1 THE INTERFACE The user interface (UI) for mBlock is distributed into five major sections/area as follow. 1. Menu/Toolbar No. Function Description 1 Language Change the UI language. 2 File Create, open, save a project, import a project from your PC, or export your project to PC. 3 Edit Turn on/off the stage turbo mode or hide/unhide the stage. 4 Title Set or change the title of the current project. 5 Save Save the current project to My Projects. 6 Publish Publish the current project to the mBlock community. 7 Courses Visit the website for courses available. 8 Tutorials View the online mBlock 5 help documents and example programs. 9 Feedback Give your feedback to us. Your experience is what we care about. Any feedback is welcome. 10 Sign up/ Sign in Sign up an mBlock account or sign in. After signing in to mBlock 5, you can click it to view your projects, profile, account center, and cloud service authentication code, or click to sign out. 4. Stage 5.Panels 3.Scripts 1.Menu/Toolbar 2.Blocks 1 2 3 4 5 6 7 8 9 10 11
SKILL MODULE: DIGITAL STEM 15 11 Python Editor Enter mBlock-Python Editor. 2. Blocks Blocks grouped by category with different color. 3. Scripts Construct and compile program by dragging blocks to this area. 4. Stage Present designs/animation (sprites) and test the program. 5. Panels Edit the designs (edit sprites and background in stage) and connect to devices (microcontroller). https://www.yuque.com/makeblock-help-center-en/mblock-5/ui-navigation
SKILL MODULE: DIGITAL STEM 16 3.2 SPRITE PROGRAMMING Lets learn how to program the sprite. Sprite is a computer graphic which may be moved on-screen and otherwise manipulated as a single object. i. Add a Sprite a. Click Sprites under the stage and click + to add a sprite. Note: The default sprite is Panda, and you can click× in the upper right corner of the sprite to delete it. b. Choose the sprite in the Sprite Library dialog box appears and click OK. Choose Bee1.
SKILL MODULE: DIGITAL STEM 17 c. Bee1 sprite appears on the stage.
SKILL MODULE: DIGITAL STEM 18 ii. Move the Sprite a. Drag the Events block to the Scripts area. b. Drag the Motion block and drop it under the current blocks. c. Click the green flag under the stage to run it. Bee1 moves.
SKILL MODULE: DIGITAL STEM 19 iii. Add Sounds a. Click Sounds on the Sprites tab. b. Click Add Sound in the lower left corner.
SKILL MODULE: DIGITAL STEM 20 c. Choose the sound in the dialog box appears, and click OK. d. The sound choose appears in the sound list. Click × to return to the editing page.
SKILL MODULE: DIGITAL STEM 21 e. Drag the Sound block and drop it under the current blocks, and then choose “Chee Chee” from the drop-down list box. (6) Click the green flag in the lower right corner of the stage and see what happens.
SKILL MODULE: DIGITAL STEM 22 iv. Add a Costume a. Click Costumes on the Sprites tab. b. Click Add Costume in the lower left corner.
SKILL MODULE: DIGITAL STEM 23 c. Choose the costume in the dialog box appears, and click OK. d. The costume choose appears in the sound list, and can be modified. Click × to return to the editing page.
SKILL MODULE: DIGITAL STEM 24 e. Drag the Looks block and drop it under the current blocks. e. Click the green flag in the lower right corner of the stage and observe what happens. Activity 1: Bouncing ball Able to add sprite, move and control it. Using forever loop control.
SKILL MODULE: DIGITAL STEM 25 Activity 2: Bouncing ball with a static obstacle Able to add sprite custom made sprite and control it. Using single if loop control. Activity 3: Bouncing ball with a moving obstacle Able to control sprite to move it by key board.
SKILL MODULE: DIGITAL STEM 26 Activity 4: Complete bouncing ball game Able to include additional control using multiple if loop. Complete the program with the following scripting. Edit sprites “Basketball” and “Game Over”.
SKILL MODULE: DIGITAL STEM 27 Review Questions: 1. How to adjust speed of the bouncing ball? 2. How to come out custom made shape sprite? 3. What game can you think you can make using mBlock?
SKILL MODULE: DIGITAL STEM 28 4.0 Programming Arduino 4.1 ARDUINO BOARD Arduino is an open-source computer hardware and software company, project, and user community that designs and manufactures single-board microcontrollers and microcontroller kits for building digital devices and interactive objects that can sense and control objects in the physical and digital world. Microcontroller is a compact integrated circuit designed to govern a specific operation in an embedded system. It is a micro-computer that can control and carry out data analysis. Famous microcontroller manufacturers are MicroChip, Atmel, Intel, Analog devices, and more. A microcontroller board, contains on-board power supply, USB port to communicate with PC, and a microcontroller chip. In circuit serial programming (ICSP) Power Supply Analog Inputs 5 V / 3.3 V / GND External Power Supply 9 V to 12 V (≥ 250 mA) USB Programming Port (Type B) Main MCU Atmega328 Reset Button Digital Output ~ : PWM 0,1 : Serial Port (Rx, Tx) Digital ground pin Analog Ref. pin
SKILL MODULE: DIGITAL STEM 29 In order to design the system, we need a tools or software to communicate with the microcontroller (Arduino). We can instruct the Arduino to do automation system. Figure 1 shows the overall Arduino environment in general. Figure 1: Arduino Environment There are many ways to program the Arduino with different software features. Many software is widely available as open-source such as mBLOCK and nodeRED other than Arduino IDE itself.
SKILL MODULE: DIGITAL STEM 30 4.2 PROGRAM ARDUINO UNO USING MBLOCK 5 mBlock 5 can be used to program hardware devices as well as sprites. This section uses Arduino UNO board as an example. First, let’s think about how the flowchart look like for each activity. Activity 5: Blinking LED Components: 1 x Arduino UNO 1 x USB Type-B Cable Male-male jumper wires 1 x LED 1 x 220 Ohms Resistor 1 x Bread Board Figure 1: Arduino UNO wiring diagram for Blinking LED. Hardware Procedure: a. Connect all components according to wiring diagram in Figure 1. b. Positive (+ / anode) of LED to 220 ohms resistor and digital Pin 12 on Arduino. Negative (- / cathode) of LED to pin GND of Arduino. Figure 2: LED terminals. GND 5V Pin 12
SKILL MODULE: DIGITAL STEM 31 mBlock Procedure: a. On the mBlock interface, click on add devices to add Arduino UNO. Click on add button. Scroll and choose Arduino Uno by clicking on it. Then, click ok.
SKILL MODULE: DIGITAL STEM 32 b. Start first block scripting of the Arduino UNO board on the script area. c. On the Block Script menu, click Events. d. Choose when Arduino Uno starts up by dragging the block script and drop to the script area. Block script menu Script area
SKILL MODULE: DIGITAL STEM 33 e. On the Block Script menu, click on Control. f. Choose forever loop by dragging the block script and drop under when Arduino Uno starts up block.
SKILL MODULE: DIGITAL STEM 34 g. On the Block Script menu, click on Pin. h. Choose block, drag and drop it in forever loop. Change the pin value 9 to pin 12. Set the output as low. i. On the Block Script menu, click Control. j. Choose wait 1 seconds, drag and drop to script area. k. Arrange all the block as follow. l. Right click on the set digital pin’s block scripts, click on Duplicate to copy the block script set. Drop the block script set copied. Right click block, select Duplicate.
SKILL MODULE: DIGITAL STEM 35 m. Complete the whole block script as follow. n. Setup the connection to Arduino board. Make sure Upload button is set. Then, click Connect button.
SKILL MODULE: DIGITAL STEM 36 o. Connection is successful. Arduino now connected to mBlock software and can be programmed. p. Upload the block script by clicking Upload button. Tick Show all connectable devices. Check the Arduino connected to the serial port (USB) available and click Connect. Click Upload to upload block script.
SKILL MODULE: DIGITAL STEM 37 q. The Arduino Uno has been programed. Observe the outcome from the circuit. Try adjust by changing the number, reupload the block script and observe the output.
SKILL MODULE: DIGITAL STEM 38 Activity 6: ON and OFF the LED with keyboard keys a. On the mBlock panel, click Sprite. From block script menu, click extension. b. Select and click + Add for Upload Mode Boardcast extension. i) Click Sprite. ii) Then, click extension.
SKILL MODULE: DIGITAL STEM 39 c. On the mBlock panel, click Sprite. From block script menu, click Events and choose by dragging the block script and drop to the script area. d. On block script menu, click Upload Mode Broadcast and choose by dragging the block script and drop to the script area. Edit the statement by replacing “message” with “ON”. iii) Drag and position the block here. Edit statement to ON. i) Select Upload Mode Broadcast. ii) Choose this block.
SKILL MODULE: DIGITAL STEM 40 e. Duplicate the first block scripts and change space to alphabet a and edit the upload message to OFF. f. Select Devices in mBlock panel and click Arduino Uno. From block script menu, click extension. Select and click + Add for Upload Mode Boardcast extension (same as step b.). ii) Change to character a. iii) Edit statement to OFF. i) Right-click, and select duplicate.
SKILL MODULE: DIGITAL STEM 41 g. Select Upload Mode Broadcast. i) Choose this block. iii) Edit statement to ON. i) Select Upload Mode Broadcast . iv) Select Pin. v) Choose this block. iii) Set the output as HIGH.
SKILL MODULE: DIGITAL STEM 42 h. Complete the block script as follow. i. Upload the block script by clicking Upload button. Now we can control LED from keyboard. Exercise 1: Improve the program in Activity 6 Improve the block script so that the LED can be control with three options. 1. ON – turn ON the LED 2. OFF – turn OFF the LED 3. Blink – set the LED ON for 1s and OFF for 2s.
SKILL MODULE: DIGITAL STEM 43 Exercise 2: Create a mood lights based on the flowchart Components: 1 x Arduino UNO 1 x USB Type-B Cable Male-male jumper wires 3 x LED (different color) 3 x 220 Ohms Resistor 1 x Bread Board Circuit Diagram: Blue LED Green LED Red LED Key A ON OFF OFF Key S OFF ON OFF Key D OFF OFF ON Flowchart: Note: This flowchart still can be improved. 3 5 6
SKILL MODULE: DIGITAL STEM 44 Activity 7: Control LED using buttons in mBlock (virtual buttons) Components: 1 x Arduino UNO 1 x USB Type-B Cable Male-male jumper wires 1 x LED 1 x 220 Ohms Resistor 1 x Bread Board Circuit Diagram: Figure Activity 7: ON and OFF using button in mBlock Hardware Procedure: a. Connect all components according to wiring diagram in Figure. b. Positive (+ / anode) of LED to 220 ohms resistor and digital Pin 12 on Arduino. Negative (- / cathode) of LED to pin GND of Arduino.
SKILL MODULE: DIGITAL STEM 45 mBlock Procedure: a. Select Background from mBlock panel. Click on + sign to add background to mBlock user interface in Stage. b. We can choose any available background or we can create custom made background by clicking My Backdrops. Choose any background and click ok. i) Select Background. ii) Click this add button.
SKILL MODULE: DIGITAL STEM 46 c. The background should have changed with the one we selected. To adjust view of the stage, select one of the buttons below the stage. d. Now, we need to create our button in the stage. Click add button in Sprite panel. Options to view stage. Click add button.
SKILL MODULE: DIGITAL STEM 47 e. In Sprite library, click on Icons. Choose any button you like. In this example we choose Empty button14 and click OK. Get one more button with the same design. f. We can change the sprite button name. Let’s name it on button and off button respectively. We can edit the label on the sprite buttons by clicking Costumes. Select button design and click OK. Select button design and click OK. i) Edit name for the sprite button. ii) Redesign the button by clicking Costumes for selected sprite .
SKILL MODULE: DIGITAL STEM 48 g. Select on button sprite and click Costumes. Click on Text symbol and locate cursor on button. Type in “ON”. We can edit the font such as color and type. h. Edit color of on button by click Select editor and click the button. Change Fill color to green. Click X button to complete the sprite editing. i) Select on_button sprite. iii) Type in “ON” for this button ii) Select Text editor. i) Click Select editor. ii) Select green color in Fill. iii) Click X to close editing.
SKILL MODULE: DIGITAL STEM 49 i. Repeat the same process (step g and h) to edit the off button. Here how the stage and sprites look like. j. Click on-button sprite in Sprite panel. Click Events in Script Block. Drag the block in scripting area. ii) Select Events blocks. iii) Drag and drop the block. i) Click on-button sprite in Sprite panel.