0

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
 mySwitch=1;

 //Open the serial port for communication with the Arduino
 //Make sure the COM port is correct
 myPort = new Serial(this, "COM36", 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("Y:/MyTextFile.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. */

  while(counter1<Lines.length){
 subtext = splitTokens(Lines[counter1],","); 
 counter2=0;
 while(counter2<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[counter2]);

 //delay(300);

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

 counter2++;
 } 
 delay(2000);
 myPort.write('0');
 delay(2000);
 counter1++;
 }
 //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 */
 Lines = splitTokens(text,"-");
 }
 }catch(FileNotFoundException e){
 e.printStackTrace();
 }catch(IOException e){
 e.printStackTrace();
 }finally{
 try {
 if (br != null){
 br.close();
 }
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 }

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

 }

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

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?

2
  • 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 '16 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 '16 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.