Showing posts with label Procedures. Show all posts
Showing posts with label Procedures. Show all posts

Saturday, February 28, 2015

A Scratch Project for Drawing Tangrams and Compound Figures


Here is my Scratch project that draws two sample tangrams and the compound figures from this assignment (using Pro-Bots) created for grade 5 students.

I have created 5 different procedures for drawing the 5 components of the tangram. Please note that this is just one way of coding the figures; there are multiple correct ways of writing the programs for the same.


The functions (procedures) that are used for drawing the various polygons in the tangram can be found under the blocks titled:
  • parallelogram
  • square
  • largeTriangle
  • medTriangle
  • smallTriangle

The functions (procedures) for the compound figures that use the square and the triangle can be found under the blocks titled:
  • Square+Triangle
  • Square+Triangle_2

The functions (procedures) for the compound figures using the parallelogram and the small triangles can be found under the blocks titled:
  • TriParall_1
  • TriParall_2

I have given the code for two tangrams here as samples: one is the Monk with No Foot from the famous "Two Monks Paradox" and the other is a tangram for a House. The functions (procedures) for these can be found under the blocks titled:
  • MonkNoFoot
  • HouseTangram



Note:   The various dimensions in the assignment for Pro-Bot have been multiplied by a factor of 10 in my Scratch project, as we are dealing with pixels here and not centimeters, as in the case of Pro-Bot. 


Monday, August 18, 2014

Sensors in Pro-Bot: Relay Driving by Pro-Bots using Sensors

Pro-Bot has three kinds of sensors built into it: touch, light and sound sensors. Before we start writing programs for Pro-Bot that use the sensors, let’s see what purpose these sensors serve and what “sensing” means.

Sensing 


When someone taps your shoulder, how do you know you were touched? When the light bulb goes on in a dark room, how do you know the room suddenly got bright? When you put a candy in your mouth, how do you know that it is sweet? Because your skin sensed the touch, or your eyes sensed the light, or your tongue sensed the taste… Once you sense something, you typically react to it, don’t you? When you sense the touch, you may turn around to see who tapped your shoulder; when you sense the light coming on in the dark room, you may squint your eyes and try to figure out what is in that room; when you sense the sweetness on your tongue, you may feel happy and say “yummy”…

Your skin, eyes, tongue, etc., have “sensors” that sense some “stimulus” like touch, light, taste, etc., and enable you to respond to it. You may have also noticed that you have different sensors for different functions. Sensors are made to detect very specific stimuli. For example: your skin doesn’t see, you have eyes to do that; your eyes don’t taste the sweetness of the candy, you have taste buds on your tongue to do that.

Now what if a robot could behave similarly (it may not behave in exactly the same ways as you do)? A robot can be fitted with sensors and programmed to respond in a certain way when the sensor senses a stimulus.

Pro-Bot has 3 kinds of sensors - one senses light, one senses contact (or touch) on its front and rear bumpers and the other senses sound. Pro-Bot’s sensors must be turned on, if you want them to detect stimuli and respond to them (they are switched off by default). Think of it as needing your eyes to be open to see the light. The sensors detect only the specific stimuli that they are designed for. For example, the touch sensor or the light sensor on Pro-Bot will not detect or respond to sounds. They will respond only to touch and light stimuli respectively. However, short sharp sounds (like a loud clap or a short yell) may be detected by the sound sensor and you can program the robot to respond to it.

Now, how does Pro-Bot react when these sensors sense something? Consider the touch sensors on Pro-Bot’s front and rear bumpers. You can program Pro-Bot to do something when those sensors sense a contact (such as bumping into something, or getting bumped by something). Similarly, the light sensor on Pro-Bot can be programmed to do something when it detects a change in lighting.

Pro-Bot has 5 specialized Procedures that correspond to inputs from its sensors. These are 
33 FRONT
34 REAR
35 DARK
36 LIGHT
37 SOUND

