1

I am currently making an Arduino move at certain positions depending on numbers I send to a text variables. I am using the Processing IDE and Arduino IDE.

The problem I am currently getting is that Arduino does not read the text file at all. Instead I have to manually type in numbers myself.

Just to summarize, I am trying to get a text file that only contains ONE number. Have the processing app read it and then have Arduino move at certain positions.

Here is what I have tried so far:

  1. Delete the if statement with the switch

  2. Changed myport ('0') to myport ('9') since that's where my wire is connected

  3. Tried to use ByteRead on Arduino but it does nothing

  4. Tried to convert ByteRead into an int instead of a byte

I have checked on Google, but no luck the only link I was able to find is this http://arduinobasics.blogspot.com/2012/05/reading-from-text-file-and-sending-to.html

import processing.serial.*;
import java.io.*;

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

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

void draw() {
    /*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("C://Users//InGodWeTrush//Desktop//maxColorIndex.txt");

    /*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. */
  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(20000);
    myPort.write('0');
    delay(11000);
    //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(20000);
  }
} 

/* 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();
    }
  }
}

My Arduino code:

#include <Servo.h>
Servo servol;

void setup() {
  // initialize the digital pins as an output.
  servol.attach(9);
  // 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) {
      servol.write(18);
      Serial.println("The color is Red");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 1) {
      servol.write(36);
      Serial.println("The color is Orange");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 2) {
      servol.write(54);
      Serial.println("The color is Green");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 3) {
      servol.write(72);
      Serial.println("The color is Blue");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 4) {
      servol.write(90);
      Serial.println("The color is Purple");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 5) {
      servol.write(108);
      Serial.println("The color is Pink");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 6) {
      servol.write(126);
      Serial.println("The color is Red");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 7) {
      servol.write(144);
      Serial.println("The color is Black");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 8) {
      servol.write(162);
      Serial.println("The color is Grey");
      delay(10000);
      servol.write(1);
    } else if (byteRead == 9) {
      servol.write(180);
      Serial.println("The color is White");
      delay(10000);
      servol.write(1);
    } else {
      Serial.println("Error");
      delay(10000);
      servol.write(0);
    }
  }
}
2
  • You are sending the bytes every 0.1/0.5 seconds, but reading them every 10 seconds. Commented Dec 11, 2017 at 21:36
  • Thanks for the response. I increased the time, but it still won't read my text file. What I am noticing is the first one that I press run on (Processing IDE or Arduino) will work. If I run my Arduino code first, then it will say that COM3 is busy on the Processing IDE. If I run the code on the Processing IDE first, then I get an error on Arduino saying that there was a problem uploading to the board. Commented Dec 11, 2017 at 21:52

1 Answer 1

1

So, you connected Arduino on Serial port (or through USB-UART converter, doesn't metter), let say COM1, and then send to this port some data from one program (Processing IDE) and then trying to read from other program (Arduino IDE)?

It's impossible to connect multiple programs to the same COM port at the same time. You read byte from serial port (from Processing IDE, I guess), then you send texts by serial port back (trying to send it to Arduino IDE?).

You should do everything (write serial and read serial) in the same application on PC side (which should be connected first to COM port and will be holding it, preventing other programs to access it)

-OR-

you should use two serial ports connected to different COM ports (may be with two USB-UART converters).

Note that Arduino UNO, Nano etc. with ATmega328 chip have only one hardware serial. In this case you should use some software UART emulation to get to work second serial port.

The simpliest option is just to remove Serial.println from Arduino code and make indication on Arduino by hardware (LEDs etc.) and don't connect Arduino IDE to the board during work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.