Go Down

Topic: Processing serial fails (Read 126 times) previous topic - next topic

Hello guys
I've got an arduino Uno hooked up to my PC and am sending few values from a few sensors  to the PC using  serial. It works fine on the arduino side but fails when I try to display it using Proceesing. My Processing code is -
import processing.serial.*;

Serial myPort;  // Create object from Serial class
String C = "";
String H = "";
String R = "";
String L = "";
PFont font;
int index0 = 0;
int index1 = 0;
int index2 = 0;
int index3 = 0;
String data = "";

void SerialEvent(Serial myport) {
  data = myPort.readStringUntil('.');
  data = data.substring(0, data.length() - 1);

  index0 = data.indexOf(",");
  index1 = data.indexOf("/");
  index2 = data.indexOf("|");
 

  R = data.substring(0, index0);
  H = data.substring(index0 + 1, index1);
  L = data.substring(index1 + 1, index2);
  C = data.substring(index2 + 1, data.length());
}

void setup()
{
  size(400, 400);
  myPort = new Serial(this, "COM8", 9600);
  myPort.bufferUntil('.');
  font = loadFont("Aldhabi-48.vlw");
  textFont(font, 48);
}

void draw()
{

  background(0);             // Set background to white

  fill(255);
  text(H, 200, 200);
  fill(255);
  text(C, 250, 200);
  fill(255);
  text(L, 300, 200);
  fill(255);
  text(R, 350, 200);
}



Arduino Code is -

int rSensor = A0;
int hygrometer = A1;
int lSensor = A2;
int tSensor = A3;
int rVal = 0;
int hVal = 0;
int lVal = 0;
int tVal = 0;

void setup() {
  pinMode(rSensor, 0);
  pinMode(hygrometer, 0);
  pinMode(lSensor, 0);
  pinMode(tSensor, 0);
  Serial.begin(9600);

}

void loop() {
 rVal = analogRead(rSensor);
 hVal = analogRead(hygrometer);
 lVal = analogRead(lSensor);
 tVal = round((analogRead(tSensor)*4.88)/10.0);
  Serial.print(rVal);
  Serial.print("R,");
  Serial.print(hVal);
  Serial.print("H/");
  Serial.print(lVal);
  Serial.print("L|");
  Serial.print(tVal);
  Serial.println("C.");
  delay(500);
}

riesens

First, please post code within code-tags (such as I have) - It makes it easer to read.

Code: [Select]

import processing.serial.*;


There's no such import statement in C++. You need to use the following to import libraries:

Code: [Select]
#include <library>
My latest blog posts:
Arduino, MQTT and Protobuf: http://blog.noser.com/arduino-iot-with-mqtt-and-nanopb/
Lightweight Network Communication: http://blog.noser.com/lightweight-network-communication/

riesens

sorry, i didn't see, that there are two code sections in your post, due to the missing code-tags.

Do you see the correct messages in a serial monitor?

You don't have a main() function in your java code.
My latest blog posts:
Arduino, MQTT and Protobuf: http://blog.noser.com/arduino-iot-with-mqtt-and-nanopb/
Lightweight Network Communication: http://blog.noser.com/lightweight-network-communication/

#3
Jan 07, 2015, 01:13 am Last Edit: Jan 07, 2015, 01:17 am by Anime_Freak
Sorry about the code tag thing, I'm new to this forum so yeah. And btw, about the main function, I'm using Processing, a derivation of Java but much simpler. Processing doesn't require a main function, therefore there isn't one. Yes, I see the message properly using the serial monitor but the data isn't displayed properly when using Processing.

riesens

This means that your problem lies within you Processing code.

As I don't know the processing-System, I can't help you much. Maybe a Processing-Forum would be a better place to ask around, but maybe you need to redraw your window after the handling of the SerialEvent. Is it possible to print something (i.e the four Strings C,H,R, and L) to the console at the End of the Event Handle? (In Java this is usually done with System.out.println). If the Strings are displayed correctly on the console output, the problem lies within your graphics stuff. Otherwise within the data parsing.
My latest blog posts:
Arduino, MQTT and Protobuf: http://blog.noser.com/arduino-iot-with-mqtt-and-nanopb/
Lightweight Network Communication: http://blog.noser.com/lightweight-network-communication/

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy