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:
Delete the if statement with the switch
Changed myport ('0') to myport ('9') since that's where my wire is connected
Tried to use ByteRead on Arduino but it does nothing
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);
}
}
}