2016-08-12 3 views
1

私は現在、シリアルイベントを介して加速度計の値を処理するArduinoプログラムを完璧に処理しています。セットアップに温度計を追加しようとしていますが、処理にはピンを読み取るだけで0が受信されます。 Serial.printを設定するとシリアルモニタに印刷されますが、加速度計の読み値と一緒に適切な値を送ることができません。Arduino Serial.write to Processingが0を返しますか?

Arduinoのコード:

int inByte = 0; 

void setup() { 
    Serial.begin(9600); 
    while (!Serial) { 
    ; // wait for serial port to connect. Needed for native USB port only 
    } 
    establishContact(); // send a byte to establish contact until receiver responds 
} 

void loop() { 

    if (Serial.available() > 0) { 

    // get incoming byte: 
    inByte = Serial.read(); 

    // send sensor values: 
    Serial.write(analogRead(A3)); // X AXIS 
    Serial.write(analogRead(A2)); // Y AXIS 
    Serial.write(analogRead(A1)); // Z AXIS 
    Serial.write(analogRead(A0)); // TEMPERATURE 
    } 
} 

void establishContact() { 
    while (Serial.available() <= 0) { 
    Serial.print('A'); // send a capital A 
    delay(300); 
    } 
} 

処理コード:

import processing.serial.*; 

Serial myPort;      
int[] serialInArray = new int[4];  
int serialCount = 0;     
int xInput, yInput, zInput; 
float temperature; 
boolean firstContact = false; 

void setup() { 
    size(600, 600, P3D); 
    pixelDensity(2); 
    noStroke(); 
    background(0); 
    printArray(Serial.list()); 
    String portName = Serial.list()[4]; 
    myPort = new Serial(this, portName, 9600); 
} 

void draw() { 
} 

void serialEvent(Serial myPort) { 
    // read a byte from the serial port: 
    int inByte = myPort.read(); 
    // if this is the first byte received, and it's an A, 
    // clear the serial buffer and note that you've 
    // had first contact from the microcontroller. 
    // Otherwise, add the incoming byte to the array: 
    if (firstContact == false) { 
    if (inByte == 'A') { 
     myPort.clear();   // clear the serial port buffer 
     firstContact = true;  // you've had first contact from the microcontroller 
     myPort.write('A');  // ask for more 
    } 
    } else { 
    // Add the latest byte from the serial port to array: 
    serialInArray[serialCount] = inByte; 
    serialCount++; 

    // If we have 3 bytes: 
    if (serialCount > 2) { 

     zInput = serialInArray[0]-80; 
     yInput = serialInArray[1]-80+69; 
     xInput = serialInArray[2]-77; 
     temperature = serialInArray[3]; // should return voltage reading (i.e 16ºc = 130); 
     //println("x = " + xInput + ", y = " + yInput + ", z = " + zInput + ", Temp = " + serialInArray[3]); 

     // Send a capital A to request new sensor readings: 
     myPort.write('A'); 
     // Reset serialCount: 
     serialCount = 0; 
    } 
    } 
} 

加速度値が完全に印刷するが、温度はちょうど返しシリアルモニタに0 Serial.print(analogRead(A0))私に正しい値を与えるので、温度計は間違いなく働いています。

ご協力いただきありがとうございます。この行に

+1

// 3バイトの場合はまだ温度がありません; – datafiddler

答えて

1

ため(serialCount> 2){

変更した場合(serialCount> = 4){

OR型キャストを使用するか、温度を変更しようとした場合整数のために!!!

int温度;

+0

ありがとうございます!これは、配列の長さをシフトしながら、私が受け取っていたデータの量よりもわずかに大きくなりました!いくつかの奇妙な理由のために私は4を必要としたときに配列の長さを5にしていましたが、tempは正しく読み始めました。フォトセルも追加できるように管理されているので、5つの入力がうまく動作します。ありがとうございました! – themessup

+0

私は1つの問題を抱えていますが、それは私の写真のセルセンサーは0から255までの値を処理中に送信するだけです(arduinoシリアルモニターは700 +写真セルは処理する値になります。どのように私はこれを修正することができますどのような考え? – themessup

関連する問題