The following tutorial will demonstrate how to Read values from a Text file (.txt, .csv) to blink 1 of 9 LEDs attached to an Arduino. It uses the combination of an Arduino and Processing program to process the file. The Processing program will read the text file in real time, only sending new information to the 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.
1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1
- Save the file on your hard drive. In my case, I have chosen to save the file at this location.
D:/mySensorData.txt
- 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 | style="color: blue;">import processing.serial.*; |
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 | style="color: green;">/* This program was created by ScottC on 8/5/2012 to receive serial |
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.
Other programs can be used to create text file, but you will need the processing program to read the file and send the values to the Arduino. The Arduino will receive each value and react appropriately.
SIMILAR PROJECT: Use a mouse to control the LEDs on your Arduino - see this post.
SIMILAR PROJECT: Use a mouse to control the LEDs on your Arduino - see this post.
An alternative Processing Sketch
This Processing sketch uses the loadStrings()method instead of the FileReader method used in the first sketch. This sketch also provides better control over sending the values to the Arduino. When the sketch first loads, the application window will be red. By clicking your mouse inside the window, the background will turn green and the file will be imported and sent to the Arduino, with every value being sent at half second intervals. If you update the text file and save, only new values will be transmitted, however, if you want the entire file to transmit again, you can press the window once (to reset the counter), and then again to read the file and send the values again from the beginning of the file.
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).
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 | style="color: rgb(0, 128, 0);">/* TextFile Sender: Written by Scott C on 5th April 2013 |
No comments:
Post a Comment