9 May 2012

Reading from a Text File and Sending to Arduino

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import processing.serial.*;
import java.io.*;
int mySwitch=0;
int counter=0;
String [] subtext;
Serial myPort;


void setup(){
//Create a switch that will control the frequency of text file reads.
//When mySwitch=1, the program is setup to read the text file.
//This is turned off when mySwitch = 0
mySwitch=1;

//Open the serial port for communication with the Arduino
//Make sure the COM port is correct
myPort = new Serial(this, "COM6", 9600);
myPort.bufferUntil('\n');
}

void draw() {
if (mySwitch>0){
/*The readData function can be found later in the code.
This is the call to read a CSV file on the computer hard-drive. */
readData("D:/mySensorData.txt");

/*The following switch prevents continuous reading of the text file, until
we are ready to read the file again. */
mySwitch=0;
}
/*Only send new data. This IF statement will allow new data to be sent to
the arduino. */
if(counter<subtext.length){
/* Write the next number to the Serial port and send it to the Arduino
There will be a delay of half a second before the command is
sent to turn the LED off : myPort.write('0'); */
myPort.write(subtext[counter]);
delay(500);
myPort.write('0');
delay(100);
//Increment the counter so that the next number is sent to the arduino.
counter++;
} else{
//If the text file has run out of numbers, then read the text file again in 5 seconds.
delay(5000);
mySwitch=1;
}
}


