Reading from a Text File and Sending to Arduino

Components Required
- Arduino UNO
- Breadboard
- 9 LEDs
- 9 x 330 ohm resistors
- Wires to connect the circuit
- USB connection cable: to connect the computer to the Arduino
- A computer: to run the processing sketch, and to compile / upload the Arduino sketch
- Processing Program installed on computer
- Arduino Program installed on the computer
- A comma separated text file (*.txt).

Arduino Layout

The Text File
- Open Notepad or equivalent text file editor, and paste the following data into it.
- Save the file on your hard drive. In my case, I have chosen to save the file at this location.
- It should look like the following screenshot
Additional notes regarding the Text file:
- Just remember what you call it, and where you saved it, because we will be referring to this file later on in the Processing script.
- Keep all values on the same line.
- Separate each number with a comma.
- The number 1 will blink the first LED which is attached to Pin 2 on the Arduino.
- The number 9 will blink the last LED which is attached to Pin 10 on the Arduino.

Processing Code
You can download the Processing IDE from this site.1 |
|
I used this site to highlight and format my code.
Once you have copied the text above into the Processing IDE, you can now start working on the Arduino code as seen below.

Arduino Code
You can download the Arduino IDE from this site.Copy and paste the following code into the Arduino IDE.
1 |
|
Additional Information:
- The Arduino code will still work without the processing program. You can open the serial monitor window to send the commands to the Arduino manually. In fact, if you encounter any problems, I would suggest you do this. It will help to identify the root cause of the problem (ie Processing or Arduino Code, or physical connections).
- If you choose to use the Serial Monitor feature of the Arduino IDE, you cannot use the Processing program at the same time.
Once you have assembled the Arduino with all the wires, LEDs, resistors etc, you should now be ready to put it all together and get this baby cranking!

Connecting it all together
- Connect the USB cable from your computer to the Arduino, and upload the code.
- Keep the USB cable connected between the Arduino and the computer, as this will become the physical connection needed by the Processing Program
- Make sure that you have the text file in the correct location on your hard drive, and that it only contains numbers relevant to the code provided (separated by commas).
- Run the Processing program and watch the LEDs blink in the sequence described by the text file.
- You can add more numbers to the end of the line, however, the processing program will not be aware of them until you save the file. The text file does not have to be closed.
SIMILAR PROJECT: Use a mouse to control the LEDs on your Arduino - see this post.

An alternative Processing Sketch
I personally like this updated version better than the first, plus I was inspired to update this blog posting due to the fact that some people were having problems with the FileReader method in the first sketch. But both sketches should work (they worked for me).
1 |
|