Arduino不断从txt更新文件读取

时间:2018-06-21 05:11:03

标签: arduino processing

我想知道你们中的任何人是否可以通过解决我不知道如何解决的问题来帮助我。

我正在尝试创建一个可以使用arduino读取txt文件的项目。 Arduino正在使用Processing来访问文件,将数字推入串行端口,并且arduino读取串行端口以打开正确的灯以显示txt文件中的值(值1,2,3将打开引脚上的LED 2,3,4和0会关闭所有LED)。

处理和Arduino工作正常(它从文件中读取并显示正确的灯光)。问题是我无法使其循环,我尝试更改代码并添加循环,但无济于事。

这是代码。

处理

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, "COM11", 9600);
myPort.bufferUntil('\n');
}

void draw() {

// 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("D:/test.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'); */
myPort.write(subtext[counter]);
delay(500);
myPort.write('0');
delay(100);
//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

void setup() { 
 // initialize the digital pins as an output.
 pinMode(2, OUTPUT);
 pinMode(3, OUTPUT);
 pinMode(4, OUTPUT);
 pinMode(5, OUTPUT);
 pinMode(6, OUTPUT);
 pinMode(7, OUTPUT);
 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(10, OUTPUT);
// 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){
 //Turn off all LEDS
 digitalWrite(2, LOW);
 digitalWrite(3, LOW);
 digitalWrite(4, LOW);
 digitalWrite(5, LOW);
 digitalWrite(6, LOW);
 digitalWrite(7, LOW);
 digitalWrite(8, LOW);
 digitalWrite(9, LOW);
 digitalWrite(10, LOW);
 }

 //Turn LED ON depending on the byte Read.
 if(byteRead>0){
 digitalWrite((byteRead+1), HIGH); // set the LED on
 }
  }
 }

0 个答案:

没有答案