Friday 18 May 2012

Arduino: How to read in from a text file and alternatives

Greetings all,

Apologies its been so long since my last post, I blame a BSc with a dissertation (which I will post about soon) which has basically made me disabled since February.

This post is aimed at trying to help overcome the fact that the Arduino doesn't have a file structure and therefore there is no direct way to read/write to files, the most common file of course being a text file. I haven't written any examples myself as the best ones are in the links provided. I have however tried out these examples and they have all worked.

Solution 1: Using the Processing IDE and serial port
The first solution I've only discovered from writing this post and its fair to say that its the best solution.

It involves using the Processing IDE which can be downloaded here. Having only just discovered Processing I can't say too much about it, but what I do know is its a sister program to the Arduino IDE so things are very similar between the two. It incorporates many examples including file I/O, graphics and simulations which make it a very handy and useful program.

I will maybe post about it once I've got to grips with it.

As for how to read in the text file, the work has been done in this blog http://arduinobasics.blogspot.co.uk/2012/05/reading-from-text-file-and-sending-to.html

A few points to note: The example in the blog may or may not use the RX/TX library. I already have that library installed on my PC so I'm not sure if it requires it or not (it probably doesn't).

The example works by communicating over a COM port and so Arduino just has to read the serial input given to it.

Solution 2: Using the RX/TX library
As mentioned in solution 1, there is an RX/TX library that allows communication over ports (usually COM3 or COM10). It can also be interfaced with Java and so can be used in IDEs such as NetBeans or Eclipse.

Its not worth producing examples as they have already been done well on the Arduino website:
http://arduino.cc/playground/Interfacing/Java

Solution 3: Using an SD card shield
Another solution is using an Arduino SD card shield with the SD card library. This can be quite handy, especially as data can easily be stored on an SD card.

Your standard shield can be one like this.

You will then of course need the SD card library which can be downloaded  here ( i think).

The best examples are on the arduino.cc website, the most relevant one being this one: http://arduino.cc/en/Tutorial/ReadWrite

Solution 4: Treating your .ino file as a text file
So this is a bit of a hack that I actually used for my dissertation. Its not the best way but at least it is a solution.

For this to work, you simply treat the .ino file (was formerly a .pde file) as a text file.

From my experience in Java, you can read/edit/write the .ino file with no problems, thus I can only assume it will be the case for other programming languages.

Its not really worth showing an example as its fairly straight forward, the difficulty lies in finding the right place to insert the new code/text.


Unfortunately, this isn't great, its cumbersome and requires a lot of string manipulation. However it worked for my dissertation which required large amounts of amended text. The benefit is that this avoids using any other components, save time and the number of pins used.


In conclusion
In my personal opinion, the best solution is the first one. By having the data sent over a port, it means the Arduino can be active whilst the processing program runs.

Of course you may have a scenario where one of the other solutions suits you more.

Hope you've found this post helpful, next post should be about natural computing.

Any questions/comments/suggestions welcomed.

Jake.


Sunday 26 February 2012

Arduino - How to power LED matrices using shift registers: Part 2

Welcome to part two.

This has all the steps you need to get your creations up and running. But first, the code for my program can be found here: http://dl.dropbox.com/u/16309910/test.ino or search for me on github: j-fray or clone it using https://j-fray@github.com/j-fray/Arduino---Simulating-bead-sort.git


There are also some pictures of the schematics which you can find here:
Imput layer: http://dl.dropbox.com/u/16309910/inputlayer.png
Output layer: http://dl.dropbox.com/u/16309910/outputlayer.png
Power layer: http://dl.dropbox.com/u/16309910/powerlayer.png

I will upload any remaining documents to my github as listed above.

Now obviously this program is meant for 6 matrices, but it should be pretty straight forward to alter it for any number of matrices.

Step 1 - Setup
A good place to start is to get one matrix up and running. You need 3 pins on your Arduino board, one for data, one for clock and one for load pins. With the MAX7219 these correspond to pins 1, 13 and 12 respectively on the shift register. Put the shift register on a breadboard and connect these up.

Then its time to connect the ground and power pins up. The ground pins on the 7219 are 4 and 9, the power pins are 18 and 19 (check the link to the schematic in part 1). Pin 18 needs to be connected to a 40KOhm resistor (or equivalent combinations) and then connected to the power supply.

Arduino boards in general have a 5V output and that's what I used, just make sure the pins are connected in series and not in parallel.

Step 2 - Connecting to matrix
Now to start connecting up the matrix. As mentioned in part 1 the pins mappings of LED matrices vary for each make so check the datasheet of your matrix to see what pins use what. In my case pins 1 - 4 and 20-24 control the rows.13-19 control the red cols and 5-12 control the green cols.

Connect your matrix up appropriately. It is worth uploading the program to the Arduino board first and connecting the pins one at a time so that you can see the matrix working and deduce if there are any problems with a particular row.

