Pages: [1]   Go Down
Author Topic: 8 input data-->Arduino -->Proccesing  (Read 1291 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!

 I am working on project where I have 7 different sensor and one servo motor, so 8 different data (one data gives me a analog value of laser sensor, second data is position of servo motor and another 6 data are  analog values of photocells). All of this are connected on Arduino. I don't have no problems using serial monitor on Arduino IDE, to show me values of position and sensors.

Anyway, now I would like to visualize this data on Processing.org. Here I have problems where to start. I saw examples how to show data on processing with using one analog sensor on Arduino. Examples for proccesing.org are in different books like:

Code:
Example 2A: Light sensor (Wiring/Arduino)
// Code to read an analog value and write it to the serial port
int val;
int inputPin = 0; // Set the input to analog in pin 0
void setup() {
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
val = analogRead(inputPin)/4; // Read analog input pin, put in range 0 to 255
Serial.print(val, BYTE); // Send the value
delay(100); // Wait 100ms for next reading
}
Example 2B: Light sensor (Processing)
// Read data from the serial port and assign it to a variable. Set the fill a
// rectangle on the screen using the value read from a light sensor connected
// to the Wiring or Arduino board
import processing.serial.*;
Serial port; // Create object from Serial class
int val; // Data received from the serial port
void setup() {
size(200, 200);
noStroke();
frameRate(10); // Run 10 frames per second
// Open the port that the board is connected to and use the same speed (9600 bps)
port = new Serial(this, 9600);
}
void draw() {
if (0 < port.available()) { // If data is available to read,
val = port.read(); // read it and store it in val
}
background(204); // Clear background
fill(val); // Set fill color with the value read
rect(50, 50, 100, 100); // Draw square
}



or six analog sensor connected on Arduino (example on this post:http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1262190340, but in this case is using function print().Instead using function print() I used function text() but sensor values ​​were recorded one over the another. So in time there you couldn't see nothing. Proccesing code:

Code:
import processing.serial.*;     // import the Processing serial library
Serial myPort;                  // The serial port


int sensors[];
PFont font;

void setup() {
  // List all the available serial ports
  println(Serial.list());

  font = loadFont("Calibri-18.vlw");

  // I know that the first port in the serial list on my mac
  // is always my  Arduino module, so I open Serial.list()[0].
  // Change the 0 to the appropriate number of the serial port
  // that your microcontroller is attached to.
  String portName = Serial.list()[0];
  myPort = new Serial(this, Serial.list()[1], 9600);
  myPort.bufferUntil('\n');

  size(600, 600);
}

void draw() {

}
void serialEvent(Serial myPort) {
  // read the serial buffer:
  String myString = myPort.readStringUntil('\n');
  if (myString != null) {
    myString = trim(myString);

    // split the string at the commas
    // and convert the sections into integers:
    textFont(font, 15);
    fill(0);
    int sensors[] = int(split(myString, ','));
    // print out the values you got:
    for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
      text("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t",100*sensorNum, 200);
    }
  }
}



One part of my Arduino code is look like:
Code:
       for(pos = 60; pos < 120; pos += 1)  // goes from 0 degrees to 180 degrees
        {                                  // in steps of 1 degree
          myservo.write(pos);              // tell servo to go to position in variable 'pos'
          delay(150);      // waits 15ms for the servo to reach the position


          // record the maximum sensor value
          if (averageValue > sensorMax) {
            sensorMax = averageValue;
            polozajMaxGrobo = pos;

          }
          // record the minimum sensor value
          if (averageValue < sensorMin) {
            sensorMin = averageValue;
            positionMin = pos;
          }
Serial.print(pos);
      Serial.print(",");
      Serial.print(sensorValue00);
      Serial.print(",");
      Serial.print(sensorValue01);
      Serial.print(",");
      Serial.print(sensorValue02);
      Serial.print(",");
      Serial.print(sensorValue03);
      Serial.print(",");
      Serial.print(sensorValue04);
      Serial.print(",");
      Serial.print(sensorValue05);
      Serial.print(",");
        }

I heared for parsing but I don't know how to do it. I'll be very grateful if someone could help me to solve the problem or just give me an advice. I apologize if I forgot to add some information, so please ask me.

Thank you very much!
« Last Edit: April 06, 2012, 06:43:13 am by drejcek » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Instead using function print() I used function text() but sensor values ​​were recorded one over the another. So in time there you couldn't see nothing.
The last two arguments to the text() function define where to place the text. You use the same y value for all the text, changing only x, Clearly, you are not changing x by a large enough increment.

Quote
I'll be very grateful if someone could help me to solve the problem
What is the problem?

You have code that draw one line on a graph. You have other code that takes a stream of data, splits it into discrete values, and prints the values.

Combine the two, and draw 8 lines, in different colors, of course.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey!

Thanks for your answer PaulS. I tried what you suggest, but is not the problem that x or y position is so near to each other. The problem, what I think is that, when it makes a new loop, processing.org write new value over the last values. So in time it make unclear to read. I think I need to use function that clear window before new loop.  You know what I mean??

I posted picture just for example how it looks like.


Another think is, that I woud like to print bar diagram and values for photocells sensors, servo motor will print me a position in degress and laser sensor will also print me values. But I think I won't have problems with displaying values. But I have problems with string that I get from arduino and defining sensors at void serialEvent(Serial myPort) {}. So my another question is how to define for exemple sensor 3 I would like to put in x3, y3 position not with for loop?

I apologize because of my english and because I can not explain more professionally.

Thank you very much and regards!


* Capture.JPG (11.43 KB, 173x152 - viewed 30 times.)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If anybody know how to do it in Processing.org, should I use function myPort.clear(); to clear port buffer in processing code?  But where in my code should I put this? Or how should I do it?


Regards!

drejcek
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
should I use function myPort.clear(); to clear port buffer in processing code?
Why do you want to throw away random amounts of unread data?

Clear the screen before writing more text to it. You shouldn't be using the screen for debugging serial data input, anyway.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey,

yes I don't want to trow away unread data. But I want to clear window before new line of data will come. That's why I am asking you (everyone who can help me) what to do after every new loop processing doesn't write new value over last value?

Regards

drejcek
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But I want to clear window before new line of data will come.
No, you want to clear the window before you write new data on it. Completely different events. The background() function clears the screen.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey,

yes, you have right. Sorry!  Thank you. I tried and it working!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now arduino code is looking like:

Code:
void loop() {
  for(pos = 60; pos < 120; pos += 1)  // goes from 0 degrees to 180 degrees
  {
    delay(150);      // waits 15ms for the servo to reach the position

    Serial.print(",");
    Serial.print(pos);
    Serial.print(",");
    // read the sensor:
    int sensorValue0 = analogRead(A0);
    // print the results:
    Serial.print(sensorValue0);
    Serial.print(",");
    // read the sensor:
    int sensorValue1 = analogRead(A1);
    // print the results:
    Serial.print(sensorValue1);
    Serial.print(",");
    // read the sensor:
    int sensorValue2 = analogRead(A2);
    // print the results:
    Serial.print(sensorValue2);
    Serial.print(",");
    // read the sensor:
    int sensorValue3 = analogRead(A3);
    // print the results:
    Serial.print(sensorValue3);
    Serial.print(",");
    // read the sensor:
    int sensorValue4 = analogRead(A4);
    // print the results:
    Serial.print(sensorValue4);
    Serial.print(",");
    // read the sensor:
    int sensorValue5 = analogRead(A5);
    // print the results:
    Serial.print(sensorValue5);
    Serial.print(",");
    int sensorValue6 = analogRead(A6);
    Serial.print(sensorValue6);
    Serial.println(",");
  }
}

and part of processing code is looking like:
Code:
void draw() {
  background(250);
  delay(150);

  // read the serial buffer:
  String myString = myPort.readStringUntil('\n');
  if (myString != null) {
    myString = trim(myString);

    // split the string at the commas
    // and convert the sections into integers:
    textFont(font, 15);
    fill(0);


    int sensors[] = int(split(myString, ','));
    // print out the values you got:
    for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
      if (sensorNum == 0) {
        text("Laser Value: " + sensors[sensorNum] + "\t", 50, 55);
      }
      if  (sensorNum == 1) {
        text("Position: " + sensors[sensorNum] + "\t", 50, 73);
      }
      if  (sensorNum == 2) {
        text(sensors[sensorNum], 100*sensorNum, 420);
      }
      if  (sensorNum == 3) {
        text(sensors[sensorNum] + "\t", 100*sensorNum, 420);
      }
      if  (sensorNum == 4) {
        text(sensors[sensorNum] + "\t", 100*sensorNum, 420);
      }
      if  (sensorNum == 5) {
        text(sensors[sensorNum] + "\t", 100*sensorNum, 420);
      }
      if  (sensorNum == 6) {
        text(sensors[sensorNum] + "\t", 100*sensorNum, 420);
      }
      if  (sensorNum == 7) {
        text(sensors[sensorNum] + "\t", 100*sensorNum, 420);
      }
    }
  }
 
    Axis();
    Labels();
//  PrintBars();
}

I cut out part which was written in void serialEvent(Serial myPort) {} and paste it under void draw(){}. Here I don't have no problems with writing value one over another and some other problems.


Thanks to PaulS.

Regards!

drejcek
Logged

Miami/Florida
Offline Offline
Sr. Member
****
Karma: 14
Posts: 345
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi
Instead of clearing the whole screen with background(color); you can just draw a rectangle with the color you want in the area where you are going to text each new data. In that way you just refresh the dynamic new changing data but you dont have to rewrite the whole screen. You do it everytime in the code before texting the new data. The data will be refreshed and wont overlap. The screen background and static information on it remains the same. Faster and simpler this way. You may change the color of the rect depending on the values of the data for ex from green to red if data in above or below your threshold values just to make it look nice or you can just change the color of the font. The idea is to use a small portion of the screen for the dynamic information to be refreshed and leave the rest of the screen with the static info alone while possible. Less code to be written and faster.
Logged

Perseverance is 90% of the solution. The remaining 10% is more perseverance.

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Hello!

Thanks for advice arduinoadrian. I will try it in the near future!

Regards!

drejcek
Logged

Pages: [1]   Go Up
Jump to: