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 ![]()
