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.


6 comments:

  1. I intend to use the logic in Method 1 so that Arduino can read .txt file stored in the computer. The values sent over serial port by processing needs to be the input of Arduino program. But when I run the two programs: one in Arduino and the other in processing, I get the error message "Port already in use". I know this is obvious but how can I mitigate this problem?

    ReplyDelete
  2. @Soumyabrata I've encountered this before. I think its because Process and Arduino are trying to access the port at the same time. I don't have anything set up to test it but try the following:

    Upload the relevant program onto your Arduino. Then close the Arduino IDE.

    Now if you run the Processing program it should work.

    Let me know if not :)

    ReplyDelete
  3. Is it possible this to work without opened Processing ? Just to plug the usb into the PC and Arduino to start to read from the file ?

    ReplyDelete
  4. Hi. I am interested with solution no 4. I acctually want to use your solution. Did you use PROGMEM or the flash memory?

    ReplyDelete
    Replies
    1. I didn't explicitly use any type of memory. When I was dealing with the .ino file it was through Java treating it as a text file.

      What is it you're trying to do?

      Delete
  5. This comment has been removed by the author.

    ReplyDelete

Did you find this post interesting?