Shapejam

Trying to follow Buckminster Fuller's example in life, one step at a time to bring you brain scrapings for a new millennium.

Playing with an Arduino

09 Feb 2015 - Nic Ho Chee

Many moons ago, a young technologist would have perhaps seen the odd sci-fi movie, stared in wonder at a robot of some kind and pondered, "how can I make that?" A short conversation with an elder later, they might have found themselves entrusted with a copy of the Maplin's catalogue, marvelled at the future hinted at on the cover and leafed through it to be assailed by myriad arcane serial numbers to byzantine artefacts whose purpose could only be guessed. If that young person were very lucky they would have someone to show them at least the first steps on a path towards creating an electronic automaton. The vast majority would however have none of this, as shown by the dearth of Engineers Britain is creating, electronic or otherwise. Fast forward twenty or so years, and the conversation becomes:

Here is an Arduino, contained in this box is everything that you need to know to start creating electronics which could help you understand the world.

Recently I was gifted one of these beauties, the Arduino starter kit. The kit contains a small programmable micro-controller, with breakout pins which allow you to read and write either digital or analogue values. The analogue pins support a range of effectively 10-bits, with a number of digital pins all of which support simple on/off for +5V/0V and a smaller subset of these which have an 8-bit range. These can be used to control electronics, and the kit contains most of the parts needed to complete a few Heathkit style projects as an introduction to the device.

The Arduino has a compact IDE which can be used to write small programs in C++, with access to library extensions that appear to belong to the language Processing. These programs/sketches are then cross compiled into assembly for the ATmega328 micro controller at the heart of the Arduino which supports code up to 32KB in size. The sketches can be transferred to the device via USB from a Mac, Windows or Linux box.

The project book which comes with the Arduino, slowly introduces new users to the device features, ranging from simply turning Light Emitting Diodes on or off, to reading the output of Potentiometers and controlling motors with transistors. My favourite sketch shows how to write text to a small two row LCD. The code shown below can be used with the project pin layout to enable scrolling using the LCD.

The first section includes the LiquidCrystal header and initialises an LCD object which we can use to send signals to the LCD. The Arduino ships with a set of libraries which allow the user to abstract some of the core functionality, and the LiquidCrystal header can be found amongst these.

#include <LiquidCrystal.h>
#include <LiquidCrystal.h>

// Setup lcd object with access pins indices.

const int REGISTER_SELECT = 12;  // Controls where text drawn.
const int ENABLE_PIN = 11;      // Signals command to LCD
const int DATA_LINE0_PIN = 5;    // Data is sent via the next 4 pins.
const int DATA_LINE1_PIN = 4;    // ... 4 bits of data sent across.
const int DATA_LINE2_PIN = 3;
const int DATA_LINE3_PIN = 2;

LiquidCrystal _lcd(
  REGISTER_SELECT, 
  ENABLE_PIN,
  DATA_LINE0_PIN,
  DATA_LINE1_PIN,
  DATA_LINE2_PIN,
  DATA_LINE3_PIN);

The code below contains a simple C++ class which allows the device to print some text and move it to the left a single character on each call to print().

// Very simple class that scrolls text in an Arduino lcd...
class ScrollingText
{
public:
  // Constructor takes arbitrary string and modulos draw
  // to row 0 or 1.
  ScrollingText(String text, int row)
  {
    _text = text;
    _row = row % 2; 
    _cursor = 0;
  } 
  
  // Print the string and move the cursor to the next position.
  void print()
  {
    String output = _text.substring(_cursor, _text.length());
    _lcd.setCursor(0, _row);
    _lcd.print(output);
    
    if (_cursor > 0)
    {
      output = _text.substring(0, _cursor);
      _lcd.setCursor(_text.length() - _cursor, _row);
      _lcd.print(output);
    }
 
    ++_cursor;
    if (_cursor >= _text.length())
      _cursor = 0;
  }
  
private:
  String _text;
  int _cursor;
  int _row;
};

ScrollingText _line0("All work aaand no play...", 0);
ScrollingText _line1("Makes Jack a dull boy.", 1);

A simple Arduino progam has two main sections, the setup() method contains initialisation code which is run once at the beginning of a program. The loop() method is run continually after the program is initialised. In this setup method, the LCD has been initialised to support sixteen columns and two rows. The two lines are then printed to the LCD.

void setup()
{
  // Initialise lcd with 16 char x 2 rows. 
  _lcd.begin(16, 2);
  
  // Draw initial position of scrolling text.
  _line0.print();
  _line1.print();
}

The Arduino loop method is then called continually whilst the program is running. As soon as the loop method completes, it will be scheduled to run immediately. In the case shown below it simply clears the LCD, prints text and waits for a quarter of a second.

void loop()
{
  // Draw contents of LCD
  _lcd.clear();
  _line0.print();
  _line1.print();
 
  // Pause for a quarter of a second
  delay(250);
}

After a short time tinkering with the device I'm impressed by how quickly I could prototype electronic circuits, and will probably use this in the future as a digital/analogue I/O controller for a synthesiser or joystick project. I'd strongly recommend this to kids as it introduces electronics in a set of simple easy to understand chunks.