blog, bentosheets and railspages.
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.
Have a look at some examples (In the Arduino Software: File->Examples), especially
which will be helpful for the exercises below, but feel free to have a look at all the other examples! These are “Arduino Sketches”, which contain two methods: setup and loop. You put all setup code (e.g. initializing pins as input or output pins, seeding the random function) in setup and everything else in loop which will be called repeatedly. You can define variables outside the methods as shown in the BlinkWithoutDelay example.
Have a look at the Arduino Library: Arduino Language Reference
Debugging: You can use the Serial communication to print out debugging messages from your sketch. See 04.Communication->ASCIITable for an example.
Connect a LED to an arbitrary pin (using a 330 ohm Resitor) and let it blink. The cathode (usually the shorter lead) has to be connected to the negative voltage supply, on the arduino usually to ground (GND) - you can of course connect it the other way around, the Anode to 5V and the cathode to one of the output pins which has then to be set to “LOW” to let the led shine.
(If you don’t have an extra LED, you can also use the led on the board connected to pin 13 as in Examples->01.Basic->Blink., but actually connecting a LED by hand might give you a little impression how much fun tinkering with the arduino is!)
if bored, you can connect the LED to one of the PWM pins (marked with ~, pins 3,5,6, 9, 10, 11) and let it fade, as shown in example 03.Analog->Fading.
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!)
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.
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.
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