Scratch: An Introduction to Computer Programming Activity

If I was to ask you to explain what is happening in this computer program, do you think you could do it? Give it a try by yourself!

;Chip Settings

#chip 16F88,4

#config LVP=OFF, MCLR=OFF, WDT=OFF, OSC=INTRC_IO

;Defines (Constants)

#define SendAHigh SET PORTB.7 OFF

#define SendALow SET PORTB.7 ON

#define analog_reading_1 left_LDR

#define analog_reading_2 right_LDR

#define LCD_IO 4

#define LCD_RW PORTB.1

#define LCD_RS PORTB.0

#define LCD_Enable PORTA.6

#define LCD_DB4 PORTB.2

#define LCD_DB5 PORTB.3

#define LCD_DB6 PORTB.4

#define LCD_DB7 PORTB.5

;Variables

Dim analog_reading_1 As word

Dim analog_reading_2 As word

Dim left_LDR As word

Dim right_LDR As word

Dim Error1 As word

Dim analog_reading_3 As word

Dim whole_number As word

Dim remainder As word

Set PORTA.3 Off

Do Until Error1 < 1005

analog_reading_1 = ReadAD(AN0)

analog_reading_2 = ReadAD(AN1)

analog_reading_1 = analog_reading_1 + 1000

Error1 = analog_reading_1 - analog_reading_2

Dir PORTA.3 Out

'this time the servo wants to move to the extreme left until the dead spot

PulseOut PORTA.3, 900 us

wait 20 ms

SerPrint 1, " second "

SerPrint 1, Error1

CLS

Print "SECOND="

Print Error1

Loop

I bet you found it difficult, if not impossible, to even understand the program let alone explain what is happening. There has to be a better way to learn computer programming!

Well.... why don’t we take a step back and break down the process into several smaller and easier steps that we can understand and eventually allow us to expand our programming knowledge.

To understand a computer language, you first have to know that information flows in a program in a certain manner, according to the logical answers to simple questions. The best way to see how a computer program works is to show the flow of information in a visual based diagram called a flow chart. Take a look at the example below and see if you can understand the process.

With the use of the symbols diagram below and the previous example, try to create your own flow chart for a simple process that you are very familiar with. Be creative, if you need further inspiration, check out Sheldon’s flow chart for making friends. http://www.youtube.com/watch?v=k0xgjUhEG3U

Believe it or not, you have just made your first program! OK, it might not actually do anything, but you now understand how a basic program is constructed and without a solid framework, your program will not run as intended. Your flow chart is very close to the visual style of computer programming that Scratch uses and you are now ready to try out the next level of difficulty in programming.

The next activity will allow you to learn how to create your first actual computer program that will make an animation in Scratch!

Scratch is an open source (free to use, share, modify) visual-based programming software that allows users to create their very own animations, interactive art, music, and video games. Scratch is a very powerful program that allows students from Elementary to Harvard University to learn how to program and create programs that range from simple animations all the way to complex systems that can be used to control motors, physics calculations, and advanced logic systems.

Let’s get started!

1)  Open up scratch and immediately save your program to your last name-animation. Example Jurica-animation

2)  The first thing you need to know is that the HELP tab on the menu will answer many of your questions on how to use scratch or if you need examples and if you right click on any item in Scratch a specific help screen will show you what that item is used for and how to use it.

3)  The lower right corner is your sprite area and a new custom sprite can be drawn up or a sprite can be inserted from the file. You can input your own photos too! For now let’s open up bat-1a from choose a sprite from file/animals/bat-1a. Right click and delete the cat sprite.

4)  Now we are going to add a costume to the bat. Left click on the tab for costumes (top center of screen) and click on import. This time select bat-1b.

5)  Click on the control block in the top left hand corner in the blocks palette. Left click and hold down to drag the “when clicked” block on to the script area underneath the Scripts tab in the middle of the screen (grey screen). This block has to start off any program you make and when the green flag (top right) is clicked, your program(s) will start. The red stop sign will stop your program.

6)  Click on the looks block in the blocks palette and drag over a “switch to costume” block. When you drag this block over to the script area, place it against the edge of the “when clicked” block until a white line appears. If you let go of the block when there is a white line, the two blocks will stick together. If you want to remove a block, just drag it off so there is no white line.

7)  Now we need to have some input, so let’s click on the control block again and drag a “wait” block onto the “switch costume” block. The number in the white box represents seconds, so how long the program will wait in seconds.

8)  Add another “switch costume” block onto the program, except this time click on the pull down and select the other costume.

9)  Add another “wait” block.

10) Now we have a program that starts, switches to a sprite, waits 1 second, switches to another sprite, and then waits another second. Click on the green flag and see what happens.

11)  The animation worked great but the program only ran one time and then it stopped, so we need to tell the program to repeat the program. Click on the control block and add a “forever” block to your program. When you drag this block over, you will notice that it expands to grab the previous blocks when you hover over the beginning of the program. Anything that is inside of the “forever” block will be repeated forever (looped). Now click on the flag and see what happens.

12) Adjust the wait times to modify the bat’s movement and make it more realistic.

13) Now we need some kind of scene, so let’s click on stage (lower right), then background (top center), then import. A bat will look good in the woods, so select nature and then woods. You now have a nice looking scene with an animation. Let’s add some sound now.

14) We could go to sprite, then sounds and then import to use some of the sounds already in the file, but there aren’t any bat sound effects. So we are going to look online and download the wing flapping effect from this site instead. Make sure you save the file somewhere you can find it, for example save it directly into the scratch folder/media/sounds http://www.partnersinrhyme.com/soundfx/PDsoundfx/PDsoundfx_sounds/animal1_sounds/animal_bats_wav.shtml. Import the bat wing sound effect.

15) Click on the sounds block on the block palette and drag the “play sound until done” block onto the last wait “block.” Ensure the sound effect is selected in the block. Click the green flag and see what happens. You may need to modify the wait times to make the animation seem appropriate.

16) Congratulations! You have made your first computer program! Now use what you have learned and see what else you can create. Try making something that you are interested in......

Computer studies 9 Scratch: An Introduction to Computer Programming Mr Jurica 1