You can access and modify the above Procedures via the Menu button on the control pad. The instructions in each of these Procedures will be executed when the the corresponding sensor detects a stimulus. If the Procedure corresponding to the sensor is empty (if you decide not to react to a stimulus), Pro-Bot does not respond to changes in the sensor condition. 
So, what happens after Pro-Bot has responded to a stimulus? Before you answer that question, consider this scenario: Imagine that you are sitting in your chair and reading a book. Your friend comes over, taps you on the shoulder and asks you something. Your skin’s touch sensor senses the tap, and your ears (another of your sensors), sense the spoken words. Maybe your friend was asking you to join her in a game. Let us say you respond saying “Later”. What do you do next? You would continue reading that interesting book, right? 
Let’s analyze what just happened. You were doing something… then you got “interrupted” by your friend… you “handled” that interruption… and then you got back to doing your reading… A computer or a robot can react the same way. When its sensor detects a stimulus, Pro-Bot can react to it by running a specific program, and after it is done, Pro-Bot continues with what it was doing before the interrupt happened. For example, if Pro-Bot was driving and midway, it entered a dark tunnel, it would detect the change in light and may turn on the headlights (if you programmed it to respond that way) and after that, it would continue driving along. After it is done with the response to the stimulus, Pro-Bot resumes the steps in the main program. 

What you have learned above is a fundamental behavior in Computer Science and Robotics: handling interrupts

Let us test our understanding now with an assignment that uses sensors.



Relay Driving by Pro-Bots using Touch Sensors:


Computer Science Concepts involved:   Procedures, Sensors to detect and react to stimuli, a quick peek into Interrupt handling

Math Concepts Involved:   Linear measurements, Solving real world problems by modeling with mathematics

Grade Levels:   3, 4, 5

Hours Required:   1

Materials Required:   A pre-set path drawn for the Pro-Bot to drive on, preferably marked with blue tape. Optionally, a shoebox with one vertical side cut open to act as a garage for Pro-Bot


Programming Assignment:


Let’s look at a simple task to start off with, involving 2 Pro-Bots. The steps are listed below.
  1. Mark a path on the floor with blue tape that is about 40 cm long. You can also mark a target finish line at the end of the path.
  2. Place one Pro-Bot at the beginning of the path, facing forward and ready to drive along the path. 
  3. Place the second Pro-Bot at approximately the midpoint of the path, 20 cm away from the start point, facing forward and ready to start driving. (Both cars face the same direction.)
  4. Optionally, place a ‘garage’ (made out of an upside down shoe box with one side cut for the car to enter) at the very end of the path.
  5. Make sure that the sensors are set to "On" from the Menu button on Pro-Bot.
  6. Program your Pro-Bots so that the first car starts driving along the path while the second car is waiting. The first car hits the back of the second car, makes a beep sound and stops. The second car now starts driving. It drives all the way into the finish line/ garage. It makes a beep sound and stops. (Optional step: when it gets inside the garage, it switches its headlights on.)


Now, how can we program the two Pro-Bots to do this?
  • For the first Pro-Bot, the task involves driving forward, say 20 cm, to reach the second car and then reacting to a touch on the front bumper when it hits the second Pro-Bot. So, your program for the first Pro-Bot has to be split up into two parts - the Main program that handles the driving forward part and Procedure 33 FRONT that handles the contact to the front sensor. You will have to edit Procedure 33 FRONT to make the beep sound & add in a few Pause instructions to make the car stop.
  • For the second car, driving starts only when it gets hit on the rear bumper. To make the car wait, your Main program shall have a few Pause instructions in a loop. You would also need to modify Procedure 34 REAR to make the car react to the hit on the rear bumper and drive to the finish line/ garage. Optionally, once inside the garage, the light sensor can detect the darkness and respond to it; for this modify the procedure 35 DARK to switch on the lights.

Once the two Pro-Bots have been programmed, press the GO button on both cars at the same time and watch the relay race happen! Here is a sample set of programs, for two Pro-Bots kept at a distance of 20 cm from each other and the finish line at a distance of 20 cm from the second Pro-Bot:



Pro-Bot 1:

  • Main - Fd 20

  • 33 FRONT -  Sound 3
                              Rpt 20 [
                              Ps
                              ]

Pro-Bot 2:

  • Main - Rpt 20 [
                   Ps
                   ]

  • 34 REAR -  Fd 20
                           Sound 3

  • Optionally, 35 DARK - Light On

Note:   This project can be easily extended to include more cars and more complicated paths. Since we do not have a Stop instruction available, we can make the car stop by using the Pause instruction in a loop. 


Extension


