2016-07-20 6 views
0

Arduino 컨트롤 및 처리 인터페이스를 통합하려고합니다. 내 Arduino 코드에는 a1, a0 및 d0 핀 (모두 digitalRead)에 연결된 세 개의 버튼이 있습니다.Arduino 및 처리 통합 - 단추 카운터

int x;// assigned to A0 input 
int y;// assigned to A1 input 
int z; //assigned to D0 input 
int votes[3]={0,0,0}; 

void setup() { 
    // initialize the serial communication 
    Serial.begin(9600); 

    while(!Serial); 
} 

void loop() { 
    // first we need to read the values from the BUTTONS 

    x = digitalRead(A0); 

    y = digitalRead(A1); 

    z = digitalRead(0); 

    if(digitalRead(A0) == HIGH) 
    { 
    Serial.print("cat1 ");     
    Serial.print(votes[0]=votes[0]+1); 
    Serial.print("\n"); 
    } 
    else if(digitalRead(A1) == HIGH) 
    { 
    Serial.print("cat2 "); 
    Serial.print(votes[1]=votes[1]+1); 
     Serial.print("\n"); 
    } 
    else if(digitalRead(0) == HIGH) 
    { 
    Serial.print("cat3 "); 
    Serial.print(votes[2]=votes[2]+1); 
    Serial.print("\n"); 
    } 

    delay(200); 
} 

I는 막대 그래프로 처리마다의 버튼을 누르면 시간 표시를 증가 카운터 싶다. 버튼을 누를 때마다 막대 그래프가 약간 올라갑니다. 이 경우 3 개의 줄 중 하나 (3 개의 단추와 연결됨)가 눌러지기를 원합니다.

처리 코드

은 다음과 같습니다 :

import processing.serial.*; 

// Serial Port variables 
Serial myPort; 
String buff = ""; 
String buff1 = ""; 
String buff2 = ""; 
int index = 0; 
int NEWLINE = 10; 
// Store the last 256 values from the sensors so we can draw them. 
int[] valuesx = new int[256]; 
int[] valuesy = new int[256]; 
int[] valuesz = new int[256]; 

void setup() 
{ 
    // set size of the window 
    size(512, 512); 
    // turn on anti-aliasing, this makes things look smoother 
    smooth(); 

    println(Serial.list()); // use this to determine which serial port is your Arduino 
    myPort = new Serial(this, Serial.list()[0], 9600); 
    myPort.bufferUntil('\n'); 
} 

// draw() happens every frame of the sketch. This is where all the calculations are made. 
// When draw() is finished executing, it executes again, over and over. 
void draw() { 
    // set the background to purple 
    background(87, 36, 124); 
    // set stroke weight(thickness of the line) to 5 pixels 
    strokeWeight(5); 

    for (int i = 0; i < 255; i++) { 

    stroke(247, i); 
    // draw the line (x1, y1, x2, y2) 
    line(1, valuesx[i], 1, valuesx[i + 1]); 
    line(5, valuesy[i], 5, valuesy[i + 1]); 
    line(10, valuesz[i], 10, valuesz[i + 1]); 
    } 

    // Check the Serial port for incoming data 
    while (myPort.available() > 0) { 
    // if there is data waiting... 
    // execute serialEvent() function. Look below to see how SerialEvent works. 
    serialEvent(myPort.read()); 
    } 
} 

// serialEvent controls how incoming serial data from the Arduino module is handled 
void serialEvent(int serial) 
{ 
    if (serial != NEWLINE) { 
    // Store all the characters on the line. 
    buff += char(serial); 
    } 
    else { 
    // The end of each line is marked by two characters, a carriage 
    // return and a newline. We're here because we've gotten a newline, 
    // but we still need to strip off the carriage return. 
    buff = buff.substring(0, buff.length()-1); 
    index = buff.indexOf(","); 
    buff1 = buff.substring(0, index); 
    buff2 = buff.substring(index+1, buff.length()); 

    // Parse the String into an integer. We divide by 4 because 
    // analog inputs go from 0 to 1023 while colors in Processing 
    // only go from 0 to 255. 
    int x = Integer.parseInt(buff1)/2; 
    int y = Integer.parseInt(buff2)/2; 

    // Clear the value of "buff" 
    buff = ""; 

    // Shift over the existing values to make room for the new one. 
    for (int i = 0; i < 255; i++) 
    { 
     valuesx[i] = valuesx[i + 1]; 
     valuesy[i] = valuesy[i + 1]; 
    } 

    // Add the received value to the array. 
    valuesx[255] = x; 
    valuesy[255] = y; 
    } 
} 

불행하게도, 나는 여기가 버튼을 누를 때 내가 오류입니다. processing_error

답변

0

쉼표를 포함 할 buff을 기다리고있어처럼 보이는,하지만하지 않습니다.

index = buff.indexOf(","); 

을 그럼 당신은 substring()를 호출 할 때 유효한 매개 변수하지 않은, 당신의 index-1에 전달되고 있습니다

당신이 indexOf(",") 전화, 그것은 더 쉼표가 발견되지 않았 음을 나타 내기 위해 -1을 반환 :

buff1 = buff.substring(0, index); 

그게 오류의 원인입니다. 이제 귀하의 다음 질문은 : buff에 쉼표가 포함되어 있지 않은 이유는 무엇입니까?

그리고 그 대답은 당신이 당신의 Arduino에서 기대하는 것에 달려 있습니다. 무엇인지 확인하려면 buff 값을 인쇄 해보십시오.

Arduino에 대해 많이 알지는 못하지만, 그 코드에서 쉼표를 인쇄하는 곳은 어디에도 없습니다.