Skip to content

Latest commit

 

History

History
132 lines (72 loc) · 7.69 KB

lights.md

File metadata and controls

132 lines (72 loc) · 7.69 KB

Lecture 1: Let There Be Lights

Plan of Action

- Origin and History of Arduino - Getting Acquainted with the IDE - "Hello World" sketch - Traffic Lights

Arduino Uno

Development of the Arduino

Born in an Italian design school, the Arduino was always going to be beatuiful. The distinctive microcontroller has won admirers all over the world, not just for its good looks but in particular for its ease of programming. While the company name and brand are protected copyrights the hardware design and programming software are all open source, meaning that anyone with sufficient knowledge can adapt and modify either of these to their hearts content. Since 2009? the Uno and a wide variety clones/copycats/officailly supported derivatives have found their way into the hands of engineers and hobbyists alike, which has contributed to the recent explosion in the maker movement and internet-of-things devices.

Arduino Philosophy

Much of the Arduino's success is attributable to the open source nature of the project. It is possible to modify both the source code of the Arduino software as well as build your own Arduino in fact it's technically possible to build an imitation Arduino for under €5 worth of parts. This open philosophy has led to a huge community of users, hackers and makers springing up and supporting the platform, and it is arguably this community that is that Arduino's greatest asset.

The Arduino IDE

An IDE, or __i__ntegrated __d__evelopment __e__nvironment, is used to write human readable software, compile it into machine readable code and produce illegible debug messages that will have you tearing your hair out in no time. The Arduino IDE is based on Processing, an open source project developed by ?, and is currently in version 1.6. The icons on the top of the editor perform the following functions:

Toolbar

**Verify** and **Upload**: Verify simply checks that the code is syntactically correct and suitable for uploading. The upload button verifies the code and then uploads it to the connected board.

Toolbar

**New, Open** and **Save** as the the names imply are for creating new sketches, opening existing sketches and saving the current sketch.

Toolbar

On the top right of the editor is the **Open Serial Monitor** button. It allows you to send and recieve serial data between your computer and the Arduino.

Anatomy of a Sketch

A program in Arduino is referred to as a sketch, which is a nod to its design origins. Sketches have the filetype .ino, as in ardu__ino__.

As we progress we'll develop more and more sophisticated programs, but let's start with the most basic "Hello World" sketch, blink, which simply turns on and off the on-baord LED.

There are two sections, or blocks, of code that every sketch has to have, the setup() and the loop()

The loop function will just execute over and over again. In our example we are two different messages, or signals, to our LED. First we set it to be on (HIGH) and then off (LOW).

Hello Ardunio World

This very basic bit of code gives us a good idea of how to structure an Arduino sketch, however, even this basic code has it's limitations.

Let's say that we now want to move our LED from pin 13 to pin 12 - how many changes will this require in our code? We have to change the pinMode function and two digitalWrite() functions. I we had hard coded in pin 13 like this hundreds, or even thousands of times, then maintaing our code would quickly become a nightmare. A better approach in situations like this is to use a variable. A varaible acts like a placeholder for some type of information. Imagine you have a variable called bankBalance, depending on how often you get paid, or what you've bought recently, the value of this variable will change, or vary, hence the name variable. From the perspective of the Arduino, or any computer for that matter, it sees the world only in binary digits, 1s and 0s, which can be used to represent anything the programmer wants. For this reason many programming langagues allow you to tell the computer what type of data, or varible, you wish to use. We'll cover data types in greater detail but for now all we need to know is that an int or integer is capable of storing roughly ±32,000 whole numbers. Since Arduino pins only go from 0 - 13 this will do fine.

Morse Code

As well as sending information digitally, via ones and zeros, it's also possible to send information light or sound. Systems such as Morse code encode information as either long or short pulses. Does this old ring tone sound familiar to you?

Your browser does not support the audio element.

What we'll do now is have our LED recreate this pattern (3 short, pause, 2 long, pause, 3 short, pause) using the functions we've learned so far.

Creating Functions

A function, or procedure, (the two terms are used interchangably depending on which language you're using) broadly defined is simply a set of instructions that a computer should execute repeatedly, until it reaches a well defined stopping condition. More specifically, in our sketch we would like to have a function to produce any number of short blinks (dots) and any number of long blinks (dashes)

Resistors

A resistor is a passive electronic device which limits the amount of electric current that can flow through a circuits. The image on the left is of a ceramic-coated carbon filament resistor and the schematic on the right is the circuit symbol for a resistor. An object's resistance is measured in units of Ohms.

Ohm's Law

gimmick: math

You'll probably recall from your science studies that voltage, current and resistance in a circuit are all related. More specifically, voltage (V) is equal to current (I) times resistance (R)

$$ V = I \times R $$

Traffic Lights

traffic lights circuit diagram

### Code Download Code

Video

Stretch Goal

  • Initialise the LEDs using an array
  • Toggle lights based on keyboard input

Where to?

Next Section Arduino Resources