If working on the relay race project with 3 or more cars, all the cars other than the first and last ones would need to handle both their front and rear touch sensors. Let’s look at an example with 3 cars, assuming they are kept at a distance of 20 cm each. In this case, the first car would behave as Pro-Bot 1 above and the last car would behave as Pro-Bot 2 above. Here is a sample set of programs for the 3 car relay race:


First  Pro-Bot :

  • Main - Fd 20

  • 33 FRONT -  Sound 3
                              Rpt 20 [
                              Ps
                              ]

Middle Pro-Bot:

  • Main: Rpt 20 [
                  Ps
                  ]

  • 34 REAR -  Fd 20
                           Sound 3
                                
  • 33 FRONT -  Sound 3
                              Rpt 20 [
                              Ps
                              ]

Last Pro-Bot :

  • Main - Rpt 50 [                     // Remember to make the Pro-Bot wait longer 
Ps // here to allow for the other two cars to catch up.
                   ]

  • 34 REAR -  Fd 20
                           Sound 3

  • Optionally, 35 DARK - Light On




Sunday, August 17, 2014

Nets of 3D Shapes Part 2 - Prisms & Pyramids: More Practice with Procedures using Pro-Bot

This programming assignment is intended to provide more practice with Procedures using Pro-Bot. We shall use Procedures to store programs for various polygons. We shall then write programs for Pro-Bot to draw nets of some 3-Dimensional figures using these Procedures. 
We already worked on the nets of cubes in our previous assignment,  Nets of 3D Shapes Part 1 - Cubes. This is a follow-up to that work, involving more complex polyhedrons. 


Computer Science concepts involved:   Sequential programming, Repeat loops, Procedures

Math concepts involved:   Polyhedrons (prisms, pyramids), Nets of 3D figures (visualizing 3D figures on a 2D plane, identifying multiple nets, properties of nets), Polygons (triangles, quadrilaterals, pentagons), Measurement, Angles

Material required:  Card paper/thin cardboard to draw the nets on

Extension activity:   Make the 3D figures by cutting out the net from the card paper and folding along the edges 

Grade levels:   5

Hours required:   2 or more



Nets of 3-Dimensional Figures


A 3-Dimensional (3D) shape is a shape that has length, width and depth. They are also called solid figures or solid shapes. The length, width and depth are the three dimensions. Most of the objects that we see around us are 3-Dimensional. For example: your books, school bag, a box of crayons, Pro-Bot, table, chairs, water bottle, soccer ball, even yourselves are all 3D shapes.

How do these shapes differ from 2-Dimensional (2D)  figures, like the ones that you draw on paper? Think about how a cube or a sphere differs from a square or a circle drawn on paper. Well, the difference is that they have depth, unlike the 2D figures drawn on paper, which have only length and width. 3D shapes do not lie flat on a plane surface and they are difficult to draw on a piece of paper. 

But what if we could open up the 3D shapes and lay them out flat on paper? This would show us exactly how these solid shapes are made. A net can help us convert a 3D shape to a 2D figure. Nets are the flattened shapes of 3D objects. The net shows every edge and every face of the 3D figures laid out flat on paper. The net has only length and width; it does not have depth. It makes it easier for us to study and analyze some of the properties of a 3D object. You can cut out the net from the paper and fold it along the edges to create the 3D object. The same 3D object may be flattened into more than one net.

Let’s look at a few 3D shapes and draw their nets. We shall use Pro-Bot to draw the nets on thin cardboard. You can then cut out the nets and fold them to create the 3D shapes.



Nets of Prisms:


A prism is a solid object whose bases (or ends) have the same size and shape and are parallel to each other. The sides of the prism are parallelograms. A prism has the same cross-section all along its length. The shape of the bases (or ends) give the prism its name. We shall look at two types of prisms here:
  • Rectangular prisms
  • Triangular prisms


Nets of Rectangular Prisms:


Rectangular prisms are very commonly seen in our daily lives; for example, boxes, books, buildings, etc. A rectangular prism has 6 rectangular faces, 12 edges and 8 vertices. Opposite faces are congruent and have the same dimensions.  All faces of the rectangular prism are rectangles. A cube is a rectangular prism where all rectangular faces have equal edges.

