Monday, January 4, 2016

Tutorial - How to set up simple home automation system - Part V

The Arduino


On this page

Arduino Boards

The official Arduino site describes Arduino as "an open-source physical computing platform based on a simple microcontroller board, and a development environment for writing software for the board"Open-source here refers to the board designs, and they're okay with other companies selling clones (possibly cheaper).
There are a lot of different boards available, but it all started with the Arduino Uno - the board we'll use in this project. You can also use a Nano, which is basically a smaller Uno. But it might be a little harder to work with for beginners.
If you don't have the right USB cable yet, make sure the board you buy comes with one (most do). Theofficial Arduino Starter Kit is also a very good way to get an Arduino and a whole bunch of components (plus a book with 15 projects to get familiar with the platform). You can find it at many (online) retailers.
Product links to Amazon.com, with the price at the time of writing:

Software

After you buy your Arduino board, you'll need to download the Arduino IDE. It's a simple programming environment where you can write your sketches and upload them to your Arduino.
The Arduino IDE
The green bar at the top has all the actions you'll need:
  • The checkmark will Compile your code. If it's correct, the bottom part of the window will stay green and tell you about memory use. If it's not, it will be orange and tell you what's wrong.
  • The arrow will Upload the sketch to your board (and compile first).
  • The three buttons next to that are for the usual file actions: Create, Open, and Save.
  • And finally the button on the far right is the Serial Monitor, which you can use to monitor your code while it's running on your Arduino.

A Little Bit About Programming

If you're familiar with programming, you can probably skip this section.
An Arduino sketch has a few sections. At the top you specify which libraries you want to include. Libraries are pieces of code that you can import so you don't have to start from scratch. For example:
#include <dht.h>
This will include the DHT library that takes care of all the lowel-level code involved with talking to a DHT temperature sensor. You need to download a library to your Arduino libraries folder before you can include it in your sketches.
Below the libraries you declare global variables. Think of variables as little pieces of information. Each variable has a type, name, and value:
int packets_sent = 0;
This variable is called "packets_sent" and starts with value "0". The type of this variable is "int" - meaning it can hold whole numbers (= no decimals). It uses 16 bits to store its value so it can hold values from -32,768 to 32,767 (215 = 32,768, plus 1 bit for the sign (+/-)).
For more on variables, see Variables on the official Arduino site.
Finally, each Arduino sketch has two default functions:
  • First is setup(), where you keep code that will be run only once when the Arduino starts up.
  • The main logic is in loop(), which is run repeatedly: as soon as it reaches the end, it starts again from the top of the loop function.
The Arduino site has a Language Reference where you can learn about all the different structures, variables and functions. For now, here's a crash course:
// Any code that starts with two slashes is a comment
// That means it's meant for humans to read - the compiler will ignore it

// Variable types:
int number; // 16-bit number (-32,768 to 32,767)
long bignumber; // 32-bit number (-2,147,483,648 to 2,147,483,647)
float decimalnumber; // decimal number (not always exact though - computers are weird)
bool ok; // boolean (only two values: either true or false)
string message; // text
char letter; // single character

// If you only want to execute code when a certain condition is met, use "if"
// In this example everything between the curly brackets will only be executed
// if the value of "number" is higher than 10
// You can add an "else" statement, but "if" can exist without it too
if (number > 10) {
 bignumber = 100; // give the "bignumber" variable the value "100"
} else {
 bignumber = 0;
}


No comments:

Post a Comment