2017-12-11 3 views
1

현재 텍스트 변수에 보내는 숫자에 따라 특정 위치에서 Arduino를 움직이고 있습니다. Processing IDE와 Arduino IDE를 사용하고 있습니다.Arduino에서 텍스트 파일을 실행하는 데 도움이 필요합니다.

현재 문제는 Arduino가 텍스트 파일을 전혀 읽지 않는다는 것입니다. 대신 수동으로 숫자를 입력해야합니다.

그냥 요약하면, 하나의 숫자 만 포함 된 텍스트 파일을 얻으려고합니다. 처리 응용 프로그램에서 읽은 다음 Arduino를 특정 위치로 이동시킵니다. 여기

내가 지금까지 시도한 것입니다 : 그 어디 이후

  1. 가 ('0') ('9') myport하는 스위치

  2. 변경된 myport를 if 문을 삭제 내 와이어는 아두 이노에 ByteRead를 사용하려고

  3. 을 연결하지만 아무것도하지 않습니다

  4. 를 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

0.1/0.5 초마다 바이트를 보내지 만 10 초마다 읽습니다. –

+0

응답 해 주셔서 감사합니다. 시간은 늘었지만 텍스트 파일은 읽지 않습니다. 내가 눈치 채는 것은 내가 (IDE 또는 Arduino 처리)를 실행하는 첫 번째 작업입니다. 내 Arduino 코드를 먼저 실행하면 COM3이 Processing IDE에서 사용 중이라고 표시됩니다.먼저 Processing IDE에서 코드를 실행하면 Arduino에서 보드에 업로드하는 데 문제가 있다는 오류가 표시됩니다. –

답변

1

, 직렬 포트 (또는 USB-UART 변환기를 통해 Arduino를 연결 한 경우)가 COM1이라고 말한 다음 한 포트 (처리 IDE)에서이 포트로 데이터를 보낸 다음 다른 프로그램에서 읽으려고 시도합니다 (Arduino IDE)?

여러 프로그램을 동일한 COM 포트에 동시에 연결할 수 없습니다. 직렬 포트에서 바이트를 읽으면 (처리 IDE에서), 직렬 포트를 통해 텍스트를 전송합니다 (Arduino IDE로 보내려고합니까?).

당신은 (그것을 잡고 그것을 액세스 할 수있는 다른 프로그램을 방지한다 COM 포트에 먼저 연결해야한다)

-OR를 PC 측에서 동일한 응용 프로그램에서 모든 것을 (직렬 쓰기 및 시리얼 읽기)해야

-

다른 COM 포트 (두 개의 USB-UART 변환기가있을 수 있음)에 연결된 두 개의 직렬 포트를 사용해야합니다.

ATmega328 칩을 장착 한 Arduino UNO, Nano 등은 하나의 하드웨어 시리얼을 가지고 있습니다. 이 경우에는 두 번째 직렬 포트를 작동시키기 위해 일부 소프트웨어 UART 에뮬레이션을 사용해야합니다.

가장 간단한 옵션은 Arduino 코드에서 Serial.println을 제거하고 하드웨어 (LED 등)로 Arduino를 표시하고 작업하는 동안 Arduino IDE를 보드에 연결하지 않는 것입니다.