Activity 5– KISS - Keep it Simple with Subroutines

One of the most appealing things about programming languages is the ability to write action steps (or code) once and then reuse that set of actions over and over.  In programming, this process is accomplished with Subroutines.  Subroutines allow you to create a group of Tiles that you can use again and again as needed.

 

In our previous activity we drew a circle by drawing one quarter of a circle and then copied those same tiles 3 more times to draw the rest of the circle.  If we were using a Subroutine, we could have 1 Subroutine that drew one quarter of a circle, and then we could just run that Subroutine 4 times. 

 

In the Scribbler Program Maker, the symbol for Subroutines is the Gear.  Anytime you see that, it means that feature can run a Subroutine.  In fact, you have already been using a Subroutine in all of your programs.  The Green Gear at top of the program edit is the Main Subroutine.  The Main routine is always green.  In this image, the Yellow Gear is a different Subroutine that is used to turn on the lights of the Scribbler.

 

 

Note: To learn more about inserting and running Subroutines, view Creating/Calling Subroutines section in the Scribbler Tips documentation.

 

 

In our first use of Subroutines, we are going to use the lights to flash the signal, S. O. S. which is the universal signal for HELP.  To do this we are going to write 2 Subroutines, one for the letter S and one for the letter O.  Then we will run the S Subroutine once, then the O Subroutine, then the S subroutine again.  Let’s get to it!

       

First off, we need to know how to represent HELP, or SOS with lights.

  • S is shown by flashing lights 3 times fast.
  • O is shown by flashing lights 3 times slowly.

 

 

 

Pre-Activity Check List:

 

 

Activity Steps:

 

  1. Open the Scribbler Program Maker on your computer. 
  2. Starting with a blank program, create a subroutine (Yellow) that blinks the LED on and off once for a short duration
    1. The way this is done is to place a “Make a New Subroutine” Tile and then add the Tiles you want into it.  You will notice that each time you add a new subroutine it is placed to the right of the existing Tiles, and that you get to choose from the available seven colors.  The program should now look as follows:

 

Let’s take a closer look at this program before moving on.  You will notice that there is nothing in between the Green Gear.  Also, if you look closely at the yellow Subroutine, it looks like it is transparent.  The reason that the yellow Subroutine is see-through is because we have not told the Green Gear to run the yellow Subroutine.  We will do that after we make our next subroutine.

 

  1. Create a similar subroutine for the ‘0’ letter that blinks a little longer. 

·         Hint: Use the copy/paste to copy the Tiles in the yellow subroutine to the orange subroutine and then you can just change the timer!

·         Your program should look like this:

 

 

Now that we have our two light flashes (Fast Flash and Slow Flash), we need to make new subroutines to call the flashes so we can make the letters SOS.

 

  1. Insert a new subroutine Tile (red) and inside it, call the yellow subroutine three times followed by a delay. 
    1. Look for the “Call a subroutine” block in the lower left corner of the tool bar.

 

    1. When you drop it into place the Call Panel allows you to select what subroutine to call, click the Gear button and cycle to the Yellow Gear.  Then click the Green Checkmark.  Insert two more yellow subroutines to run the lights subroutine (yellow) 3 times.
    2. Add an additional 1-second pause to the end.  Your program will now look like this:

 

  1. That takes care of the “S” letter, now we need a subroutine to make the lights flash 3 times slowly for the “O” letter.  Add one more subroutine (blue) and copy/paste the entire contents of the red subroutine into it. Now edit each of the subroutine call blocks to call the orange subroutine instead of yellow.  This will generate three long pulses and a pause, the ‘O’ character.  Your program should now look like the following:

 

 

  1. Now that we have the lights and the letter subroutines complete, we now need to tell the Green Gear (our main routine) what to run.  This is where the beauty of subroutines comes in. All we have to do is insert a call to each ‘letter’ in our word.
    1. Insert a call to the Red subroutine into the Green Gear.
    2. Insert a call to the Blue subroutine into the Green Gear.
    3. Insert a call to the Red subroutine into the Green Gear.

 

  1. Save the program and upload it to the Scribbler.
    1. Does it work like you expected? 
  2. Change the time in the yellow sub routine from .25 seconds to .10 seconds and re-upload the program to Scribbler.  What changed? 
    1. Did you notice that the timing changed on BOTH ‘S’ letters.  That is another benefit of subroutines.  If you change the subroutine, every time it is called, those changes will be shown.  This will save lots of time from having to make those changes in several places.

 

Optional Activity

 

Create a program using subroutine to send your initials in Morse Code.  You can use your favorite search engine and search for “Morse Code letters” to find out how your initials are shown with lights.

 

 

>>Update the Lesson Worksheet with your observations.