top of page

Lets continue on learning about how to use the new functions in Scratch 2.0

​

1.Set up your bread board with 3 more LED's, resistors and jumpers from GPIO's 6 and 13

Don't forget, long leg (Anode+)

is across from your jumper!

Short leg (Cathode-) is across from your resistor 

Lets light up those LED's!

What do these numbers mean?

High is on!

Low is off

Why not have our cat change colours as well!

OK it's time to learn about functions, these blocks contain code that we build inside them so we can use them again and again.  We won't have to write the whole code out again, just use the name of the function, without writing so much code, our life becomes a little easier, click Make a Block

Lets name our function "setup"

The "define setup" block will appear on your screen, get rid of the long block code below 'define setup', we don't need it anymore

We need to create the function, so this function will turn off all the GPIO's by setting them to 'low'

Grab your 'when flag clicked' block and drag our your new setup block and attach them together.  Huzzah you just made your first function!!!!

Now lets make another function block that will allow us to choose what GPIO we want and how quickly it blinks. Start by clicking Making a Block, call the block blink

Now we will add parameters, these are inputs we put into our function, so we can input information in to that function to change how it acts! Click on 'Options' just below your blink block and click on 'add number input'

Call your new input 'GPIO' 

1

2

3

Ok click the 'Add number input again' and this time call it 'time'

Now we have created another function! Except this function has parameters in it. So cool, these will allow us to choose the GPIO we want to use and the 'time' (the wait block)

We are not done, we have to code our function. So pull out a 'forever' block a 'set gpio to' block and 'wait secs' blocks and drop them in.  Then pull out the GPIO and time blocks into the blocks you just dragged in 

drag out

Now do the same except set the output to low.  Now sit back and think long and hard about what is going on here.  Pretty powerful stuff.  You just created a function with two parameters.  Now you can use that function and put in whatever parameters you like! You don't have to write out all that code again! Lets try it out

repeat previous step but set to low

Drag out your shiny new Blink block and put in the GPIO you want to use and the time. See what you can create!

CHALLENGE #2

​

Add a 4th LED and blink function.  Make it so your LED's all blink at different times.  Have the cat moving around the screen when you activate the different blink functions. 

​

bottom of page