The result should be a small sequence on the matrix.

Step 3 - Repeat
Repeat steps 1 and 2 for however many matrices you want, remember to do small bits at a time and test them. The last thing you want is to wire the whole thing and find it doesn't work or you've wired it wrong.

Here are some pictures of my creation:




Schematics
Input layer

Output layer

Power layer




Common problems (I found)
Matrix kept turning off
The most common problem I had was finding the matrix would just randomly turn off and wouldn't turn back on until I reset the Arduino board. I found out this was due to the resistance of the resistor I was using. There is a handy table on selecting the value for this resistor (Rset) in the guide in part 1.

The matrix is displaying my sequence/pattern incorrectly
This is most likely due to connecting the pins up incorrectly. Remember to check the pin mappings for your matrix and whether its common cathode or anode.

My sequence will stop/freeze
This will probably be due to the delays used in the program. If its anything like mine there will be a delay at the start/end of the sequence code. Playing round with that should get it working again.

Any other problems you have you're welcome to ask me about them, but I can't guarantee I'll be able to solve your problem :)

Thanks for reading and I hope this was helpful

Jake

Arduino - How to power LED matrices using shift registers: Part 1

I intend for this to be a comprehensive guide to controlling LED matrices, there are some guides out there (also as blogs) but it all depends on what you're doing.

If you're looking for a quick step by step guide, skip to part 2, if you want to know everything about what you're doing, read this before part 2.

Note - I have only been doing Arduino stuff since July 2011 and so I still consider myself to be a novice at it. While I have completed what I set out to do, there may be some things which I may have done wrong or are bad practice. If you have any questions please feel free to email me or ask in the comments below.

Background
LED matrices can be used for many things, I have chosen to use it to represent the bead sort algorithm (this is for my dissertation, I will probably post about that at some point in the future). This is where beads are sorted using the properties of gravity, and is handy because each LED can represent 1 bead. Here is a gif I found on google images that show you how it works: http://mgs.spatial-computing.org/ImageGallery/EXEMPLES/BeadSort/anim_bead_julien.gif

The equipment
Unfortunately the components for any build like this will be a little expensive, but it depends how you do it.
Here is a list of my equipment:

How the LED matrices work
When I first started using them, it was a bit of an alien world to me so hopefully this part will help. Remember the details for the matrices will be different for each model, check the matrix datasheet for all the necessary information. The matrices I'm using are a common cathode 24 pin matrix. The common cathode part of it means that for a row to be on, it has to be set to LOW (cathode-negative). For this particular matrix pins 1-4 and 21-24 control the rows, pins 5-12 control the columns with green, pins 13-20 control the columns with red. The mappings are below:

How the shift registers work
Shift registers are still a bit of an enigma to me. I don't know exactly how they work but know how to use them. The shift register I'm using (MAX7219) is meant for LED control. It will take serial input (bits, one after another) and give parallel output (all the bits, all at once). It has 24 connections in total. Rather than explain each one in detail, this guide on the Arduino website covers pretty much everything: http://arduino.cc/playground/Main/MAX72XXHardware

The guide above also tells you how to choose the correct resistance value of Rset (in my case 30-40KOhms).

Arduino program
My source code will be as a link on part 2, but I will explain a bit about the program here.

My program uses the LED control library (explained below) so that needs to be imported.

One of the first lines is LedControl lc=LedControl(2,3,4,1); This basically means create a new instance of LED control, using pins 2,3 and 4. The number 1 corresponds to the number of devices (which will always be 1 for my program).

Next I have the step definitions. Each step consists of and array of 48 bytes, as I have 24 steps thats 24 * 48 bytes or 1.125KB which is a heck of a lot to hard code. Luckily I have created a program to do that for me, but that's another subject. It is likely that anyone following this will be using far smaller "steps" and fewer devices.

After the step definitions there is a small bit of code denoting the delay between each step, this is mainly so I can simulate a bit of acceleration by reducing the delay per step.

Following that are the step assignments. In brief these assignments tell the program which parts of the array to assign to which LED control instance.

For my particular case at the end of each set of steps I wipe the LEDs and start again.

IMPORTANT - There must always be a delay at the beginning/end of your step definitions, I'm not sure why but it messes up otherwise.

And that's it :)

Using the LED control library
Firstly make sure you have the Arduino software running and working (version 1.0 was released not too long ago).

I have used the LED control library, details of which can be found here: http://www.arduino.cc/playground/Main/LedControl

You can also download the library as a .zip file from that link, the link to the file is in the section "Sourcecode and download".

Make sure the Library files go to the "Libraries" or "Lib" folders in the Arduino program.

To use the library, go to Tools -> Import Library -> LED control. You may have to close and open the Arduino program again if it was open when you added the library files.

Now you're set to go :D.

I will gladly try to answer any questions so please feel free to comment/email me with any queries.

Now have a look at part 2

Jake



Did you find this post interesting?