Dr Blinken

blog, bentosheets and railspages.

Programming Arduino Workshop @ 29C3

42

Agenda

Getting Started

Arduino is a hardware prototyping platform based on Atmel Atmega Microprocessors, in case of the Arduino Uno which we will be using in the workshop the Atmel Atmega 328. Probably the best thing about Arduino is the vast amount of documentation and project examples you can find on the net, which is why I will not try to replicate any of that here and dive right into a short step-by-step guide to getting started programming it.

Exercise

one led on arduino

12-LED-Shields

I’ve prepared simple leds shields to get you started with a bit of Arduino-Programming without the need to put together or solder own prototypes. The shields let you try out some first microcontroller programming with physical interaction with the real world: reading a button state and switching leds on and off! (hey, it’s not a computer display!)

12 LED Shield

The shields have 12 LEDs directly connected to 12 (output) pins, and one or two buttons connected to two other pins, as shown in the diagram below (for just four LEDs and 1 Button).

The Buttons feature a pull down resistor making sure that you read a proper “LOW” on the input pins while the button is not pressed. You can explore the button starting with the 02.Digital->Button Example.

12 LED Shield

As most of the shields have been soldered a bit differently, I’ve provided a header file containing variables with the pin information: pins.h which you can download and use in our own sketches. To use them, you have to declare your board before including pins.h like this:

  #define BOARDn

where n is the number of your board, e.g. for B4 as in the picture above you would change the line to

  #define BOARD4

LEDs: use the int array int pins[] which lists the pins from the lower right corner to the upper left like this:

 11 10 09 08
 07 06 05 04
 03 02 01 00

e.g. to switch on the two middle leds, use:

 digitalWrite(pins[05],ON);
 digitalWrite(pins[06],ON);

(Note that I rather use ON and OFF than LOW and HIGH - one of the shields is connected the other way around, so this is abstracted as well ;-)

If you use pins.h, you can read the button state like this:

buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);

Have a look at the BoardTest Sketch in the arduino workshop example repository on GitHub

Note: some of the shields still have leds connected to pins 0 and 1 which are also used for serial communication. If you have trouble uploading sketches while animations are running, make sure to disconnect them (just don’t push those two leads in when clipping the shield on) - they should be connected to two other pins as well.

Programming Exercise: Dice Library

Write a Sketch that implements a simple Die: The Goal is to implement a simple six-sided Die that can be rolled by pressing a button.

To implement it, you will need the random() function provided by the Arduino library (Reference). It’s also a good idea to write a function showing different numbers on the die, e.g.

showNumber(int number)

You can use the Sketch DiceLibrary as a starting point. It features a header and a class file for the DiceLibrary class where you just have to fill out the methods:

void roll(); // rolls the Die and displays result
void showNumber(int number) // shows a number between 1 and 6

Ideas for Extensions

Events