Latching Input

Presently this is just the code required to get a push-to-make input to latch and LED on and off. A full explanation will follow.

LED_latch

Code


/*
* by Joshua Maxey
* created 26/03/2013
*
* requirements:
*  - 1 x LED (with suitable resistor)
*  - 1 x switch (push to make, non latching)
*
* instructions for this sketch can be found at the following URL:
*  
*
**************************************/

int led = 11;
int button = 13;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  //we will use the serial port to alert us once the setup function has finished
  Serial.begin(9600);
  Serial.println("setup is complete");
}

void loop() {
  //if the button is pushed and the LED is off
  if (digitalRead(button) == LOW && digitalRead(led) == LOW) {
    //turn it on
    digitalWrite(led, HIGH);
    Serial.println("LED is on");
    delay(200);
  }
  //if the button is pushed and the LED is on
  if (digitalRead(button) == LOW && digitalRead(led) == HIGH) {
    //turn it off
    digitalWrite(led, LOW);
    Serial.println("LED is off");
    delay(200);
  }
}

Blink

Presently this is just the code required to get an LED to blink on and off. A full explanation will follow.

This is the most simple circuit we can build to demonstrate the digital output capabilities of the Arduino
This is the most simple circuit we can build to demonstrate the digital output capabilities of the Arduino

/*
* by Jeshua Maxey
* created 26/03/2013
*
* requirements:
*  - 1 x LED (with suitable resistor)
*
*  instructions for this sketch can be found at the following URL:
*  
*
*  This example code is in the public domain.
*
**************************************/

int led = 13;

void setup() {
  //set the pin of our led to be an output
  pinMode(led, OUTPUT);
}

void loop() {
  digitalWrite(led, HIGH); //turn the LED on
  delay(1000); //wait one second
  digitalWrite(led, LOW); //turn the LED off
  delay(1000); //wait one second
}

My First Circuit

To harness the poer of the Arduino, one needs to be able to build circuits that we can then control with our board. The first circuit we will build will only use the board as a power supply. It will provide a voltage to our breadboard, forcing a current through our LED making it light up. There’s no programming in this step, just some circuit building fundamentals.

My First Circuit
Our first circuit uses only the +5V and Ground (GND) pins of the Arduino

Some more text to follow.

The Basics of Programming for Arduino

The Arduino programming language is based on an open source language called Wiring – it has a syntax similar to C. That fact may not be particularly interesting to a beginner, but for me it’s very significant. The first language I ever learnt was C. As an unguided 16 year old I naively googled “what’s the best programming language to learn first?”. This question illicits wholly different answers to the question “What’s the best programming language for beginners?” but in my ignorance I bought all £22 worth of ‘C for Dummies’, an 800+ page amalgamation of 6 C tutorial books and consumed the thing cover to cover. Since finishing that book, I’ve been looking for ways to apply this knowledge. So you can imagine my excitement to discover Arduino syntax was so similar. Anyway, that’s enough about me. Here’s what you really need to know.

The Arduino requires you use at least two functions in your sketch. These are setup() and loop().

setup()

The setup() does exactly what it says; it sets up the Arduino to do all the tasks you want it. This involves things like setting up pins as inputs and outputs that will later be used to control objects in the physical world. This function runs only once when the code is first loaded onto the board.

loop()

Once setup() has run, the second required function, loop() runs over and over and over until the arduino loses power or a new sketch is uploaded. loop() runs incredibly quickly. For a very simple sketch, it is called at a frequency of 130kHz – that’s 130,000 times each second. This is an important point to note; we’ll consider why when we write our first sketch.

IDE Setup

The Integrated Development Environment (IDE) speeds up the Arduino development process considerably. You’ll first need to download the latest version (available from this link) and install it on your machine. I use a mac computer. Subsequently, the screenshots I provide will be of the Mac Arduino IDE. That said, I can’t imagine the Windows or Linux version will vary too drastically to prevent you from following along. If in doubt, google it!

To avoid any niggling hiccoughs, I find it’s best to have your Arduino connected to your computer before you start up the IDE, although this isn’t absolutely necessary.

The IDE exists to do a lot of the complex yet trivial work of translating your code into a form you board can understand (a process called compiling), and subsequently delivering that program to the board. These two steps can vary depending on your board and setup. The IDE is smart enough to take your code and compile it correctly for whichever type of Arduino board you’re using. All you have to do is tell it what board you want to program to run on, and it will do the hard work for you.

This is a ridiculously simple task. Navigate to the top menu bar and select the ‘Tools’ dropdown menu. From here, hover over the ‘Boards’ option. A list of Arduino boards will appear. Choose the model you’re using (this is invariably displayed somewhere on the board itself) and hey presto, the IDE knows how to compile your code.

Board Select
This screen capture shows the menus you need to navigate through to select your board model

The only thing left is to tell the IDE how you’ll be communicating with your Arduino board. When starting out, you’ll want to use the simplest option available to you, USB. Navigate again to the ‘Tools’ dropdown. This time hover over the ‘Serial Port’ option. From here select the ‘dev/tty.usbmodem621’ option.

Serial Port
This screen capture shows the menus you need to navigate through to define how you’ll be communicating with the board

Et voila! We’re ready to start programming for Arduino.