【发布时间】:2018-05-25 11:40:56
【问题描述】:
我目前正在根据我发送到文本变量的数字在某些位置进行 Arduino 移动。我正在使用 Processing IDE 和 Arduino IDE。
我目前遇到的问题是 Arduino 根本不读取文本文件。相反,我必须自己手动输入数字。
总结一下,我正在尝试获取一个仅包含一个数字的文本文件。让处理应用程序读取它,然后让 Arduino 在某些位置移动。
这是我迄今为止尝试过的:
用switch删除if语句
将 myport ('0') 更改为 myport ('9'),因为那是我的电线连接的地方
尝试在 Arduino 上使用 ByteRead,但什么也没做
试图将 ByteRead 转换为 int 而不是字节
我在谷歌上查过,但运气不好,我能找到的唯一链接是这个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();
}
}
}
我的 Arduino 代码:
#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);
}
}
}
【问题讨论】:
-
您每 0.1/0.5 秒发送一次字节,但每 10 秒读取一次。
-
感谢您的回复。我增加了时间,但它仍然不会读取我的文本文件。我注意到的是我按下运行的第一个(处理 IDE 或 Arduino)将起作用。如果我先运行我的 Arduino 代码,那么它会说 COM3 在处理 IDE 上很忙。如果我先在处理 IDE 上运行代码,那么我会在 Arduino 上收到一个错误,说上传到开发板时出现问题。
标签: arduino processing