현재 텍스트 변수에 보내는 숫자에 따라 특정 위치에서 Arduino를 움직이고 있습니다. Processing IDE와 Arduino IDE를 사용하고 있습니다.Arduino에서 텍스트 파일을 실행하는 데 도움이 필요합니다.
현재 문제는 Arduino가 텍스트 파일을 전혀 읽지 않는다는 것입니다. 대신 수동으로 숫자를 입력해야합니다.
그냥 요약하면, 하나의 숫자 만 포함 된 텍스트 파일을 얻으려고합니다. 처리 응용 프로그램에서 읽은 다음 Arduino를 특정 위치로 이동시킵니다. 여기
내가 지금까지 시도한 것입니다 : 그 어디 이후가 ('0') ('9') myport하는 스위치
변경된 myport를 if 문을 삭제 내 와이어는 아두 이노에 ByteRead를 사용하려고
을 연결하지만 아무것도하지 않습니다
를 int로 ByteRead를 변환하는 시도 대신 바이트의
나는 Google에서 확인했지만, 운이 내가 찾을 수 있었던 유일한 연결이 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();
}
}
}
내 아두 이노 코드입니다 : 그래서
#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이 Processing IDE에서 사용 중이라고 표시됩니다.먼저 Processing IDE에서 코드를 실행하면 Arduino에서 보드에 업로드하는 데 문제가 있다는 오류가 표시됩니다. –