The full Raspberry Pi Workshop in step-by-step format can be found here http://coreelec.io/piworkshop We're going to animate a nice sine wave in our shell. To do this we'll write a simple function.

### Search Videos

Related Content

### Transcript

This time around we’re going to write our very first function. Now it’ll be a very simple function but the power that this gives you to organise your code is really important so we’re just going to go through this quite quickly. The script that we’re going to arrive at is going to produce a nice animation I guess, in the prompt or the shell of a sine wave. So I’m going to run a complete script, you can run Python scripts from the console window by typing in python3, because I want to use Python3. If you were using Python2 you would just use ‘python’ but I’m using Python3 so I’m going to use that command and I’m just going to give it the path to where I’ve kept my file and that is in resources, and it’s called 2.3 something so I’ll just use the wildcard there. You can see that the prompt is now animating this nice sine wave which is just like a series of bar graphs essentially and it does that a bit and then stops. So lets achieve this effect from scratch. I’m going to close my shell and back in the Python shell we need to create a new shell and I’m just going to save that in the chapter 2 directory of course and I’m going to call that 2.2_pretty_sine.py. Ok so, to get this effect working we’re going to essentially be concatenating strings again. In the first script that we tried we had a concatenation of that equal sign, this is the very same thing, we’re just going to use a bit of math to calculate how long or how many equal signs we need to use.

As always to get started we should import Math and import Time, that will give us the sine function and some delays on our Raspberry Pi. Now we don’t want the thing to go on for ever, I think, so I’m going to define a number of cycles that I want the sine wave to go through - numCycle and I’m going to say 5. Now Math is giving us access to the mathematical constant Pi but rather than have to type math.pi when I want to invoke pi, I’m just going to create a variable called pi and make it equal to that and that just saves a bit of typing down the track perhaps. Now this is going to be the function that we write and you can kind of think of it as exactly what I’ve done here with pi. It’s just going to make the code slightly easier to read, it’s not necessarily going to increase how useful it is - we’re essentially renaming something as something else. So I’m going to define my function with ‘def’ and I’m going to call the function ‘sin()’. In the last bit here we talked about functions and arguments - when we define a function this is when we define what it wants to take into it, so I’m going to define this function with a single argument and I’m just going to call the argument ‘x’ .

That’s what is used inside the function, so whenever I refer to x inside this function (x) that refers to the input. We finish that off with a colon and when we hit enter we can see we’re indented. So what this function is going to do is simply called math.sin(x) and we’re going to parse it x and it’s going to return that. So that means that where ever we call sin(x) you can essentially think of that as being replaced by whatever the result of the return line is and in fact this time round it’s math.sin(x) so you can see the parallel between this line where essentially we’re relabelling a constant and this line where we’re kind of redefining a function. So let’s create our animation! First off we need something to parse into the sine function and I’m going to call it x again, just to highlight that variables out here are not the same as variables with inner function. This ‘x’ is different. I’m going to say while x is less than, and this is where we set up the logic to decide how long our code runs for. So I want it to run for 5 cycles which means I’m going to use ‘numCycle’ and I’m going to multiply it by 2 times pi. This is just, I guess, an artefact of the sine function is using radians. We want to work in number of cycles that the sine wave does, but the sine function works in radians so to convert from one to the other you have to multiply it by 2 pi. If you don’t know about radians and you want to learn some more about what they actually are there is a fantastic animation on the Wikipedea page about how a radian is defined. Essentially it’s just a convenient way to express angle. Alright, enough about that, let’s do some math!

So we need to calculate how long our bar is going to be. Sine is a function that returns a value between negative 1 and 1 so I need to do a little bit of magic on that to turn it in to something that’s between 0 and how ever long our bar needs to be. To reach that end I’m going to create a XXXXXXX called bar and I’m going to say that it is equal to 20 times the sine of x. So that means that bar could take on a value of between negative 20 and positive 20 because we’re taking sine x which is negative 1 and we’re just multiplying it by 20 so just making it bigger linearly. But that is going to return to us some funny values, it’s going to return fractional values like decimal point values. Of course I can only print whole numbers of equal signs in my animation so what I’m going to wrap this whole thing around in is a call to int and what this does as perhaps you can see by the prompt, int takes something in and produces an integer.

So if it gets 2.1 in it will return 2. This is almost but quite, it’s almost like being able to round to the nearest whole number. So I’ve created sum number called bar and that’s going to change as x changes which means we need to increment x. So this += that’s like saying x = x + 0.3. It’s kind of a short hand notation. Now I’m going to print, this is where I’m going to print the bar for each step of the animation. I’m going to say, well hang on a minute now, this is going to return - bar is going to take on negative numbers and I can’t have that. It’s like I can’t print negative numbers of bar length so what I actually need to do is take bar which is from negative 20 to 20 and shift it. So I’m going to shift it by 21 so theres always something on the screen, so I’m going to say 21 + bar, that means that this will always evaluate to I guess something between 1 and 41 or 40. So that is now just sum number which means that we can multiply it by our string and then of course because we don’t want this flying past without being able to see the animation we’re going to involve times sleep and I guess 30 milliseconds should be enough and that completes our script.

So you can see that we have a loop that is a little more complicated perhaps than the one we looked at before and we’ve just touched on defining XXXXXX function. We’ll do that more in the next script but for now lets give this a run or lets see if it runs at all - ah yes, there it is, theres our sine wave oscillating away - fantastic! So in the next section we’re going to take this idea of writing functions to organise our code at the next level and actually do something a little more useful with it.