Skip to content

Instantly share code, notes, and snippets.

@ZucchiniZe
Last active November 8, 2017 22:10
Show Gist options
  • Save ZucchiniZe/996a99ddbcd8375f9f7fdf65f31b11fa to your computer and use it in GitHub Desktop.
Save ZucchiniZe/996a99ddbcd8375f9f7fdf65f31b11fa to your computer and use it in GitHub Desktop.
teaching programming to hs juniors who have no prior experience

Teaching how to code

We will be learning the basics of programming using the very common language 'javascript'. Some of you may have heard of the programming language 'java'. It is very different from javascript. Javascript is somewhat similar to C++ which is the language that we use to program the arduinos.

Instructions

To follow along, all of you should go to https://repl.it/languages/javascript. There are two halves of the screen, the white and the navy

The white half is also known as the editor or where you write your code.

The navy half is also known as the REPL (read-evaluate-print-loop) where you can run your code and instantly see the result.

To test out the REPL and make sure it works, type 1 + 1 into the REPL (navy half) and then hit enter. Once you hit enter it should look like this

> 1 + 1
=> 2

Variables

In programming one of the most basic concepts that is used almost every day are variables. In essence they are containers that have a name and a value.

Lets test it out in the REPL by typing out the following code:

> var x = 1
> var y = x + 1

> y

When you hit enter after you type in the y it should show 2. This is

This works because we have bound the variable of x to the value of 1. So, in terms of containers, we have 1 container so far and its name is x and its value is the number (or integer in computer terms) 1.

The equivalent arduino code should look like:

int x = 1;
int y = x + 1;

y;

You can also bind the follow types of variables to a name:

  • strings
  • numbers
  • booleans
  • objects
  • functions

We will talk about these more in a bit.

Types: an aside

For the container there is one more part that you need to know about but is sometimes hidden from you in certain languages. This is called the type.

The type of the variable is the type of value the container holds. So now, our container contains three specific values for a single variable: the name, the value, and the type.

In javascript — the language we are currently learning — the types are automatically found for us and added to the variable container. This means that we don't have to explicity write out the type of the variable every time we want to create one (a variable).

But in C++ — the language use to write code for the arduinos — we do have to explicilty write out the type of the variable whenever we want to create a variable. To create a number (integer) in C++ you have to type out int name = 1.

Functions

Now, we are getting to the really really fun part of programming (in my opinion at least): functions!

Now the formal definition of a function is something that takes an input and delivers and output based upon that input. That is only half correct.

In our case we always have to have the functions setup() and loop() in our adruino code. These type of functions are called 'procedures' because they don't take any input (parameters) and don't deliver any output.

Now, lets create our own function. We are going to create a function called multiplyByFive and it should always take one input (parameter) and should only output one output which is going to be the input multiplied by 5.

Each function can take an unlimited number of inputs (commonly called paremeters). In this case of multiplyByFive we will only have one parameter. An advanced word for the number of parameters a function takes is "arity", so the arity of our multiplyByFive function is going to be one since we only take one parameter.

Every time you want a function to give you an output, you need to specify a return value.

Now, lets write our function!

function multiplyByFive(number) {
  return number * 5;
}

Time to break everything down. First you have to write the function keyword to tell the interpreter (the thing that runs the code) that what comes afterwards is a function. Next we have the name of the function: multiplyByFive this is what we use to reference the function and call it later on in the code. Then we have the parentheses ( and ) which tell us what our arguments (paremeters) are. This function has an arity of one with the parameter/argument name of number, meaning that it can take a single argument and inside the function, the argument is bound to the variable number. Lastly we have the return statement, the part of the function that tells us what it gives us as output. Since the return keyword is followed by number * 5; that means that the multiplyByFive outputs the number variable (from the parameter) multiplied (*) by the number 5.

Now since we know how to create basic functions, we could create a function to figure out more complicated things. Here is a function that converts degrees to radians and then squares that:

function degToRad(deg) {
  return Math.pow(deg * Math.PI / 180, 2);
}

Whew. That was a lot. Hopefully you understand the two (of many) basic concepts of programming now. If you have any questions, please just ask me or send me an email at [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment