Serial communication between Arduino & Processing

Hi guys,
I am trying to load the data in the txt file from a micro SD card by Processing, and then send the values to my Arduino to make the stepper rotate.
But I have trouble to convert the string value from the Processing, the string seems like unreadable for stepper.

The data in txt file is like this,

2054,2054,-2054,-2054,-2054,
4108,2054,2054,
4108,2054,
2054,
2054,

I want Processing read every value which is separated by a comma, and then turn to the next lines.
So here is my Processing code.

import java.io.FileReader;
import java.io.FileNotFoundException;
import processing.serial.*;
import java.io.*;
Serial myPort;


ArrayList<String> sensorData=new ArrayList<String>();
ArrayList<Integer> columnOne=new ArrayList<Integer>();
int[] data = new int[100];
int flag = 0;

void setup(){
  myPort = new Serial(this, "/dev/cu.usbmodem114", 9600);
  readData("/Volumes/NO NAME/TASKA.TXT");

}

void draw(){
  if(flag==1){
      for (int i=0; i<sensorData.size(); i++){
    String [] text = splitTokens(sensorData.get(i),",");
    for(int d=0;d<text.length;d++){
      data[d] = int(text[d]);
      print(data[d]+" ");
      myPort.write(data[d]);
      myPort.write('\n');
      delay(1000);
    }
     delay(3000);
     println();
  }
  flag = 0;
  }
  
}

void readData(String myFileName){
  File file=new File(myFileName);
  BufferedReader br=null;
  
  try{
    br=new BufferedReader(new FileReader(file));
    String text=null;
    
    while((text=br.readLine())!=null){
      String [] subtext = splitTokens(text,",");
      columnOne.add(int(subtext[0]));
      sensorData.add(text);
    }
  }catch(FileNotFoundException e){
    e.printStackTrace();
  }catch(IOException e){
    e.printStackTrace();
  }finally{
    try {
      if (br != null){
        br.close();
        flag = 1;
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Here is the result, it looks perfect.

Because I use Arduino UNO board, I can not open the serial monitor while the Processing is working.
Therefore, I use the LCD monitor to check if there is any value going into Arduino and so does the LED for.

Currently, the information which displayed on the LCD monitor are “garbled text”.
But the LED was blinking at the right time while the value was transmitted from processing.
Here is my Arduino code.

int led_pin = 13;

#include <Stepper.h>
#define STEPS 2054  
Stepper stepper(STEPS, 9, 8, 7, 6);


//LCD Connecting for viewing the value to Arduino
#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;


void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode (led_pin,OUTPUT);
stepper.setSpeed(5);    
lcd.begin(16, 2);
}

void loop() {
  String StringRead = "";    
  int StringConvert = StringRead.toInt();
  
  while (Serial.available()) {
        char c = (char) Serial.read();
        if(c!='\n'){
            StringRead += c;
        }
        delay(500);    // To keep the speed of sending & receiving balance      
    }
  
  if (StringRead !=""){
        digitalWrite(13,HIGH);
        stepper.step(StringConvert);
    }else{
       digitalWrite(13,LOW);   
    }
 

 // read all the available characters
        lcd.setCursor(0, 0);
        if (Serial.available() > 0) 
        {
        // display each character to the LCD
        lcd.write(StringConvert);
        delay(200);
        }    
}

I suppose there is something wrong with the String to Int Function.
Please help me to solve this problem, I am a beginner in code.
Thanks you :slight_smile:

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

It would be a good idea to add to your Processing program the ability to display text that it receives from the Arduino as that you allow you to use Serial.print() to show debug messages.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

  String StringRead = "";   
  int StringConvert = StringRead.toInt();

What do you expect to get when you convert an empty String to an int?

  while (Serial.available()) {
        char c = (char) Serial.read();
        if(c!='\n'){
            StringRead += c;
        }
        delay(500);    // To keep the speed of sending & receiving balance     
    }

Reading one character every half second, while writing 6 characters per second makes that comment look pretty stupid, doesn't it?

You never assign a meaningful value to StringConvert, so telling the stepper to go to 0 over and over looks pretty silly.

Robin2: Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

It would be a good idea to add to your Processing program the ability to display text that it receives from the Arduino as that you allow you to use Serial.print() to show debug messages.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

To Robin, Thanks for the serial introduction. I am going to read this!

PaulS: What do you expect to get when you convert an empty String to an int?

You never assign a meaningful value to StringConvert, so telling the stepper to go to 0 over and over looks pretty silly.

To PaulS, Actually, I expect getting value like " 2045" and "-2045" to drive the stepper as what I want...

So what I should going to do is that put the Serial.read() value into a string and convert it to be meaningful?

So what I should going to do is that put the Serial.read() value into a string and convert it to be meaningful?

Yes.