openobject.org

Moving to the Arduino

From Open Source Urbanism

We decided to base our version of the bike POV on the Arduino open source hardware platform. The Arduino has a large community of users, an extensive array of online resources and an easy to use C based development environment. By choosing the Arduino the project becomes easily repeatable, upgradable, and 100% open source.

Building a Shield

Before we could start coding for the Arduino we needed to build a board containing a line of LEDs that could be turned on and off via the Arduino's output pins. When adding hardware to the Arduino it is often convenient to construct a board that plugs directly into the Arduino header pins; these boards are called shields. We constructed a shield that contained 7 LEDs, each connected to a digital pin on the Arduino (pins 1 - 7) via a 100 Ohm resistor (resistor values were chosen using this online calculator). The shield also connects to an Arduino ground pin.

When plugged into the Arduino the individual LEDs can be turned on by setting the respective digital pin HIGH.

Initial Coding

Starting with Carlitos' code it was easy to get an arrow to display on the bike wheel.

Following advice from the forums (particularly AVRfreaks) I decided to store the LED array data for the alphabet in bytes and then use a bit shift function to step through the bytes.

I got an initial alphabet description from haakoh's personal home page at the University of Oslo.

My first code attempt is available here: Bike_POV_Beta_1


The first version in operation (errors can be seen in some letters, this was a bug in the alphabet definition, it has been fixed in beta version 4).

Easy String Decoding

Although the above code works it cannot convert a string of characters for display. To do this the alphabet font needs to be in an indexed array. The Arduino environment only seems to like numbered indexes (i.e. we can't use the alphabet characters as the indexes). Instead we can do something similar to bluesterror and use the ASCII code to index the character.

The Arduino site has an ASCII chart that shows all 127 ASCII characters.

bluesterror's code looks like this:

void write_char(char ch)
{
   int i,j;
   ch = toupper(ch);
   if (ch < ' ' || ch > 'Z')
       ch = ' ';

   ch -= ' ';

   for (i=7; i>=0; i--) {
       for (j=0; j<6; j++) {
           char b = font[ch * 6 + j];
           digitalWrite(pins[j], !!(b & (1 << i)));
       }
       delay(DELAY);
   }
   for (j=0; j<6; j++)
       digitalWrite(pins[j], 0);
   delay(DELAY * 2);
}

The useful ASCII characters start at number 32 - the 'space' character. bluesterror's alphabet array must also start at this character, to get the characters position in the array the ASCII code is shifted by 32; thus the 'space' character starts at index 0. bluesterror's alphabet exists as a single array where each char in the array represents a row of LEDs. There are 6 rows per letter; the start of the letter is calculated by multiplying the char number by 6. I'm going to take a different approach and use a multi-dimensional array. This will consist of an array for each character containing 5 bytes that represent the 5 rows of LEDs. These character arrays will then be collected together in a larger array that defines the entire alphabet. Each characters position in the alphabet array will be based on the same order as the ASCII code. This way I can use the ASCII code (minus 32) to address the individual character arrays. I think this makes it easier to manipulate the alphabet and it's easier for me to think through the whole process.

bluesterror uses the toupper() function to convert the characters to upper case. To get this to work you need to include the ctype.h library in the application. Including libraries in your sketch takes up valuable space in the Arduino memory. It's probably more economical to include definitions for all the lower case letters instead. I found a fully defined ASCII library for a 7x5 dot matrix on this page by Alice Campbell. It has upper and lower case letters defined. NOTE: There are errors in the font definition. For a revised version see Bike_POV_Beta_4 (definitions for other size matrixes are available here and here.)

The code is available here: Bike_POV_Beta_2


< Trial of Bike POV devices | Adding a Sensor >