TinkerKit Tutorial: Basics: 04 – Starting with the code

Basic Lessons:

  1. Introduction
  2. Project Setup
  3. Arduino Software
  4. Starting with the code
  5. Sensors and Actuators
  6. Button & IF statement
  7. Serial Communication
  8. Connect Sensors and Actuators
  9. Button States
  10. FOR Loop
  11. Traffic Light

 

In this tutorial we’ll introduce the basic concept of a TinkerKit! sketch. If you’re already familiar with the Arduino Code, it will be a piece of cake. For the code example we’ll be starting again with an LED, but the process is the same for every other output. LEDs are the simplest outputs and in this tutorial we’re focusing on the code.

Plug the TinkerKit! as usual and open the Arduino software, then connect an LED at the O0 port.

This is the layout you should have:

led

Now in the software open the Empty example (at this point you should be familiar with opening examples but if you don’t remember how to do it, just look under File > Examples > TinkerKit > Empty).

globals_etc

Ok, this is how the Empty example looks like: it’s the starting point of every TinkerKit! sketch. It’s composed by three parts: globals,setup and loop.

  • Globals: it’s the beginning of any sketch, here we’re declaring the components and other things (like variables) that we’re going to use in our code.
  • void setup() (or just setup): it’s function that runs just once before the main code and is used to setup everything before the loop.
  • void loop() (or loop): it’s where we write the main code. The loop function is repeated over and over until the Arduino is powered. All the instructions written inside the curly brackets are executed sequentiallyone line after the other.

 

If you want, delete the commented lines that start with //, they’re not seen by the software and are used to add comprehensibility to your code.

The line to write in the “globals” section is the following:

TKLed led(O0);

  • The first word of the line is the class, since we plugged an LED it’s the TKLed class; if by chance we’ll connect a relay, it would be TKRelay and so on.
  • The second element is the name that we assign to that particular LED. Here we use just “led”, if we are working with multiple LEDs, it’s reasonable to call them led1, led2 and so on. Each component has a name.
  • The third part is quite easy, it’s the number of the port where the block is connected with the shield, if you’re not sure, just look the number of the port where your cable is plugged into the shield.
  • All the lines of code we write must end with a semicolon ; it’s a common error for beginners to forget them, so don’t worry if it’s not that automatic at the beginning.
Screen-Shot-2012-10-23-at-12.56.02

We leave the void setup() empty, TinkerKit! objects don’t need to be initialized.

Let’s turn the LED on by writing inside the curly brackets { } of the void loop()

led.on();

Quite simple isn’t it? First we write the name of the element we want to command, in this case the LED that we previously named “led”, then the method we want to apply to it, in this case “.on()” that turns the LED on at its maximum brightness. Don’t forget the semicolon at the end of the line and upload everything to the board using the upload button. Your LED should turn on, if not, we wrote the complete code so you can check for errors:

#include <TinkerKit.h>
TKLed led(O0);

void setup() {
}

void loop() {
  led.on();
}

Now try to write .off() where .on(); was, then upload the sketch to the board and see what happens: the LED should turn off.

Ok, now let’s make it blink. What we’re going to do in other words is turning the LED on, wait a second then turn the LED off and wait another second. In order to achieve that, we have to introduce a new function called delay. The delay “freezes” the board for a certain amount of time, expressed in milliseconds (1 second = 1000 milliseconds) and it’s what let us tell the board to “wait”.

delay(1000);

the line above can be read as “wait one second” (1000 milliseconds).

On the Arduino board, the instructions are executed one after the other, so our loop will look like:

led.on();
delay(1000);
led.off();
delay(1000);

upload it on the board to see the LED blinking!

Blinking an LED is the “Hello World” of physical computing, different components have different classes and methods but more or less that’s the dynamics of a TinkerKit! code:

• declare the components in the globals
• assign methods in the loop function
• run / upload

NOW?
Start playing with other components, in the next tutorial we’ll be using a sensor to control an output and introduce the “if” operator.


 ★ Challenge ★

 

Try to light up the LED at its mid-brightness instead of having it ON at full power.

The solution is in the next tutorial.

 

Prev Section: Arduino Software

Next Section: Sensors and Actuators

One thought on “TinkerKit Tutorial: Basics: 04 – Starting with the code

Leave a Reply to Rick Cancel reply

Your email address will not be published. Required fields are marked *