Code Kitty Coding Guide

Let's code your Code Kitty! If you have never coded before, don't worry. We will help you through it.

First, let's clear a few things up.

  • What is code?
    • Code (also called "Computer Programming") is the language we use to communicate with our computers and electronic devices. But we use language with a computer differently than we do with a person. Instead of being polite and saying "please" and "thank you", we give our computer commands using as few words as possible.
    • Example:
If we want a person to get us a ball, we would say "Please get me the ball".  
If we want a computer to get the ball, we might say "get(ball);"  

How does code work?

    • Computers are electronic devices (meaning that they run on electricity). They send this electricity to all the parts ("components") inside, both to power them and also to communicate information between them. At the heart of the computer is a component called a "Processor" that acts as the central "brain" and controls what power and messages get sent to what components. The code you write tells the processor what to do and how to do it.


    • There are different kinds of code that are good for different things. We think of these different kinds of code in a similar way to the different languages people can speak (like english, spanish, french, etc), and we also call them languages on the computer. Different coding languages are used for different things, for example HTML and Javascript are used to make web sites, and C++ is used to write applications (and program Arduinos!). Some languages like Python are useful for many purposes and very much worth learning, but for this lesson we will focus on C++ since that is what the Arduino uses.


    • Code is written as a set of instructions and stored in a file on your computer. The code is then either "compiled" (turned from instructions humans can understand into instructions a computer can understand) or "interpreted" (fed into a different computer program that interprets the instructions and shows you what happens).

    • Most apps on your tablet or smart phone are compiled, as are the applications on your desktop or laptop computer. When a compiled program is run, your computer reads the instructions and follows them to do whatever the app is supposed to do.


    • Web sites are an example of interpreted code. Your web browser reads the instructions and follows them to know how to show you the information in the HTML code files.
  • What is an Arduino?
    • An Arduino is a very small computer processor (so it is a "microprocessor") which is designed to be easily used and coded by anyone, and to be very inexpensive so that you can use it for any project.
    • There are several connections (pins) on the Arduino which are there so that you can connect other things to them. Maybe you want to connect a motor to make something drive, or an LED to light something up. You'd connect these components to pins on the Arduino, and then write code that tells the Arduino what to send to (or receive from) those pins.
    • There are many different sizes and shapes of Arduino microprocessors. Code Kitty uses the small Nano one because it is very inexpensive and easy to use.
    • We also use a special (purple!) custom-made board that goes on the pins of the Nano and makes it easier to connect the parts of your Code Kitty robot. It has parts labelled for "Left servo", "Right servo", "Tail" and "+" (Power).


  • How do you code an Arduino?
    • Arduino code files are called "Sketches". Each sketch is a list of instructions that tell the arduino what to do and how to do it.
    • Every Arduino sketch is setup the same. Look at the image below for a breakdown of what the parts of a sketch do:

But what does it all mean? Let's break it down line by line:

#include <CODEKITTY.h>

This line, called an include tells the Arduino to look in a file called CODEKITTY.h for a list of things to add into the code. This gives you a way of re-using bits of code without having to type them all out every time. This list is called a Library, and usually they contain functions you can use in your code.


/*
 This is a comment.
 Comments are ignored by the arduino.
 They are just a way for you to keep notes in your code
*/

These lines (actually anything put between /* and */) are called comments. Comments are a way for a coder to write notes in their code, so that somebody looking at their code can know something. Maybe you want to explain how a function is meant to be used, or what you had in mind when you came up with a bit of code. No matter what they say, comments are ignored (not processed) by the Arduino.


void setup(){

This line starts the setup() function. The setup function is automatically added to every sketch and needs to be there for the Arduino sketch to work (even if you don't put any code in it). Any code you put on a new line after the curly brace { is run once when the Arduino first turns on.

So what's with the { } anyway? The code of functions (in other words, the instructions that the processor follows when that function is used) are put between the curly braces. It always starts with { and ends with }. This way the computer knows which instructions are supposed to happen when setup() is called (when the Arduino first starts up).


CODEKITTY kitty;

This line declares an instance of the CODEKITTY object. In other words, it makes a copy of the Code Kitty software and names it 'kitty'. From then on, whenever you want to tell Code Kitty to do something, you can say 'kitty' instead of CODEKITTY at the start of the function you are using. This step is required to be able to use the Code Kitty library (cklib), and you need it as the first line inside your loop() and setup() functions.


kitty.forward(1000);
kitty.left(500);
kitty.risingBeep();

These lines are the instructions that the Code Kitty will follow when it turns on. In this case, it says to go forward for one second (times are in milliseconds or ms for short, and one second is one thousand milliseconds. So half a second is 500ms, two seconds is 2000ms, and so on), and then turn left for 500ms, and then make a rising beep (three beeps in rising pitch).

This code is just provided as an example (try it and see how it works on your code kitty!), but you can put any code here that you want.

Please check out the Code Kitty Function reference for a complete list of the functions available in the Code Kitty library (cklib).


void loop(){

This line starts the loop() function. Any code within the curly braces of the loop function will be run over and over again until something stops it.




Coding your Code Kitty in 9 steps



Coding Guide



Coding Guide Sidebar: Common Arduino Functions

That's it for now! There will be a lot more instructions and examples in the future, and you can always email info@codekitty.org or connect with us on Twitter or Facebook if you have any questions!

We hope you enjoy coding your Code Kitty robot!