Given below is a rectangular prism and one of its nets. The bases of this prism are square in shape. Hence it is also called a square prism






















  1. Write a program for Pro-Bot to draw the net given in the figure. Store the program for each rectangular face as a Procedure. (If there is a procedure for the 6 cm side square that you have already written, it can be used in this program as well.) 
  2. Can you come up with more nets for the above prism. Try at least one other net and write a program for Pro-Bot to draw it. 
  3. Once you are done drawing each net using the Pro-Bot, cut out the net from the paper. You can fold the paper along the edges and create the prism.
  4. Given below is another rectangular prism. Write a program for Pro-Bot to draw a net for this prism. 
  



Nets of a Triangular Prism:


In the figure below, you can see a triangular prism, with a shape that resembles a camping tent. It has 5 faces, two of which are equilateral triangles and three are rectangles. 




















  1. Write a program for Pro-Bot to draw an equilateral triangle of sides 6 cm and store it as a Procedure on Pro-Bot. Remember to use Repeat Loops when you are working with regular polygons (polygons with all edges equal and all angles equal).
  2. Next, construct a net for this triangular prism. Write a program for Pro-Bot to draw the net, using the Procedures for the rectangle and the triangle that you previously wrote.
  3. How many different nets can you draw for this triangular prism? Write a program for Pro-Bot to draw each net that you can identify for this prism.


Nets of Pyramids:


The word “pyramid” immediately brings to mind the royal tombs of ancient Egypt. The pyramids of Egypt are square pyramids, with a square base and triangular sides. There are various types of pyramids; they are named according to the shape of the base. A pyramid is made by connecting the base to the apex (top point where all the triangular sides meet). 

In this assignment we shall look at three regular pyramids, which have regular polygons as their base: 

  • Triangular Pyramid
  • Square Pyramid
  • Pentagonal Pyramid

Net of a Triangular Pyramid/Tetrahedron:


A triangular pyramid is also known as a tetrahedron ("having 4 faces"). A tetrahedron has 4 triangular faces, 4 vertices and 6 edges. Three triangular faces meet at each vertex. 

In the figure below, a tetrahedron with 6 cm edges is given. All the triangular faces are equilateral triangles, with sides of 6 cm. 





















  1. Can you write a program for Pro-Bot to draw a net for the tetrahedron, using the Procedure for the equilateral triangle of sides 6 cm that you wrote earlier? 
  2. How many nets can you create for the tetrahedron?
  3. After drawing the net using Pro-Bot, you can cut the net out of the paper and fold it along the edges to build the 3D tetrahedron. 

Net of a Square Pyramid:


A square pyramid has 5 faces, 5 vertices and 8 edges. The base is a square and the other faces are triangles. In the figure below, a square pyramid with all edges measuring 6 cm is given. 






















  1. Can you write a program for Pro-Bot to draw a net for this pyramid, using some of the Procedures that you wrote earlier? 
  2. How many nets can you create for the square pyramid?
  3. After drawing the net using Pro-Bot, you can cut the net out of the paper and fold it along the edges to build the pyramid. 

Net of a Pentagonal Pyramid:


A pentagonal pyramid has 6 faces, 6 vertices and 10 edges. The base is a pentagon and the other faces are triangles. In the figure below, a pentagonal pyramid with all edges measuring 6 cm is given. 




















  1. Can you write a program for Pro-Bot to draw a net for this pyramid, using some of the Procedures that you wrote earlier? Remember to use Repeat Loops while writing the program for the pentagon. 
  2. How many nets can you create for this pyramid?
  3. After drawing the net using Pro-Bot, you can cut the net out of the paper and fold it along the edges to build the pyramid. 

Nets of 3D Shapes Part 1- Cubes: More practice with Procedures using Pro-Bot

This programming assignment is intended to provide more practice with Procedures using Pro-Bot. We shall use a Procedure to store the program for a square. We shall then write programs for Pro-Bot to draw nets of a 3-Dimensional object, a cube in this case, using the Procedure. 


Computer Science concepts involved:   Sequential programming, Repeat loops, Nested Loops, Procedures

Math concepts involved:   Cubes; Nets of 3D objects: visualizing cubes on a 2D plane, identifying multiple nets per cube, properties of nets of a cube; Squares, Measurement, Angles

Material required:  Card paper/thin cardboard to draw the nets on

Extension activity:   Make the cube by cutting out the net from the card paper and folding along the edges 

Grade levels:   3, 4

Hours required:   2 (or more)


Nets of 3-Dimensional Figures


A 3-Dimensional (3D) shape is a shape that has length, width and depth. They are also called solid figures or solid shapes. The length, width and depth are the three dimensions. Most of the objects that we see around us are 3-Dimensional. For example: your books, school bag, a box of crayons, Pro-Bot, table, chairs, water bottle, soccer ball, even yourselves are all 3D shapes.

How do these shapes differ from 2-Dimensional (2D)  figures, like the ones that you draw on paper? Think about how a cube or a sphere differs from a square or a circle drawn on paper. Well, the difference is that they have depth, unlike the 2D figures drawn on paper, which have only length and width. 3D shapes do not lie flat on a plane surface and they are difficult to draw on a piece of paper. 

But what if we could open up the 3D shapes and lay them out flat on paper? This would show us exactly how these solid shapes are made. A net can help us convert a 3D shape to a 2D figure. Nets are the flattened shapes of 3D objects. The net shows every edge and every face of the 3D figures laid out flat on paper. The net has only length and width; it does not have depth. It makes it easier for us to study and analyze some of the properties of a 3D object. You can cut out the net from the paper and fold it along the edges to create the 3D object. The same 3D object may be flattened into more than one net.

Let’s look at a very common 3D shape, a cube, and draw its nets. We shall use Pro-Bot to draw the nets on thin cardboard. You can then cut out the nets and fold them to create the 3D object.


Nets for a Cube


Have you seen the dice that you use for board games? It has the shape of a cube. A cube is one of the most common 3D figures, with 6 square faces, 12 edges and 8 vertices. 

What would the cube look like if you cut it open along some of its edges and laid it out flat on a piece of paper, so that you can see every face and every edge? The flattened version of the cube would be its net. There can be more than one net for a given 3D shape. Can you guess how many nets exist for a cube?

In the figure below are a couple of nets for a cube of sides 6 cm each. 




















You can see from the figure that each net is made up of multiple squares; each square representing a face of the cube. All the squares are similar, with edges measuring 6 cm each. If you fold the above nets along the edges/lines drawn in the figure, you would end up with a cube.



Programming Assignment


  1. Write a program to draw a 6 cm side square. Remember to use Repeat Loops in your program for the square. Store your program as a Procedure.   (Since the same square is used multiple times in each net, it would be easier for you as the programmer, to write the program for the square just once, store it in a Procedure and then call that Procedure from your main program whenever you need it.) 
  2. Write the programs for Pro-Bot to draw the nets for the cube as given in the figure above. Use the Procedure for the square that you previously wrote while writing the programs.
  3. There are 11 possible nets for a cube, two of which are given above. Can you identify the other 9 nets for the cube as well? 
  4. Write a program for Pro-Bot to draw each net that you identify, using the Procedure for the square in your program.
  5. Once you are done drawing each net using Pro-Bot, cut out the nets from the paper. Fold the paper along the lines drawn and create a cube from each net.You could even draw the numbers/dots on the six squares as seen on a pair of dice. 
  6. List the properties that seem to be common for the various nets that you came up with.
  7. Compare the area and perimeter of the different nets. 



Sunday, August 3, 2014

Programming Roman Numerals using Pro-Bot

The aim of this programming assignment is to use Procedures in Pro-Bot to draw Roman Numerals. 
Roman numerals, the numeric system used in ancient Rome, employs combinations of letters from the Latin alphabet to signify values. Numbers are formed by combining symbols and adding the values. There is no zero in this system. Roman Numerals, as used today, are based on seven symbols: 





















Symbols are placed from left to right in order of value, starting with the largest. To write the Roman numeral, each of the non-zero digits should be treated separately. However, in a few specific cases, to avoid four characters being repeated in succession (such as IIII or XXXX), subtractive notation is used as follows (Wikipedia):
  • the numeral I can be placed before V and X to make 4 units (IV) and 9 units (IX)
  • X can be placed before L and C to make 40 (XL) and 90 (XC)
  • C can be placed before D and M to make 400 (CD) and 900 (CM) according to the same pattern
