I am working on a project that uses processing to read data from a file and then sends these data to an arduino Uno board using serial, then supply these values to the arduino braccio Braccio.ServoMovment() function to move the robotic arm according to them. however whenever I run the code I keep getting wrong values into the function and the arm moves in a weird way, I have tested the same code and tried to light up an led and it worked fine for some reason.
import processing.serial.*;
import java.io.*;
int mySwitch=0;
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
mySwitch=1;
//Open the serial port for communication with the Arduino
//Make sure the COM port is correct
myPort = new Serial(this, "COM5", 9600);
myPort.bufferUntil('\n');
}
void draw() {
// print(" the switch " + mySwitch);
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("C:/Users/Tareq/Desktop/data.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. */
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'); */
// print(subtext[counter]);
delay(5000);
myPort.write(subtext[counter]);
print(subtext[counter]);
delay(2000);
//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(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 */
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();
}
}
}
Arduino Code
#include <Braccio.h>
#include <Servo.h>
#include<SoftwareSerial.h>
Servo base;
Servo shoulder;
Servo elbow;
Servo wrist_rot;
Servo wrist_ver;
Servo gripper;
String stringData[6] ;
int intData[6] ;
void setup() {
Serial.begin(9600);
Braccio.begin();
pinMode(1, OUTPUT); // Set pin as OUTPUT
pinMode(2, OUTPUT); // Set pin as OUTPUT
pinMode(3, OUTPUT); // Set pin as OUTPUT
pinMode(4, OUTPUT); // Set pin as OUTPUT
pinMode(5, OUTPUT); // Set pin as OUTPUT
pinMode(6, OUTPUT); // Set pin as OUTPUT
pinMode(7, OUTPUT); // Set pin as OUTPUT
pinMode(8, OUTPUT); // Set pin as OUTPUT
pinMode(9, OUTPUT); // Set pin as OUTPUT
}
void loop() {
for(int y=0;y<6;y++){
while(Serial.available()== 0){}
stringData[y]= Serial.readString();
if(stringData[y]=="90")
intData[y]=90;
else if(stringData[y]=="120")
intData[y]=120;
else if(stringData[y]=="180")
intData[y]=180;
else if(stringData[y]=="45")
intData[y]=45;
else if(stringData[y]=="160")
intData[y]=160;
else if(stringData[y]=="170")
intData[y]=170;
else if(stringData[y]=="165")
intData[y]=165;
else if(stringData[y]=="60")
intData[y]=60;
else if(stringData[y]=="30")
intData[y]=30;
else if(stringData[y]=="110")
intData[y]=110;
else if(stringData[y]=="80")
intData[y]=80;
else if(stringData[y]=="155")
intData[y]=155;
}
Braccio.ServoMovement(20 , intData[0], intData[1], intData[2] , intData[3] ,intData[4] , intData[5]);
}
NOTE: The file im reading from is a notepad containing numbers separated with a " , " something like 150,30,60, etc for my code I used 6 for testing someone might say its weird why send string then map it into an int instead of sending int directly, well I tried the int first and it didn't work then I tried this.