/* The following function will read from a CSV or TXT file */
void readData(String myFileName){

File file=new File(myFileName);
BufferedReader br=null;

try{
br=new BufferedReader(new FileReader(file));
String text=null;

/* keep reading each line until you get to the end of the file */
while((text=br.readLine())!=null){
/* Spilt each line up into bits and pieces using a comma as a separator */
subtext = splitTokens(text,",");
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try {
if (br != null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* This program was created by ScottC on 8/5/2012 to receive serial 
signals from a computer to turn on/off 1-9 LEDs */

void setup() {
// initialize the digital pins as an output.
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
// Turn the Serial Protocol ON
Serial.begin(9600);
}

void loop() {
byte byteRead;

/* check if data has been sent from the computer: */
if (Serial.available()) {

/* read the most recent byte */
byteRead = Serial.read();
//You have to subtract '0' from the read Byte to convert from text to a number.
byteRead=byteRead-'0';

//Turn off all LEDs if the byte Read = 0
if(byteRead==0){
//Turn off all LEDS
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
}

//Turn LED ON depending on the byte Read.
if(byteRead>0){
digitalWrite((byteRead+1), HIGH); // set the LED on
}
}
}

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.



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).


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* TextFile Sender: Written by Scott C on 5th April 2013
using Processing Version 2.0b8 */

import processing.serial.*;

Serial comPort;
int counter=0; // Helps to keep track of values sent.
int numItems=0; //Keep track of the number of values in text file
boolean sendStrings=false; //Turns sending on and off
StringLoader sLoader; //Used to send values to Arduino

void setup(){
comPort = new Serial(this, Serial.list()[0], 9600);
background(255,0,0); //Start with a Red background
}

void draw(){
}


void mousePressed() {
//Toggle between sending values and not sending values
sendStrings=!sendStrings;

//If sendStrings is True - then send values to Arduino
if(sendStrings){
background(0,255,0); //Change the background to green

/*When the background is green, transmit
text file values to the Arduino */
sLoader=new StringLoader();
sLoader.start();
}else{
background(255,0,0); //Change background to red
//Reset the counter
counter=0;
}
}



/*============================================================*/
/* The StringLoader class imports data from a text file
on a new Thread and sends each value once every half second */
public class StringLoader extends Thread{

public StringLoader(){
//default constructor
}

public void run() {
String textFileLines[]=loadStrings("d:/mySensorData.txt");
String lineItems[]=splitTokens(textFileLines[0], ",");
numItems=lineItems.length;
for(int i = counter; i<numItems; i++){
comPort.write(lineItems[i]);
delay(500);
comPort.write("0");
}
counter=numItems;
}
}


82 comments:

  1. I was writing a blog post about reading in text files for Arduino as it doesn't have a file system and I found this post.

    Its really handy and combines stuff I had looked at.

    I will reference this blog and post when I publish my post.

    Well done :)

    ReplyDelete
    Replies
    1. Jake_F has identified a number of ways to read from a text file: for those interested - have a look at his blog.

      Delete
  2. Thanks for the post!
    Is there a way to upload and store the data from the txt file once into Arduino? Or does the Arduino need to be plugged into the computer the whole time the sketch runs?

    ReplyDelete
    Replies
    1. In this case - yes the Arduino needs to be plugged into the computer. However, you can get around that by using a shield of some sort. For example, an XBee, WiFi, or Bluetooth shield will allow you to detach the Arduino from your computer, or alternatively perhaps an SD card shield, where you can load the information to the card and then reference that afterwards. It depends on what you are trying to do, and whether you need the Arduino to act independently or not. It also depends on how much information you need to be stored/transmitted.

      Delete
  3. Thanks for the tutorial!
    Can the processing keep update the value of the txt.file by just pressing the "start" button once only instead of restarting the processing and run it again?

    ReplyDelete
    Replies
    1. Hi Anonymous,

      It has been a long time since I did this project - but from memory, I am pretty sure if you update the text file and press save... processing will continue from where it left off. I am pretty sure that you don't re-start Processing - otherwise it will read the file from the beginning. Give it a try.

      Delete
  4. I having the same problem ""Cannot find class of type named "FileReader""
    And i ADD "import java.io.*;" on the top...

    ReplyDelete
    Replies
    1. Not sure why it is not working for you. It works for me when I tested it out. Am using Processing version 2.0b8.

      Delete
  5. Null pointer exception in - if(counter<subtext.length){ -

    ReplyDelete
    Replies
    1. Hi Marcelo,

      I think I need to update this post. If you are desperate to use this code, then perhaps use an older version of Processing. Otherwise, try

      import java.io.FileReader;

      Your null pointer exception probably relates to the FileReader issue also.


      Delete
  6. I'm starting to program Arduino.
    I use PIC.. 16F877A and 16F628A...

    I need these code because i'm doing an laser engraving...
    All the cordinate will be in an text file....

    Thanks for everything!

    ReplyDelete
    Replies
    1. Marcelo,
      I have updated the code, try the second alternative Processing sketch and see if this works for you instead.

      Delete
  7. Thanks!!!
    I will change the code for send an caracter just when the software receive one specific byte by USB. I will use that logic for control the link and send the characters, so that the hardware control and not the software (in this case, an mause click).

    ReplyDelete
  8. dear sir,
    how to read a text file and display it using Arduino?

    ReplyDelete
    Replies
    1. *display on a LCD4884

      Delete
    2. Have you seen these pages :
      Arduino Forum

      Tronixstuff

      Once you send values to the arduino (as per my example), you should be able to send them to the shield as per the links provided.
      You may also want to look at my Serial communication tutorial.

      Delete
  9. Great tutorial but I am in need of a slight tweak, I have a text file where all the data is on separate rows like:
    1
    3
    4
    5
    2
    etc
    I would like to read the first 150 rows, stop and i'll program in a stepper then read the next 150 rows etc until the end of the file. the numbers still correspond to an LED being toggles. Is there any way you could help me please??

    ReplyDelete
    Replies
    1. Hi Jake,
      I have limited time because I am studying at the moment. So I cannot write it out for you, but I can give you some pointers.

      In the updated processing sketch, there is a line with the variable textFileLines[].

      From your example,
      textFileLines[0] = 1
      textFileLines[1] = 3
      textFileLines[2] = 4
      textFileLines[3] = 5
      textFileLines[4] = 2
      etc etc

      You don't have any delimiters on each line - so you don't need to use the splitTokens line.

      Why not read the entire file? and then do the 150 line/row grouping or handling afterwards using a couple of for-loops.
      Or if the file is only being updated progressively, then you could get a variable to hold what line you were up to, so that when you read the entire file again, you can skip the x number of lines.

      Scott

      Delete
    2. Cheers Scott, heading in the right direction now

      Good luck with your exams!

      Delete
  10. This is great! Thank you so much for giving this to us all. I'm wondering what your thoughts are on something I'm trying to accomplish that is similar, and I hope I can explain it well enough.

    I am playing with two IC chips and I'm trying to get the csv file (or text) to tell Arduino to turn on multiple leds simultaneously (I'd like them to be solid, not blinking). So, there will be two sets or arrays of data being sent, one for each IC.

    Array1(which is row1 in CSV) [1,2,3,4,5]
    Array2(which is row2 in CSV) [1,2,3,4,5]

    When Array1 comes back with a value of 3, then I'd like the third led to light.
    When Array2 comes back with a value of 5, then I'd like the fifth led to light.

    I am pretty new, and appreciate any help or direction you can provide. This project is going to be installed in an order fulfillment process to help me package and ship products faster.

    Thanks,

    Scott

    ReplyDelete
    Replies
    1. Hi Scott,

      Sounds like an interesting project.
      Will finish studying in about a month - which is when I will have more time to give more useful responses. You query confused me a little.
      How are you getting the values to the Arduino?
      And how does Array1 come back with a value of 3 ? How is that determined?

      Once you have selected a value from the array (I am assuming), you can send this value to the Arduino. In the example above, the LED stays on until you send a "0" to the Arduino, which is when it wipes it clean.
      The value is sent from a computer using the processing language.
      You can customise either of the programs (Arduino or Processing) to meet your specific requirements.

      You may decide that there is a special command to toggle the LED rather than switch them all off. It really depends on what you are trying to achieve and how you plan to achieve it.

      Regards
      Scott

      Delete
  11. This comment has been removed by a blog administrator.

    ReplyDelete
  12. i have been download the processing code 64-bit and when i upload the processing code, it says that "serial does not run in 64-bit mode"...
    what should I do ?
    please help me...>0<

    ReplyDelete
    Replies
    1. download and use the 32-bit version instead. Don't use the 64 bit version for Serial communication.

      Delete
  13. can you tell me the syntax to read an integer value that is stored in a text file and send it to arduino uno using serially.
    The integer stored in the text file will be periodically updated.

    Kindly Help ...

    ReplyDelete
    Replies
    1. String myString = "16";
      int myInt = int(myString);

      or

      int myInt = Integer.parseInt(myString);


      http://arduinobasics.blogspot.com.au/2011/06/reading-text-or-csv-file-using.html

      Delete
  14. Very good post,.
    but...

    i'm using servo controller using arduino and the protocol is "#0 P200 T100"
    #0 > ID
    P200 > Position
    T100 > delay

    and i'm using vb to control the arduino >> servo controller and i'm using your code :

    byteRead = Serial.read();
    /*ECHO the value that was read, back to the serial port. */
    Serial.println(byteRead);


    on serial monitor and i make pos 'whatever' work perfectly, but when i'm using VB to control (with Track Bar), data was very poor and not work well,. please help me to solving protocol combination all character, Thanks. :)

    ReplyDelete
    Replies
    1. Have never used VB with Arduino, would not know where to start.
      I think this is one for the forums... however, I am happy answering questions about my project.

      Delete
  15. Hey Scott thanks for this really helpful post.

    I wondering if it was possible to make processing receive some data from the arduino using the serial port and then write it to a file which some other c++ code running on the computer can use.

    I basically want the arduino to receive some data act accordingly and then when a button is pressed it sends the state of the process back to processing which then writes the received part to a file.

    Thanks in Advance.

    ReplyDelete
    Replies
    1. Yes - it is possible.
      I have a tutorial that shows how to do this - but unfortunately it is way out of date, and was done using Processing 1. However, I know that you can write to a file using processing 2, and you should be able to find it within their website.
      This link may help you get on the right track:
      https://www.processing.org/reference/PrintWriter.html

      Here is my old tutorial:
      http://arduinobasics.blogspot.com.au/2011/06/from-processingorg-how-to-append-text.html

      You should be able to work it out from these two links.

      Delete
  16. Hi Scott,

    I realize this is an old post and will be surprised if you answer this, but its worth a shot. The code works great except the last little bit:

    digitalWrite((byteRead+1), HIGH);

    If I change the "byteRead+1" to a specific pin it works fine but with this it just doesn't send the command to the pin!

    Many thanks

    ReplyDelete
    Replies
    1. Add this to the Arduino Sketch just after that digitalWrite line to help identify the problem:

      Serial.println(byteRead);

      Upload the code to the Arduino, and then open the serial monitor.
      If you want the LED attached to pin 2 to light up, then you need to type 1 in the serial monitor, and press enter. The LED attached to pin 2 should turn on, and the byteRead variable (1) should display in the serial Monitor.

      My guess is that you are not following my project as described.
      Which pin exactly are you having a problem with ?

      I just tested the sketch - and it works fine for me

      Delete
  17. Thankyou for your post.
    I'm using the MOUSE PRESSED CODE to send integers from a text file serially to the arduino. On the arduino i have a basic LED code where i have used 6 LEDs marked 1 2 3 4 5 and 6 respectively.So the processing code reads the integers from the text file and the LEDs light up respectivley.The problem is that after typing the integers for a total of 65-70 times the serial sending stops after that for example if i have typed 80 integers in the text file it wont work after the 65-70 range.I'm using a baud rate of 9600 and the delays have been set to 500 in both the arduino and the processing code..I cant seem to figure out the problem could you help me out with a syntax if that is what is needed to be added,

    And also what is the relation between the delay set in the arduino after each LED (500) on command and the delay provided in the processing code do they have to be same or can they be different?As changing the delays in both also seems to have a LITTLE effect on the numbers of integers that can be read but i cant go below a delay of 500 for the project im working on.THANKS

    ReplyDelete
  18. P.S im inserting the integers in the text file in the form 1,2,3,4,5,6,1,2,3,4,5,6 and so on ...and after a total of 65-70 integers (where im not counting the commas) the serial sending stops.

    ReplyDelete
    Replies
    1. sorry just lost in this code
      im inserting the integers in the text file in the form 123456123456123456 and so on ignore the above comment.

      Delete
    2. Hi Anonymous,
      Firstly - I am not sure where you are seeing a delay in my Arduino code??? It might be in your code, but not in mine.. so you might want to think about changing that.
      I control the speed of LED illumination from the Processing sketch. I can drop this delay down to 15 without issues, and I am sure you could go below 15 if you wanted to.
      I use comma delimiters to help with this process.
      I think however, that your main problem exists with the delay in your Arduino code.

      Delete
  19. How to repeat reading values?? Where to put the loop????

    ReplyDelete
    Replies
    1. In the alternative processing sketch, you would place the code within the mousePressed function in the draw function instead. You would have to put a delay.

      Delete
  20. Scott or anyone
    I'm trying to create an LED controller such as this, but without the PC running the processing code. Using an Arduino Uno or Mega alone, reading a table in a text file. Have you seen any examples of this?

    ReplyDelete
    Replies
    1. No, I haven't seen this being done. I think you need some software on the computer to read the text file. It doesn't have to be processing, but processing is one of the easier methods.

      Delete
  21. Thank you very much for the code! I'm currently using it to send data via xbee.Can you help me on the separator? I need to separate it when a new line of data log is added, splitTokens() needs string and the program won't accept splitTokens(text, "\n")

    ReplyDelete
    Replies
    1. Hi Anonymous:
      In the Alternative Processing Sketch above - you will see these lines:

      String textFileLines[]=loadStrings("d:/mySensorData.txt");
      String lineItems[]=splitTokens(textFileLines[0], ",");

      Because this tutorial only made use of one line of data, you will notice that the splitTokens method is working on the first line of the file : textFileLines[0]
      To get the next line, it would be textFileLines[1]
      You could use a loop to work your way from line to line.

      Delete
  22. (newbie alert)hey, thanks a lot for the code.i have few questions glad if you to clear them:
    (i)The processing IDE reads the values from the notepad and stores the values in a string datatype. so when it sends the data via the serial port to the arduino does the arduino consider it as a string or a char or the int(char) is sent.
    (ii)if the notepad has a value fr example ,20, so does the subtext array save the value as 2 and 0 seperately

    ReplyDelete
  23. Hi Anonymous,
    (i) In the Arduino sketch above - you will notice this line:

    byte byteRead; and this line byteRead = Serial.read();

    Therefore, the Arduino is expecting a byte.

    ii) If the notepad has values of 0,20,10 within the text file then this line:

    String lineItems[]=splitTokens(textFileLines[0], ",");

    will create an array where lineItems[0] = 0 lineItems[1] = 20 lineItems[2] = 10

    So what will happen if you send these numbers to the Arduino?
    I think my Serial Communication tutorial will help you understand.

    ReplyDelete
    Replies
    1. so when subtext[1]= 20 when sent via myPort.write(subtext[1]); the arduio first recives 2 and then 0 so i gotta save the int of the value received and then save it in two variables for example a=2 and b=0 and then do c = a*10+b so that i finally get 20 for the exection of my code(by the way 2 is sent first then after that 0 ...right?)

      Delete
    2. Yes - you are right. However, this is an old tutorial, and Arduino have improved their Serial commands. If you want to read a byte, then the tutorial above shows you exactly how to handle that. But if you want to read an integer from the Serial port (on the Arduino), which I gather is what you are trying to do, then you could do this instead:

      int newData = Serial.parseInt();

      However, you must make certain that the data being sent is actually an integer.
      You might want to make use of the Serial.peek() command..

      Anyway - it just depends on what you are trying to do, in order to decide what approach to take. However, you may need to introduce some extra safety features to ensure that it works the way you anticipate.

      It is worth playing around with the Serial monitor first, and once you are happy with the Arduino functionality - move to the Processing sketch.

      Delete
    3. Hi Anonymous,
      For the sake of keeping the comments section tidy, and to be able to help you better, I suggest we take this discussion to the ArduinoBasics Forum. This comments section is best served for simple answers or comments. Please create a new topic, in the help me with my project section, and I will reply accordingly.

      Delete
  24. Hi Friends!
    How to make the same application but now to use the text file in a SD card using arduino!

    ReplyDelete
    Replies
    1. I don't have any SD card projects - but I will have some soon.
      If you would like to send or store text to a USB stick instead, then have a look at this tutorial:
      http://arduinobasics.blogspot.com/2015/05/ch376s-usb-readwrite-module.html

      Delete
  25. Hi Scott
    Your blog experiment on reading a text file using arduino worked fine with the second processing program. But how to make the blinking to be continuous in a single mouse click.

    ReplyDelete
    Replies
    1. When you say continuous, do you mean like an endless loop - so that the sequence repeats over and over?

      If so, you could get processing to re-read the text file when it gets to the end of the file, and just keep repeating over and over.

      Delete
  26. Hi Scott
    You're right. I want it to be continuous. But how do make that work.

    ReplyDelete
  27. Hi, I can't find in your Processing code to put the COM port for my computer.
    is it here?
    comPort = new Serial(this, Serial.list()[0], 9600);
    if i change the 0 to my COM 3 port, i will get this error
    ArrayIndexOutOfBoundException: 3

    ReplyDelete
    Replies
    1. Hi SC,

      Serial.list()[0] = will select the first available COM port - which may be COM3 in your case.
      If there are no available COM ports, you will get an error.
      If there is more than one COM port to choose from, then
      Serial.list()[0] will select the first one, Serial.list()[1] will select the second one ...etc etc

      If you want to select a specific COM port (e.g. COM3),
      then you could write this:
      comPort = new Serial(this, "COM3", 9600);

      If you would like to see what COM ports are available, you could include this:
      printArray(Serial.list());

      And the reason you got this error "ArrayIndexOutOfBoundException: 3",
      is probably because you wrote this:
      comPort = new Serial(this, Serial.list()[3], 9600);
      And I am guessing that you did not have 4 available COM ports, and thus went beyond the limits of the Serial.list() array.

      I hope that makes sense.

      Delete
    2. Understood, thank you very much

      Delete
  28. This comment has been removed by a blog administrator.

    ReplyDelete
  29. Cagdas Akalın : I deleted your question from this comments section because this area is not suitable for this type of interaction. If you would like help with your project, please post your question in the ArduinoBasics forum.. Thank you very much - I appreciate your understanding.

    ReplyDelete
  30. Hi There,
    I have this code that I would like to write a .TXT file that I could be able to change the parameters of the two variable that I have in my arduino code without uploading my code every time to the arduino. So how do I do this? I looked at your video and I got totally confused what I need to do. I'm a beginner to arduino and I would deeply appropriate that if you could help me out on this. Thanks.

    ReplyDelete
    Replies
    1. Hi Anonymous,

      I am not exactly sure what you are trying to do?
      Are you trying to replicate my tutorial or are you trying to do something else?
      If you would like help with your project, then please ask within the forum:
      http://arduinobasics-forum.1116184.n5.nabble.com/Help-me-with-my-project-f2.html

      Delete
  31. hi scott

    is it possible to program the arduino board to compare a preset value with each value being read from a text fie then give a particular response when a match is found?

    ReplyDelete
    Replies
    1. Yes - but I would get the Processing sketch to do the comparison (because it would be quicker)... but you could do it on the Arduino also.

      Delete
    2. And can i also make the processing script read values from only one column of the text file and not all the columns? thanks

      Delete
    3. Of course. Just delimit it appropriately, and you can program it to read whatever column you choose.

      Delete
  32. Hi Scott,

    I have attempted to copy your code here and for some reason (although I have not changed anything) it does not work the way it is supposed to. I get some of the LEDs to turn on but not all of them. And I have tested the LEDs on their own so I know that the LEDs do in fact work normally. I often use the serial monitor on the arduino in order to debug using the print statement. However because you set up a new port connection this no longer seems possible. Any suggestions or words of advice?

    ReplyDelete
    Replies
    1. Hi Peter,

      To test the Arduino side of the project, use the Serial monitor (as you said) but don't run the Processing sketch. The processing sketch will occupy the COM port and will prevent you from running the Serial monitor. From the Serial monitor, you can send numbers to the Arduino to test the LEDS.

      Delete
  33. hi scott,

    is it possible to read 2 text files to the arduino at the same time ? my project is have 2 inputs variable (temperature and humidity) which is stored in temp.txt and humid.txt both of them are the inputs. any tips ? thank you

    ReplyDelete
    Replies
    1. Hi Ardi,
      The answer is yes and no. I will start with "No", the Arduino will not read both values at EXACTLY the same time.... but you can read both of the values really quickly. There are a few ways to do this, and it would be best served in the ArduinoBasics forum. Please create a new topic and explain in greater detail - what exactly you are trying to achieve.
      You can find the forum link at the top of this page - look for the tab labelled "FORUM". And select the category to ask questions about your own project..

      Regards
      Scott

      Delete
  34. Hi Scott,
    your blog is very useful. For my project i need to transfer data from PC to arduino and use those values for calculation.In your video, you transmitted data and based on the data received LEDs were lighten up. Is it possible to store those values in an array and perform calculation?

    ReplyDelete
  35. scott C,
    why if i have data in row ?
    like this
    0
    0
    2
    3
    15
    12
    5
    2
    1
    0
    that possible to read ? how ?

    ReplyDelete
    Replies
    1. String textFileLines[]=loadStrings("d:/mySensorData.txt");

      This will populate the textFileLines[] array .
      So in your case:
      textFileLines[0] = 0
      textFileLines[1] = 0
      textFileLines[2] = 2
      textFileLines[3] = 3
      textFileLines[4] = 15 ....

      You then just need a for-loop to send those values

      Delete
    2. scott , why this code cant read number like 10 , 20, 200 ? , this code just can read number like 1 until 9.

      Delete
    3. This should explain it:
      https://www.baldengineer.com/arduino-multi-digit-integers.html

      Delete
    4. Also - have a look at my Serial communication tutorial - which will give you a few more examples.

      http://arduinobasics.blogspot.com.au/2012/07/arduino-basics-simple-arduino-serial.html

      Delete
  36. scott c , how to read neative value ? ex : -17,-5,-4 ect

    ReplyDelete
    Replies
    1. This post will show you how:
      https://www.baldengineer.com/arduino-multi-digit-integers.html

      Look in the comments section of that post.

      Delete
  37. Hi.
    Can i store .accdb file into the SD card? or should i convert it to .csv file?

    ReplyDelete
  38. Hey!
    Thanks a lot for this tutorial, its one of the most informative ive seen! I plan on using transistors as switches rather than LEDs and would like to read a csv file which tells each switch (on/off) my file will look more like this

    1,1,0,1,0,1,1,1,0

    where each position in the file corresponds to a single pin. Ive been trying to use lines similar to this:

    if (subtext[1].equals("1"))
    {myPort.write('1');}
    if (subtext[2].equals("1"))
    {myPort.write('2');}

    ...and etc...

    For some reason, Only the first LED will turn on. any ideas?

    -Chris

    ReplyDelete
    Replies
    1. Hi Chris,

      At the top of this post, you will find some tabs. One of those tabs will have a link to the "Forum". Please create a new thread in this forum and post both your Arduino code and your Processing code. There is not enough information in your comment to say what is going wrong.

      Delete
  39. Hi, could I send the text file byte by byte to another Arduino?

    ReplyDelete
    Replies
    1. Yes:
      https://www.arduino.cc/en/Tutorial/MasterWriter

      Delete

Feel free to leave a comment about this tutorial below.
Any questions about your particular project should be asked in the ArduinoBasics forum.

Comments are moderated due to large amount of spam.