Examples: 
  1. The numbers 1 to 10 expressed in Roman numerals are: I, II, III, IV, V, VI, VII, VIII, IX, X.
  2. The number 2014 expressed in Roman numerals is MMXIV (2000 = MM, 14 = XIV). 
  3. The number 1954 can be expressed using the subtractive notation: 1000 = M, 900 = CM, 50 = L and 4 = IV. Therefore, 1954 is MCMLIV.


Programming Assignment:    


Write programs for Pro-Bot to draw the symbols in the Roman Numeral system, according to the dimensions given. 
Write a program to draw each symbol. Test your programs on Pro-Bot to see if you get the desired shapes. Store the program for each symbol as a separate procedure in Pro-Bot’s memory. 
Next, draw the Roman Numeral representation for different numbers using Pro-Bot. Make calls to the procedures from your Pro-Bot program to draw the various components in the Roman Numeral representation. 
Note: Pro-Bot does not have the option of lifting and lowering the pen via programming instructions. Hence, after drawing each symbol, it is necessary to physically lift the unit and place it on the starting spot for the next symbol to successfully draw a multi digit number. 
This assignment could also work as a group project, where each student writes the procedures for 2-3 symbols each. The students can then take turns calling the procedures for the symbols in a multi-digit number.

Computer Science concepts involved:  Sequential programming, Procedures
Math Concepts involved:  Roman Numerals, Place value, Measurement, Angles 
Grade Levels:  3, 4, 5
Hours required:  2 or more



Lesson Plan:


Hours 1& 2:  
Introduce the Roman Numerals and their values. Write programs for Pro-Bot to draw each of the symbols according to the dimensions given below. Save each program as a Procedure on Pro-Bot. 

All dimensions are in centimeters. The angular measurements are in degrees.





















Hour 3: 
Find the Roman Numeral representation of multi-digit numbers of your choice. Call the procedures in Pro-Bot that you previously wrote to draw each symbol in the number.

Example: 
Write programs to draw the Roman Numeral representation of the following numbers using Pro-Bot. 
  1. 123 
  2. 49
  3. 490
  4. 1056

Thursday, July 31, 2014

Solutions: Art with Pro-Bot - A Soccer Game

The soccer game picture is a follow-up to the "Art with Pro-Bot: A Dancing Robot" exercise, with one of the figures being the exact same as the Dancing Robot and the other being its mirror image.


Let's name the figure on the left as Team A and the one on the right as Team B. You would recognize from the figure that both Team A and Team B share some parts in common, such as the head and the neck. So, it's easier for the programmer to write the code for these parts just once and store it as a procedure; it can then be used multiple times. 

// Proc 1 - procedure for the neck and head


Fd 1
Lt
Fd 1
Rt
Rpt 3 [
Fd 2
Rt
]
Fd 1





Team A:  


// Program written in Main and using the above procedure. The Start point is marked in the figure.


Rpt 4 [
Fd 10
Bk 4
Rt
]
Fd 3
Lt
Proc 1


Team B:


// Program written in Main and using the above procedure. The Start point is marked in the figure.


Rpt 4 [
Fd 10
Bk 4
Lt
]
Fd 3
Rt
Proc 1


Art with Pro-Bot: A Soccer Game


Let’s use Pro-Bot to draw a picture of robots playing soccer.

We shall work using both Repeat Loops and Procedures in Pro-Bot for this assignment. This "art" project was designed to use the concepts of Repeat Loops and Procedures in a fun way.

There are 2 robots in the picture, one representing Team 1 and the other, Team 2. The two robots are playing each other in a game of soccer. The dimensions are as follows: Each robot's body is a 6 cm side square, the limbs are 4 cm long, the neck is 1 cm long and the head is a 2 cm side square. 

While writing the programs for Pro-Bot to draw the figures, remember to use Repeat Loops for patterns that repeat. You can also see from the pictures that the head and neck is exactly the same for both pictures. Hence, you can write the program for the head and neck once, store it as a procedure in Pro-Bot's memory and call it for drawing both robots. 

Click here for a set of solutions.

Draw in as many members for each team as you like. You can even store the programs for each robot as separate procedures which makes it easier for you to draw multiple figures in your art project. You can put in a background of your choice and draw a soccer ball in the middle once you have drawn both teams. It does not have to be just soccer; students in my class have used this theme to depict scenes from various sporting events. We worked on this project the day after a major basketball game and there were quite a few children who made pictures with basketball scenes... Let your imagination run free in creating your art work.