需要帮助在Arduino中运行文本文件

时间:2017-12-11 21:18:43

标签: arduino processing

我目前正在某些位置进行Arduino移动,具体取决于我发送给文本变量的数字。我正在使用Processing IDE和Arduino IDE。

我目前遇到的问题是Arduino根本不读取文本文件。相反,我必须自己手动输入数字。

总结一下,我试图获得一个只包含一个数字的文本文件。让处理应用程序读取它然后让Arduino移动到某些位置。

这是我到目前为止所尝试的内容:

  1. 使用开关

  2. 删除if语句
  3. 将myport('0')更改为myport('9'),因为那是我的电线连接的地方

  4. 尝试在Arduino上使用ByteRead但它什么都不做

  5. 尝试将ByteRead转换为int而不是字节

  6. 我已经检查了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();
        }
      }
    }
    

    我的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);
        }
      }
    }
    

1 个答案:

答案 0 :(得分:1)

所以,你在串口上连接Arduino(或者通过USB-UART转换器,没有遇到),让我们说COM1,然后从一个程序(处理IDE)发送到这个端口的一些数据,然后尝试从其他程序(Arduino IDE)读取?

无法同时将多个程序连接到同一个COM端口。你从串口读取字节(我猜是来自Processing IDE),然后你通过串口发送文本(尝试将它发送到Arduino IDE?)。

您应该在PC端的同一个应用程序中执行所有操作(写入串行和读取串行)(应首先连接到COM端口并保留它,防止其他程序访问它)

-OR -

您应该使用连接到不同COM端口的两个串行端口(可能有两个USB-UART转换器)。

请注意,带有ATmega328芯片的Arduino UNO,Nano等只有一个硬件序列。在这种情况下,您应该使用一些软件UART仿真来使用第二个串口。

最简单的选择就是从Arduino代码中删除Serial.println并通过硬件(LED等)在Arduino上进行指示,并且在工作期间不要将Arduino IDE连接到电路板上。