Arduino Serial to Processing Serial Problem with Bluetooth :(

edited October 2016 in Arduino

With HC-SR04 sensor, I'm trying to put Arduino Serial output to Processing with Bluetooth

On Arduino, I can get perfect distance output on serial monitor (like 2~30)

However, When I try to transport that serial to Processing with Bluetooth

It turns to someting like -1, 55, 54 on Processing Serial monitor

This is my Arduino code

int trigPin = A5;
int echoPin = A4;
void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
long duration, distance;
  digitalWrite(trigPin, HIGH);
  delay(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = ((float)(340 * duration) / 10000) / 2;

  Serial.print(distance);
  delay(500);
  if (Serial.available() > 0) {
    Serial.read();
    Serial.write(distance);
  }
}

This is Processing code

    import processing.serial.*;

    Serial serial;

    void setup() {
      size(400, 400);  

     // println(Serial.list());
      serial = new Serial(this, Serial.list()[1], 9600);
      int start = millis();
      while (millis() < start + 4000) {
      }
      serial.write(1);
     frameRate(10);
    }

    void draw() {
      if (serial.available() > 0) {
        background(serial.read(),0,0);
        serial.write(1);
      }
          println(serial.read());
    }

Can you help me?

Tagged:

Answers

  • Answer ✓

    Referring to your processing code, shouldn't your line 22 be handled by your if statement in line 18?

    I think it will be convenient to print what you are getting through your serial monitor in processing. To help understand better what you are receiving, print it in hex format:

    int inByte = serial.read();
    println("Received: 0x"+hex(inByte , 8));
    

    What do you get?

    Kf

Sign In or Register to comment.