I'm trying to read a series of numbers in a text file on my PC then send them to my Arduino board via serial port. The numbers are split by commas and dashes so I perform two loops. Sending a number to the serial port should light the equivalent LED on the Arduino board. The problem is that only the first iteration in the first loop doesn't light the equivalent LEDS in run mode though it does in the debug mode.; other iterations and other loops run flawlessly. There aren't any problems with the hardware 100% for sure. Any help would be greatly appreciated. Here is the code:

import processing.serial.*;
import java.io.*;
int mySwitch=0;
int counter1=0;
int counter2=0;
String [] subtext;
String[] Lines;
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

 //Open the serial port for communication with the Arduino
 //Make sure the COM port is correct
 myPort = new Serial(this, "COM36", 9600);

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. */

 /*The following switch prevents continuous reading of the text file, until
 we are ready to read the file again. */
 /*Only send new data. This IF statement will allow new data to be sent to
 the arduino. */

 subtext = splitTokens(Lines[counter1],","); 
 /* 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'); */



// delay(100);
 //Increment the counter so that the next number is sent to the arduino.

 //If the text file has run out of numbers, then read the text file again in       5 seconds.


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

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

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

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

Here is the Arduino code:

void setup() { 
 // initialize the digital pins as an output.
 pinMode(2, OUTPUT);
 pinMode(3, OUTPUT);
 pinMode(4, OUTPUT);
 pinMode(5, OUTPUT);
 pinMode(6, OUTPUT);

// Turn the Serial Protocol ON

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.

 //Turn off all LEDs if the byte Read = 0
 //Turn off all LEDS
 digitalWrite(2, LOW);
 digitalWrite(3, LOW);
 digitalWrite(4, LOW);
 digitalWrite(5, LOW);
 digitalWrite(6, LOW);


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

1 Answer 1


I'm confused. What language is this? Anyway my guess is that you aren't terminating what you send to the Arduino properly (would have helped to see the Arduino code). The reason I think its this is you are sending a zero after you number. How does the Arduino know that the zero is not part of the number, do you mean a null or new line?

  • Thanks a lot for your concern. I have just included the Arduino code in the question; I'd appreciate it if you had a look at it.
    – Doua Ali
    May 10, 2016 at 21:24
  • The zero just briefly turns off all the LEDs so that there would be a break between the lighting of the LEDs of each line and the lighting of the LEDs of the next line.
    – Doua Ali
    May 10, 2016 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.